Golang中常見(jiàn)加密算法的總結(jié)
如果想直接使用我下列的庫(kù)
可以直接go get 我的github
go get -u github.com/hybpjx/InverseAlgorithm
1.md5 加密——不可逆
MD5信息摘要算法是一種被廣泛使用的密碼散列函數(shù),可以產(chǎn)生出一個(gè)128位(16進(jìn)制,32個(gè)字符)的散列值(hash value),用于確保信息傳輸完整一致。
import (
"crypto/md5"
"encoding/hex"
"fmt"
)
第一種
// MD5Str md5驗(yàn)證
func MD5Str(src string) string {
h := md5.New()
h.Write([]byte(src)) // 需要加密的字符串為
fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
第二種
// MD5Str2 md5驗(yàn)證
func MD5Str2(src string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(src)))
}
2.hmacsha 加密——不可逆
HMAC是密鑰相關(guān)的哈希運(yùn)算消息認(rèn)證碼(Hash-based Message Authentication Code)的縮寫,它通過(guò)一個(gè)標(biāo)準(zhǔn)算法,在計(jì)算哈希的過(guò)程中,把key混入計(jì)算過(guò)程中。
和我們自定義的加salt算法不同,Hmac算法針對(duì)所有哈希算法都通用,無(wú)論是MD5還是SHA-1。采用Hmac替代我們自己的salt算法,可以使程序算法更標(biāo)準(zhǔn)化,也更安全。
hmac-md5加密
//key隨意設(shè)置 data 要加密數(shù)據(jù)
func Hmac(key, data string) string {
// 創(chuàng)建對(duì)應(yīng)的md5哈希加密算法
hash:= hmac.New(md5.New, []byte(key))
hash.Write([]byte(data))
return hex.EncodeToString(hash.Sum([]byte("")))
}
hamacsha1 加密
// HmacSha1 hmacSha1加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSha1(src, key string) string {
m := hmac.New(sha1.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
hamacsha 256 加密
// HmacSHA256 hmacSha256驗(yàn)證 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA256(key, src string) string {
m := hmac.New(sha256.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
hmacsha512加密
// HmacSHA512 hmacSha512驗(yàn)證
func HmacSHA512(key, src string) string {
m := hmac.New(sha512.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
hamasha 調(diào)用
package main
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
)
// Hmac hmac驗(yàn)證 key隨意設(shè)置 data 要加密數(shù)據(jù)
func Hmac(key, data string) string {
hash := hmac.New(md5.New, []byte(key)) // 創(chuàng)建對(duì)應(yīng)的md5哈希加密算法
hash.Write([]byte(data))
return hex.EncodeToString(hash.Sum([]byte("")))
}
// HmacSHA256 hmacSha256加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA256(key, src string) string {
m := hmac.New(sha256.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
// HmacSHA512 hmacSha512加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA512(key, src string) string {
m := hmac.New(sha512.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
// HmacSha1 hmacSha1加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSha1(src, key string) string {
m := hmac.New(sha1.New, []byte(key))
m.Write([]byte(src))
return hex.EncodeToString(m.Sum(nil))
}
// SHA256Str sha256加密
func SHA256Str(src string) string {
h := sha256.New()
h.Write([]byte(src)) // 需要加密的字符串為
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
func main() {
hmac_ := Hmac("hybpjx", "始識(shí)")
hamcsha1 := HmacSha1("hybpjx", "始識(shí)")
hamcsha256 := HmacSHA256("hybpjx", "始識(shí)")
hamacsha512 := HmacSHA512("hybpjx", "始識(shí)")
fmt.Println(hmac_)
fmt.Println(hamcsha1)
fmt.Println(hamcsha256)
fmt.Println(hamacsha512)
}
結(jié)果
d8801f70df7891764116e1ac003f7189
60d68e01c8a86f3b87e4e147e9f0fadce2a69661
b3f8ddf991288036864761a55046877adfe4f78ec9a89bb63932af92689b139f
b9b1fca0fe91522482ee1b2161e57d67482af6ef371614365b918c31ce774f9126ed627e378a063145f404ff2de7bd84f8e4798c385662ef4749e58e9209ca63
3.Sha 加密——不可逆
sha1
SHA-1可以生成一個(gè)被稱為消息摘要的160位(20字節(jié))散列值,散列值通常的呈現(xiàn)形式為40個(gè)十六進(jìn)制數(shù)。
func Sha1(data string) string {
sha1_ := sha1.New()
sha1_.Write([]byte(data))
return hex.EncodeToString(sha1_.Sum([]byte("")))
}
sha256
SHA256算法使用的哈希值長(zhǎng)度是256位。這是一個(gè)抽象類。此類的唯一實(shí)現(xiàn)是SHA256Managed。
// SHA256 sha256加密
func SHA256(src string) string {
h := sha256.New()
// 需要加密的字符串為
h.Write([]byte(src))
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
sha512
SHA (Secure Hash Algorithm,譯作安全散列算法) 是美國(guó)國(guó)家安全局 (NSA) 設(shè)計(jì),美國(guó)國(guó)家標(biāo)準(zhǔn)與技術(shù)研究院 (NIST) 發(fā)布的一系列密碼散列函數(shù)。
// SHA512 sha512加密
func SHA512(src string) string {
h := sha512.New()
// 需要加密的字符串為
h.Write([]byte(src))
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
sha調(diào)用
package main
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
)
func Sha1(data string) string {
sha1_ := sha1.New()
sha1_.Write([]byte(data))
return hex.EncodeToString(sha1_.Sum([]byte("")))
}
// SHA256 sha256加密
func SHA256(src string) string {
h := sha256.New()
// 需要加密的字符串為
h.Write([]byte(src))
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
// SHA512 sha512加密
func SHA512(src string) string {
h := sha512.New()
// 需要加密的字符串為
h.Write([]byte(src))
// fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
return hex.EncodeToString(h.Sum(nil))
}
func main() {
_sha1 := Sha1("始識(shí)")
_sha256 := SHA256("始識(shí)")
_sha512 := SHA512("始識(shí)")
fmt.Println(_sha1)
fmt.Println(_sha256)
fmt.Println(_sha512)
}
結(jié)果
7bac01cc58a26f3cb280b0466794a89441279946
6ef99e6d3fe34a46afcdc438435728fe95ffdab18e389ddd31609edd6729b11d
0c04e9b79f488646d0eac6f65468248507939d643cc92709b14eb0d18d8f13db509ed5ccd3312d6c234408185a4611a42525dce9e8d32255640f56a2f836635a
4.base 加密 解密
加密
// BASE64StdEncode base編碼
func BASE64StdEncode(src string) string {
return base64.StdEncoding.EncodeToString([]byte(src))
}
解密
// BASE64StdDecode base解碼
func BASE64StdDecode(src string) string {
a, err := base64.StdEncoding.DecodeString(src)
if err != nil {
_ = fmt.Errorf("解密失敗,%v\n", err)
}
return string(a)
}
base64 調(diào)用
package main
import (
"encoding/base64"
"fmt"
)
// BASE64StdEncode base編碼
func BASE64StdEncode(src string) string {
return base64.StdEncoding.EncodeToString([]byte(src))
}
// BASE64StdDecode base解碼
func BASE64StdDecode(src string) string {
a, err := base64.StdEncoding.DecodeString(src)
if err != nil {
_ = fmt.Errorf("解密失敗,%v\n", err)
}
return string(a)
}
func main() {
encodeBase64 := BASE64StdEncode("hybpjx")
decodeBase64 := BASE64StdDecode(encodeBase64)
fmt.Println(encodeBase64)
fmt.Println(decodeBase64)
}
結(jié)果
aHlicGp4
hybpjx
5.AES 加密
高級(jí)加密標(biāo)準(zhǔn)(英語(yǔ):Advanced Encryption Standard,縮寫:AES),在密碼學(xué)中又稱Rijndael加密法。這個(gè)標(biāo)準(zhǔn)用來(lái)替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。經(jīng)過(guò)五年的甄選流程,于2001年11月26日發(fā)布于FIPS PUB 197,并在2002年5月26日成為有效的標(biāo)準(zhǔn)。2006年,高級(jí)加密標(biāo)準(zhǔn)已然成為對(duì)稱密鑰加密中最流行的算法之一。
由于加密和解密的秘鑰是相同的,所以AES為對(duì)稱加密
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
//AES加密
func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = PKCS7Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
//AES解密
func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS7UnPadding(origData)
return origData, nil
}
func main() {
text := "今晚打老虎"
AesKey := []byte("0f90023fc9ae101e") //秘鑰長(zhǎng)度為16的倍數(shù)
fmt.Printf("明文: %s\n秘鑰: %s\n", text, string(AesKey))
encrypted, err := AesEncrypt([]byte(text), AesKey)
if err != nil {
panic(err)
}
fmt.Printf("加密后: %s\n", base64.StdEncoding.EncodeToString(encrypted))
origin, err := AesDecrypt(encrypted, AesKey)
if err != nil {
panic(err)
}
fmt.Printf("解密后明文: %s\n", string(origin))
}
CBC方式
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"log"
)
func AesEncryptCBC(origData []byte, key []byte) (encrypted []byte) {
// 分組秘鑰
// NewCipher該函數(shù)限制了輸入k的長(zhǎng)度必須為16, 24或者32
block, _ := aes.NewCipher(key)
blockSize := block.BlockSize() // 獲取秘鑰塊的長(zhǎng)度
origData = pkcs5Padding(origData, blockSize) // 補(bǔ)全碼
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) // 加密模式
encrypted = make([]byte, len(origData)) // 創(chuàng)建數(shù)組
blockMode.CryptBlocks(encrypted, origData) // 加密
return encrypted
}
func AesDecryptCBC(encrypted []byte, key []byte) (decrypted []byte) {
block, _ := aes.NewCipher(key) // 分組秘鑰
blockSize := block.BlockSize() // 獲取秘鑰塊的長(zhǎng)度
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式
decrypted = make([]byte, len(encrypted)) // 創(chuàng)建數(shù)組
blockMode.CryptBlocks(decrypted, encrypted) // 解密
decrypted = pkcs5UnPadding(decrypted) // 去除補(bǔ)全碼
return decrypted
}
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func pkcs5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func main() {
origData := []byte("460154561234") // 待加密的數(shù)據(jù)
key := []byte("9876787656785679") // 加密的密鑰
log.Println("原文:", string(origData))
log.Println("------------------ CBC模式 --------------------")
encrypted := AesEncryptCBC(origData, key)
log.Println("密文(hex):", hex.EncodeToString(encrypted))
log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
decrypted := AesDecryptCBC(encrypted, key)
log.Println("解密結(jié)果:", string(decrypted))
}
ECB方式
package main
import (
"crypto/aes"
"encoding/base64"
"encoding/hex"
"log"
)
func AesEncryptECB(origData []byte, key []byte) (encrypted []byte) {
cipher, _ := aes.NewCipher(generateKey(key))
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)
pad := byte(len(plain) - len(origData))
for i := len(origData); i < len(plain); i++ {
plain[i] = pad
}
encrypted = make([]byte, len(plain))
// 分組分塊加密
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted
}
func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
cipher, _ := aes.NewCipher(generateKey(key))
decrypted = make([]byte, len(encrypted))
//
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16)
copy(genKey, key)
for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}
func main() {
origData := []byte("460154561234") // 待加密的數(shù)據(jù)
key := []byte("9876787656785679") // 加密的密鑰
log.Println("原文:", string(origData))
log.Println("------------------ ECB模式 --------------------")
encrypted := AesEncryptECB(origData, key)
log.Println("密文(hex):", hex.EncodeToString(encrypted))
log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
decrypted := AesDecryptECB(encrypted, key)
log.Println("解密結(jié)果:", string(decrypted))
}
CFB 方式
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"io"
"log"
)
func AesEncryptCFB(origData []byte, key []byte) (encrypted []byte) {
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
encrypted = make([]byte, aes.BlockSize+len(origData))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(encrypted[aes.BlockSize:], origData)
return encrypted
}
func AesDecryptCFB(encrypted []byte, key []byte) (decrypted []byte) {
block, _ := aes.NewCipher(key)
if len(encrypted) < aes.BlockSize {
panic("ciphertext too short")
}
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(encrypted, encrypted)
return encrypted
}
func main() {
origData := []byte("460154561234") // 待加密的數(shù)據(jù)
key := []byte("9876787656785679") // 加密的密鑰
log.Println("原文:", string(origData))
log.Println("------------------ CFB模式 --------------------")
encrypted := AesEncryptCFB(origData, key)
log.Println("密文(hex):", hex.EncodeToString(encrypted))
log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
decrypted := AesDecryptCFB(encrypted, key)
log.Println("解密結(jié)果:", string(decrypted))
}
6.RSA加密
RSA是一種基于公鑰密碼體制的優(yōu)秀加密算法,1978年由美國(guó)(MIT)的李維斯特(Rivest)、沙米爾(Shamir)、艾德曼(Adleman)提的。
RSA算法是一種分組密碼體制算法,它的保密強(qiáng)度是建立在具有大素?cái)?shù)因子的合數(shù)其因子分解是困難的(基于大數(shù)分解的難度)。公鑰和私鑰是一對(duì)大素?cái)?shù)的函數(shù),從一個(gè)公鑰和密文中恢復(fù)出明文的難度等價(jià)于分解兩個(gè)大素?cái)?shù)之積。
RSA得到了世界上的最廣泛的應(yīng)用,ISO在1992年頒布的國(guó)際標(biāo)準(zhǔn)X.509中,將RSA算法正式納入國(guó)際標(biāo)準(zhǔn)。
RSA加密
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
)
// GenerateRSAKey 生成RSA私鑰和公鑰,保存到文件中
func GenerateRSAKey(bits int){
//GenerateKey函數(shù)使用隨機(jī)數(shù)據(jù)生成器random生成一對(duì)具有指定字位數(shù)的RSA密鑰
//Reader是一個(gè)全局、共享的密碼用強(qiáng)隨機(jī)數(shù)生成器
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err!=nil{
panic(err)
}
//保存私鑰
//通過(guò)x509標(biāo)準(zhǔn)將得到的ras私鑰序列化為ASN.1 的 DER編碼字符串
// X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
X509PrivateKey,err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
//使用pem格式對(duì)x509輸出的內(nèi)容進(jìn)行編碼
//創(chuàng)建文件保存私鑰
privateFile, err := os.Create("private.pem")
if err!=nil{
panic(err)
}
defer privateFile.Close()
//構(gòu)建一個(gè)pem.Block結(jié)構(gòu)體對(duì)象
privateBlock:= pem.Block{Type: "PRIVATE KEY",Bytes:X509PrivateKey}
//將數(shù)據(jù)保存到文件
pem.Encode(privateFile,&privateBlock)
//保存公鑰
//獲取公鑰的數(shù)據(jù)
publicKey:=privateKey.PublicKey
//X509對(duì)公鑰編碼
X509PublicKey,err:=x509.MarshalPKIXPublicKey(&publicKey)
if err!=nil{
panic(err)
}
//pem格式編碼
//創(chuàng)建用于保存公鑰的文件
publicFile, err := os.Create("public.pem")
if err!=nil{
panic(err)
}
defer publicFile.Close()
//創(chuàng)建一個(gè)pem.Block結(jié)構(gòu)體對(duì)象
publicBlock:= pem.Block{Type: "Public Key",Bytes:X509PublicKey}
//保存到文件
pem.Encode(publicFile,&publicBlock)
}
// RsaEncrypt RSA加密
func RsaEncrypt(plainText []byte,path string)[]byte{
//打開文件
file,err:=os.Open(path)
if err!=nil{
panic(err)
}
defer file.Close()
//讀取文件的內(nèi)容
info, _ := file.Stat()
buf:=make([]byte,info.Size())
file.Read(buf)
//pem解碼
block, _ := pem.Decode(buf)
//x509解碼
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err!=nil{
panic(err)
}
//類型斷言
publicKey:=publicKeyInterface.(*rsa.PublicKey)
//對(duì)明文進(jìn)行加密
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
if err!=nil{
panic(err)
}
//返回密文
return cipherText
}
// RsaDecrypt RSA解密
func RsaDecrypt(cipherText []byte,path string) []byte{
//打開文件
file,err:=os.Open(path)
if err!=nil{
panic(err)
}
defer file.Close()
//獲取文件內(nèi)容
info, _ := file.Stat()
buf:=make([]byte,info.Size())
file.Read(buf)
//pem解碼
block, _ := pem.Decode(buf)
//X509解碼
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err!=nil{
fmt.Println(err.Error())
os.Exit(0)
}
//對(duì)密文進(jìn)行解密
plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey.(*rsa.PrivateKey),cipherText)
//返回明文
return plainText
}
func main(){
// RSA/ECB/PKCS1Padding
// RSA是算法,ECB是分塊模式,PKCS1Padding是填充模式
// pkcs1私鑰生成openssl genrsa -out pkcs1.pem 1024
// pkcs1轉(zhuǎn)pkcs8私鑰 :openssl pkcs8 -in pkcs8.pem -nocrypt -out pkcs1.pem
// pkcs1 BEGIN RSA PRIVATE KEY
// pkcs8 BEGIN PRIVATE KEY
GenerateRSAKey(1024)
publicPath := "public_key.pem"
privatePath := "private_key.pem"
publicPath = "public.pem"
privatePath = "private.pem"
txt := []byte("hello")
encrptTxt := RsaEncrypt(txt,publicPath)
decrptCode := RsaDecrypt(encrptTxt,privatePath)
fmt.Println(string(decrptCode))
}
RSA分段加密
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"log"
"os"
)
func main() {
GenerateRSAKey(2048)
publicPath := "public.pem"
privatePath := "private.pem"
var a = []byte("hello")
encrptTxt, err := RsaEncryptBlock(a, publicPath)
if err != nil {
fmt.Println(err.Error())
}
encodeString := base64.StdEncoding.EncodeToString(encrptTxt)
decodeByte, err := base64.StdEncoding.DecodeString(encodeString)
if err != nil {
panic(err)
}
//生成RSA私鑰和公鑰,保存到文件中
decrptCode := RSA_Decrypts(decodeByte, privatePath)
fmt.Println(string(decrptCode))
}
func GenerateRSAKey(bits int) {
//GenerateKey函數(shù)使用隨機(jī)數(shù)據(jù)生成器random生成一對(duì)具有指定字位數(shù)的RSA密鑰
//Reader是一個(gè)全局、共享的密碼用強(qiáng)隨機(jī)數(shù)生成器
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
panic(err)
}
//保存私鑰
//通過(guò)x509標(biāo)準(zhǔn)將得到的ras私鑰序列化為ASN.1 的 DER編碼字符串
// X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
X509PrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
//使用pem格式對(duì)x509輸出的內(nèi)容進(jìn)行編碼
//創(chuàng)建文件保存私鑰
privateFile, err := os.Create("private.pem")
if err != nil {
panic(err)
}
defer privateFile.Close()
//構(gòu)建一個(gè)pem.Block結(jié)構(gòu)體對(duì)象
privateBlock := pem.Block{Type: "PRIVATE KEY", Bytes: X509PrivateKey}
//將數(shù)據(jù)保存到文件
pem.Encode(privateFile, &privateBlock)
//保存公鑰
//獲取公鑰的數(shù)據(jù)
publicKey := privateKey.PublicKey
//X509對(duì)公鑰編碼
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
panic(err)
}
//pem格式編碼
//創(chuàng)建用于保存公鑰的文件
publicFile, err := os.Create("public.pem")
if err != nil {
panic(err)
}
defer publicFile.Close()
//創(chuàng)建一個(gè)pem.Block結(jié)構(gòu)體對(duì)象
publicBlock := pem.Block{Type: "Public Key", Bytes: X509PublicKey}
//保存到文件
pem.Encode(publicFile, &publicBlock)
}
// RSA_Decrypts RSA解密支持分段解密
func RSA_Decrypts(cipherText []byte, path string) []byte {
//打開文件
var bytesDecrypt []byte
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
//獲取文件內(nèi)容
info, _ := file.Stat()
buf := make([]byte, info.Size())
file.Read(buf)
//pem解碼
block, _ := pem.Decode(buf)
//X509解碼
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
p := privateKey.(*rsa.PrivateKey)
keySize := p.Size()
srcSize := len(cipherText)
log.Println("密鑰長(zhǎng)度", keySize, "密文長(zhǎng)度", srcSize)
var offSet = 0
var buffer = bytes.Buffer{}
for offSet < srcSize {
endIndex := offSet + keySize
if endIndex > srcSize {
endIndex = srcSize
}
bytesOnce, err := rsa.DecryptPKCS1v15(rand.Reader, p, cipherText[offSet:endIndex])
if err != nil {
return nil
}
buffer.Write(bytesOnce)
offSet = endIndex
}
bytesDecrypt = buffer.Bytes()
return bytesDecrypt
}
// RsaEncryptBlock 公鑰加密-分段
func RsaEncryptBlock(src []byte, path string) (bytesEncrypt []byte, err error) {
//打開文件
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
//讀取文件的內(nèi)容
info, _ := file.Stat()
buf := make([]byte, info.Size())
file.Read(buf)
//pem解碼
block, _ := pem.Decode(buf)
//x509解碼
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
panic(err)
}
//類型斷言
publicKey := publicKeyInterface.(*rsa.PublicKey)
keySize, srcSize := publicKey.Size(), len(src)
log.Println("密鑰長(zhǎng)度", keySize, "明文長(zhǎng)度", srcSize)
offSet, once := 0, keySize-11
buffer := bytes.Buffer{}
for offSet < srcSize {
endIndex := offSet + once
if endIndex > srcSize {
endIndex = srcSize
}
// 加密一部分
bytesOnce, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src[offSet:endIndex])
if err != nil {
return nil, err
}
buffer.Write(bytesOnce)
offSet = endIndex
}
bytesEncrypt = buffer.Bytes()
return
}
7.DES加密
DES(Data Encryption)是1977年美國(guó)聯(lián)邦信息處理標(biāo)準(zhǔn)(FIPS)中所采用的一種對(duì)稱密碼(FIPS46-3)。隨著計(jì)算機(jī)的進(jìn)步,DES已經(jīng)能夠被暴力破解,1997年的DES Challenge I 中用了96天破譯密鑰,1998年的DES Challenge II-1中用了41天,1998年的DES Challenge II-2中用了56小時(shí),1999年的DES Challenge III 中只用了22小時(shí)15分鐘。
DES是一種將64比特的明文加密成64比特的密文的對(duì)稱密碼算法,它的密鑰的長(zhǎng)度是56比特。盡管從規(guī)格上來(lái)說(shuō),DES的密鑰長(zhǎng)度是64比特,但由于每隔7比特會(huì)設(shè)置一個(gè)用于錯(cuò)誤檢查的比特,因此實(shí)質(zhì)上其密鑰長(zhǎng)度是56比特。
DES 是以64比特的明文(比特序列)為一個(gè)單位來(lái)進(jìn)行加密的,這個(gè)64比特的單位稱為分組 ,一般來(lái)說(shuō),以分組為單位進(jìn)行處理的密碼算法稱為分組密碼,DES就是分組密碼的一種。
DES每次只能加密64比特的數(shù)據(jù),如果要加密的明文比較長(zhǎng),就需要對(duì)DES加密進(jìn)行迭代(反復(fù)),而迭代的具體方式就稱為模式。
DES 內(nèi)部實(shí)現(xiàn)理論:在 des 中各個(gè)步驟稱為輪,整個(gè)加密過(guò)程進(jìn)行16輪循環(huán)。
內(nèi)置庫(kù)完成
加密模式采用ECB、填充方式采用pkcs5padding、密碼使用"12345678",輸出時(shí)經(jīng)hex編碼。自己可以通過(guò)一些在線測(cè)試工具進(jìn)行測(cè)試,看結(jié)果是否一致。
package main
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/hex"
"fmt"
)
func main() {
data := []byte("hello world")
key := []byte("12345678")
iv := []byte("43218765")
result, err := DesCBCEncrypt(data, key, iv)
if err != nil {
fmt.Println(err)
}
b := hex.EncodeToString(result)
fmt.Println(b)
}
func DesCBCEncrypt(data, key, iv []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}
data = pkcs5Padding(data, block.BlockSize())
cryptText := make([]byte, len(data))
blockMode := cipher.NewCBCEncrypter(block, iv)
blockMode.CryptBlocks(cryptText, data)
return cryptText, nil
}
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
使用第三方庫(kù)
package main
import (
"fmt"
"github.com/marspere/goencrypt"
)
func main() {
// key為12345678
// iv為空
// 采用ECB分組模式
// 采用pkcs5padding填充模式
// 輸出結(jié)果使用base64進(jìn)行加密
cipher := goencrypt.NewDESCipher([]byte("12345678"), []byte(""), goencrypt.ECBMode, goencrypt.Pkcs7, goencrypt.PrintBase64)
cipherText, err := cipher.DESEncrypt([]byte("hello world"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(cipherText)
}
8.3DES加密算法
3DES(或稱為Triple DES)是三重?cái)?shù)據(jù)加密算法(TDEA,Triple Data Encryption Algorithm)塊密碼的通稱。它相當(dāng)于是對(duì)每個(gè)數(shù)據(jù)塊應(yīng)用三次DES加密算法。
由于計(jì)算機(jī)運(yùn)算能力的增強(qiáng),原版DES密碼的密鑰長(zhǎng)度變得容易被暴力破解;3DES即是設(shè)計(jì)用來(lái)提供一種相對(duì)簡(jiǎn)單的方法,即通過(guò)增加DES的密鑰長(zhǎng)度來(lái)避免類似的攻擊,而不是設(shè)計(jì)一種全新的塊密碼算法。
還有一個(gè)庫(kù) 非常NB
ECB模式下的3DES算法加解密信息,golang默認(rèn)只提供CBC模式
這邊有g(shù)olang的加密庫(kù),非常厲害
https://github.com/forgoer/openssl
安裝:
go get github.com/thinkoner/openssl
代碼如下:
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/forgoer/openssl"
)
func main() {
//定義密鑰,必須是24byte
key := []byte("123456789012345678901234")
fmt.Println("密鑰:", key, hex.EncodeToString(key))
//定義明文
src := []byte("0102030109000000030000000F8898E37A7F8F3D742006111118080000FACE05")
//3DES-ECB加密
encodeData, _ := openssl.Des3ECBEncrypt(src, key, openssl.ZEROS_PADDING)
encryptBaseData := base64.StdEncoding.EncodeToString(encodeData)
fmt.Println("加密后Base64:", encryptBaseData)
fmt.Println("加密后Hex:", hex.EncodeToString(encodeData))
//3DES-ECB解密
decodeBaseData, _ := base64.StdEncoding.DecodeString(encryptBaseData)
decodeData, _ := openssl.Des3ECBDecrypt(decodeBaseData, key, openssl.ZEROS_PADDING)
fmt.Println("解密后:", hex.EncodeToString(decodeData))
}
包括 Des的加密解密
以下只舉一個(gè)例子
srcData := "L0j+JvbeVM0svSpjIwXdE7yTu78wiEszCmW8rwjXY3vrx2nEaUeJ/Rw/c/IRdlxIH+/ro4pykx6ESOkGU1YwM8ddEuuoTg5uPsqQ9/SuNds="
key := []byte("Ctpsp@884*"[:8])
//3DES-ECB解密
decodeBaseData, _ := base64.StdEncoding.DecodeString(srcData)
decodeData, _ := openssl.DesECBDecrypt(decodeBaseData, key, openssl.PKCS5_PADDING)
fmt.Println("解密后:", string(decodeData))
源文件: https://github.com/hybpjx/InverseAlgorithm
以上就是Golang中常見(jiàn)加密算法的總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Golang加密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解在Go語(yǔ)言中如何實(shí)現(xiàn)枚舉類型
枚舉類型是一種常用的數(shù)據(jù)類型,用于表示一組有限的、預(yù)定義的、具名的常量值。而在?Go?語(yǔ)言里是沒(méi)有內(nèi)置枚舉類型的,所以本文將介紹如何實(shí)現(xiàn)?“枚舉類型”,需要的可以參考一下2023-04-04
Go語(yǔ)言使用templ實(shí)現(xiàn)編寫HTML用戶界面
templ是一個(gè)在 Go 中編寫 HTML 用戶界面的語(yǔ)言,使用 templ,我們可以創(chuàng)建可呈現(xiàn) HTML 片段的組件,下面就跟隨小編一起了解一下具體的實(shí)現(xiàn)方法吧2023-12-12
從入門到精通:Go語(yǔ)言XML數(shù)據(jù)解析指南
Go語(yǔ)言的XML包提供了強(qiáng)大的數(shù)據(jù)解析功能,讓你輕松處理各種XML格式的數(shù)據(jù),這個(gè)指南將帶你深入了解如何使用Go語(yǔ)言的XML包,快速上手XML數(shù)據(jù)解析,準(zhǔn)備好開啟XML解析之旅了嗎?Let's?Go!2024-03-03
Go語(yǔ)言基礎(chǔ)知識(shí)點(diǎn)介紹
在本篇文章里小編給大家整理的是一篇關(guān)于Go語(yǔ)言基礎(chǔ)知識(shí)點(diǎn)介紹內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-07-07

