Go 加密解密算法小結(jié)
前言
加密解密在實(shí)際開發(fā)中應(yīng)用比較廣泛,常用加解密分為:“對(duì)稱式”、“非對(duì)稱式”和”數(shù)字簽名“。
對(duì)稱式:對(duì)稱加密(也叫私鑰加密)指加密和解密使用相同密鑰的加密算法。具體算法主要有DES算法,3DES算法,TDEA算法,Blowfish算法,RC5算法,IDEA算法。
非對(duì)稱加密(公鑰加密):指加密和解密使用不同密鑰的加密算法,也稱為公私鑰加密。具體算法主要有RSA、Elgamal、背包算法、Rabin、D-H、ECC(橢圓曲線加密算法)。
數(shù)字簽名:數(shù)字簽名是非對(duì)稱密鑰加密技術(shù)與數(shù)字摘要技術(shù)的應(yīng)用。主要算法有md5、hmac、sha1等。
以下介紹golang語言主要的加密解密算法實(shí)現(xiàn)。
md5
MD5信息摘要算法是一種被廣泛使用的密碼散列函數(shù),可以產(chǎn)生出一個(gè)128位(16進(jìn)制,32個(gè)字符)的散列值(hash value),用于確保信息傳輸完整一致。
func GetMd5String(s string) string { h := md5.New() h.Write([]byte(s)) return hex.EncodeToString(h.Sum(nil)) }
hmac
HMAC是密鑰相關(guān)的哈希運(yùn)算消息認(rèn)證碼(Hash-based Message Authentication Code)的縮寫,
它通過一個(gè)標(biāo)準(zhǔn)算法,在計(jì)算哈希的過程中,把key混入計(jì)算過程中。
和我們自定義的加salt算法不同,Hmac算法針對(duì)所有哈希算法都通用,無論是MD5還是SHA-1。采用Hmac替代我們自己的salt算法,可以使程序算法更標(biāo)準(zhǔ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(""))) } func HmacSha256(key, data string) string { hash:= hmac.New(sha256.New, []byte(key)) //創(chuàng)建對(duì)應(yīng)的sha256哈希加密算法 hash.Write([]byte(data)) return hex.EncodeToString(hash.Sum([]byte(""))) }
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(""))) }
AES
密碼學(xué)中的高級(jí)加密標(biāo)準(zhǔn)(Advanced Encryption Standard,AES),又稱Rijndael加密法,是美國聯(lián)邦政府采用的一種區(qū)塊加密標(biāo)準(zhǔn)。這個(gè)標(biāo)準(zhǔn)用來替代原先的DES(Data Encryption Standard),已經(jīng)被多方分析且廣為全世界所使用。AES中常見的有三種解決方案,分別為AES-128、AES-192和AES-256。如果采用真正的128位加密技術(shù)甚至256位加密技術(shù),蠻力攻擊要取得成功需要耗費(fèi)相當(dāng)長(zhǎng)的時(shí)間。
AES 有五種加密模式:
- 電碼本模式(Electronic Codebook Book (ECB))、
- 密碼分組鏈接模式(Cipher Block Chaining (CBC))、
- 計(jì)算器模式(Counter (CTR))、
- 密碼反饋模式(Cipher FeedBack (CFB))
- 輸出反饋模式(Output FeedBack (OFB))
ECB模式
出于安全考慮,golang默認(rèn)并不支持ECB模式。
package main import ( ?? ?"crypto/aes" ?? ?"fmt" ) func AESEncrypt(src []byte, key []byte) (encrypted []byte) { ?? ?cipher, _ := aes.NewCipher(generateKey(key)) ?? ?length := (len(src) + aes.BlockSize) / aes.BlockSize ?? ?plain := make([]byte, length*aes.BlockSize) ?? ?copy(plain, src) ?? ?pad := byte(len(plain) - len(src)) ?? ?for i := len(src); i < len(plain); i++ { ?? ??? ?plain[i] = pad ?? ?} ?? ?encrypted = make([]byte, len(plain)) ?? ?// 分組分塊加密 ?? ?for bs, be := 0, cipher.BlockSize(); bs <= len(src); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() { ?? ??? ?cipher.Encrypt(encrypted[bs:be], plain[bs:be]) ?? ?} ?? ?return encrypted } func AESDecrypt(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() ?{ ?? ?source:="hello world" ?? ?fmt.Println("原字符:",source) ?? ?//16byte密鑰 ?? ?key:="1443flfsaWfdas" ?? ?encryptCode:=AESEncrypt([]byte(source),[]byte(key)) ?? ?fmt.Println("密文:",string(encryptCode)) ?? ?decryptCode:=AESDecrypt(encryptCode,[]byte(key)) ?? ?fmt.Println("解密:",string(decryptCode)) }
CBC模式
package main import( ? ? "bytes" ? ? "crypto/aes" ? ? "fmt" ? ? "crypto/cipher" ? ? "encoding/base64" ) func main() { ? ? orig := "hello world" ? ? key := "0123456789012345" ? ? fmt.Println("原文:", orig) ? ? encryptCode := AesEncrypt(orig, key) ? ? fmt.Println("密文:" , encryptCode) ? ? decryptCode := AesDecrypt(encryptCode, key) ? ? fmt.Println("解密結(jié)果:", decryptCode) } func AesEncrypt(orig string, key string) string { ? ? // 轉(zhuǎn)成字節(jié)數(shù)組 ? ? origData := []byte(orig) ? ? k := []byte(key) ? ? // 分組秘鑰 ? ? // NewCipher該函數(shù)限制了輸入k的長(zhǎng)度必須為16, 24或者32 ? ? block, _ := aes.NewCipher(k) ? ? // 獲取秘鑰塊的長(zhǎng)度 ? ? blockSize := block.BlockSize() ? ? // 補(bǔ)全碼 ? ? origData = PKCS7Padding(origData, blockSize) ? ? // 加密模式 ? ? blockMode := cipher.NewCBCEncrypter(block, k[:blockSize]) ? ? // 創(chuàng)建數(shù)組 ? ? cryted := make([]byte, len(origData)) ? ? // 加密 ? ? blockMode.CryptBlocks(cryted, origData) ? ? return base64.StdEncoding.EncodeToString(cryted) } func AesDecrypt(cryted string, key string) string { ? ? // 轉(zhuǎn)成字節(jié)數(shù)組 ? ? crytedByte, _ := base64.StdEncoding.DecodeString(cryted) ? ? k := []byte(key) ? ? // 分組秘鑰 ? ? block, _ := aes.NewCipher(k) ? ? // 獲取秘鑰塊的長(zhǎng)度 ? ? blockSize := block.BlockSize() ? ? // 加密模式 ? ? blockMode := cipher.NewCBCDecrypter(block, k[:blockSize]) ? ? // 創(chuàng)建數(shù)組 ? ? orig := make([]byte, len(crytedByte)) ? ? // 解密 ? ? blockMode.CryptBlocks(orig, crytedByte) ? ? // 去補(bǔ)全碼 ? ? orig = PKCS7UnPadding(orig) ? ? return string(orig) } //補(bǔ)碼 //AES加密數(shù)據(jù)塊分組長(zhǎng)度必須為128bit(byte[16]),密鑰長(zhǎng)度可以是128bit(byte[16])、192bit(byte[24])、256bit(byte[32])中的任意一個(gè)。 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)] }
CRT模式
package main import ( ?? ?"bytes" ?? ?"crypto/aes" ?? ?"crypto/cipher" ?? ?"fmt" ) //加密 func aesCtrCrypt(plainText []byte, key []byte) ([]byte, error) { ?? ?//1. 創(chuàng)建cipher.Block接口 ?? ?block, err := aes.NewCipher(key) ?? ?if err != nil { ?? ??? ?return nil, err ?? ?} ?? ?//2. 創(chuàng)建分組模式,在crypto/cipher包中 ?? ?iv := bytes.Repeat([]byte("1"), block.BlockSize()) ?? ?stream := cipher.NewCTR(block, iv) ?? ?//3. 加密 ?? ?dst := make([]byte, len(plainText)) ?? ?stream.XORKeyStream(dst, plainText) ?? ?return dst, nil } func main() { ?? ?source:="hello world" ?? ?fmt.Println("原字符:",source) ?? ?key:="1443flfsaWfdasds" ?? ?encryptCode,_:=aesCtrCrypt([]byte(source),[]byte(key)) ?? ?fmt.Println("密文:",string(encryptCode)) ?? ?decryptCode,_:=aesCtrCrypt(encryptCode,[]byte(key)) ?? ?fmt.Println("解密:",string(decryptCode)) }
CFB模式
package main import ( ?? ?"crypto/aes" ?? ?"crypto/cipher" ?? ?"crypto/rand" ?? ?"encoding/hex" ?? ?"fmt" ?? ?"io" ) 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() { ?? ?source:="hello world" ?? ?fmt.Println("原字符:",source) ?? ?key:="ABCDEFGHIJKLMNO1"http://16位 ?? ?encryptCode:=AesEncryptCFB([]byte(source),[]byte(key)) ?? ?fmt.Println("密文:",hex.EncodeToString(encryptCode)) ?? ?decryptCode:=AesDecryptCFB(encryptCode,[]byte(key)) ?? ?fmt.Println("解密:",string(decryptCode)) }
OFB模式
package main import ( ?? ?"bytes" ?? ?"crypto/aes" ?? ?"crypto/cipher" ?? ?"crypto/rand" ?? ?"encoding/hex" ?? ?"fmt" ?? ?"io" ) func aesEncryptOFB( data[]byte,key []byte) ([]byte, error) { ?? ?data = PKCS7Padding(data, aes.BlockSize) ?? ?block, _ := aes.NewCipher([]byte(key)) ?? ?out := make([]byte, aes.BlockSize + len(data)) ?? ?iv := out[:aes.BlockSize] ?? ?if _, err := io.ReadFull(rand.Reader, iv); err != nil { ?? ??? ?return nil, err ?? ?} ?? ?stream := cipher.NewOFB(block, iv) ?? ?stream.XORKeyStream(out[aes.BlockSize:], data) ?? ?return out, nil } func aesDecryptOFB( data[]byte,key []byte) ([]byte, error) { ?? ?block, _ := aes.NewCipher([]byte(key)) ?? ?iv ?:= data[:aes.BlockSize] ?? ?data = data[aes.BlockSize:] ?? ?if len(data) % aes.BlockSize != 0 { ?? ??? ?return nil, fmt.Errorf("data is not a multiple of the block size") ?? ?} ?? ?out := make([]byte, len(data)) ?? ?mode := cipher.NewOFB(block, iv) ?? ?mode.XORKeyStream(out, data) ?? ?out= PKCS7UnPadding(out) ?? ?return out, nil } //補(bǔ)碼 //AES加密數(shù)據(jù)塊分組長(zhǎng)度必須為128bit(byte[16]),密鑰長(zhǎng)度可以是128bit(byte[16])、192bit(byte[24])、256bit(byte[32])中的任意一個(gè)。 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)] } func main() { ?? ?source:="hello world" ?? ?fmt.Println("原字符:",source) ?? ?key:="1111111111111111"http://16位 ?32位均可 ?? ?encryptCode,_:=aesEncryptOFB([]byte(source),[]byte(key)) ?? ?fmt.Println("密文:",hex.EncodeToString(encryptCode)) ?? ?decryptCode,_:=aesDecryptOFB(encryptCode,[]byte(key)) ?? ?fmt.Println("解密:",string(decryptCode)) }
RSA加密
首先使用openssl生成公私鑰
package main import ( ?? ?"crypto/rand" ?? ?"crypto/rsa" ?? ?"crypto/x509" ?? ?"encoding/base64" ?? ?"encoding/pem" ?? ?"errors" ?? ?"fmt" ) // 私鑰生成 //openssl genrsa -out rsa_private_key.pem 1024 var privateKey = []byte(` -----BEGIN RSA PRIVATE KEY----- MIICWwIBAAKBgQDcGsUIIAINHfRTdMmgGwLrjzfMNSrtgIf4EGsNaYwmC1GjF/bM h0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdTnCDPPZ7oV7p1B9Pud+6zPaco qDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Zy682X1+R1lRK8D+vmQIDAQAB AoGAeWAZvz1HZExca5k/hpbeqV+0+VtobMgwMs96+U53BpO/VRzl8Cu3CpNyb7HY 64L9YQ+J5QgpPhqkgIO0dMu/0RIXsmhvr2gcxmKObcqT3JQ6S4rjHTln49I2sYTz 7JEH4TcplKjSjHyq5MhHfA+CV2/AB2BO6G8limu7SheXuvECQQDwOpZrZDeTOOBk z1vercawd+J9ll/FZYttnrWYTI1sSF1sNfZ7dUXPyYPQFZ0LQ1bhZGmWBZ6a6wd9 R+PKlmJvAkEA6o32c/WEXxW2zeh18sOO4wqUiBYq3L3hFObhcsUAY8jfykQefW8q yPuuL02jLIajFWd0itjvIrzWnVmoUuXydwJAXGLrvllIVkIlah+lATprkypH3Gyc YFnxCTNkOzIVoXMjGp6WMFylgIfLPZdSUiaPnxby1FNM7987fh7Lp/m12QJAK9iL 2JNtwkSR3p305oOuAz0oFORn8MnB+KFMRaMT9pNHWk0vke0lB1sc7ZTKyvkEJW0o eQgic9DvIYzwDUcU8wJAIkKROzuzLi9AvLnLUrSdI6998lmeYO9x7pwZPukz3era zncjRK3pbVkv0KrKfczuJiRlZ7dUzVO0b6QJr8TRAA== -----END RSA PRIVATE KEY----- `) // 公鑰: 根據(jù)私鑰生成 //openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem var publicKey = []byte(` -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcGsUIIAINHfRTdMmgGwLrjzfM NSrtgIf4EGsNaYwmC1GjF/bMh0Mcm10oLhNrKNYCTTQVGGIxuc5heKd1gOzb7bdT nCDPPZ7oV7p1B9Pud+6zPacoqDz2M24vHFWYY2FbIIJh8fHhKcfXNXOLovdVBE7Z y682X1+R1lRK8D+vmQIDAQAB -----END PUBLIC KEY----- `) // 加密 func RsaEncrypt(origData []byte) ([]byte, error) { ?? ?//解密pem格式的公鑰 ?? ?block, _ := pem.Decode(publicKey) ?? ?if block == nil { ?? ??? ?return nil, errors.New("public key error") ?? ?} ?? ?// 解析公鑰 ?? ?pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) ?? ?if err != nil { ?? ??? ?return nil, err ?? ?} ?? ?// 類型斷言 ?? ?pub := pubInterface.(*rsa.PublicKey) ?? ?//加密 ?? ?return rsa.EncryptPKCS1v15(rand.Reader, pub, origData) } // 解密 func RsaDecrypt(ciphertext []byte) ([]byte, error) { ?? ?//解密 ?? ?block, _ := pem.Decode(privateKey) ?? ?if block == nil { ?? ??? ?return nil, errors.New("private key error!") ?? ?} ?? ?//解析PKCS1格式的私鑰 ?? ?priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) ?? ?if err != nil { ?? ??? ?return nil, err ?? ?} ?? ?// 解密 ?? ?return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext) } func main() { ?? ?data, _ := RsaEncrypt([]byte("hello world")) ?? ?fmt.Println(base64.StdEncoding.EncodeToString(data)) ?? ?origData, _ := RsaDecrypt(data) ?? ?fmt.Println(string(origData)) }
參考:
https://www.liaoxuefeng.com/wiki/1016959663602400/1183198304823296
https://studygolang.com/articles/15642?fr=sidebar
https://segmentfault.com/a/1190000004151272
到此這篇關(guān)于Go 加密解密算法小結(jié)的文章就介紹到這了,更多相關(guān)Go 加密解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang中為什么Response.Body需要被關(guān)閉詳解
這篇文章主要給大家介紹了關(guān)于golang中為什么Response.Body需要被關(guān)閉的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Go語言for-range函數(shù)使用技巧實(shí)例探究
這篇文章主要為大家介紹了Go語言for-range函數(shù)使用技巧實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01基于Go+OpenCV實(shí)現(xiàn)人臉識(shí)別功能的詳細(xì)示例
OpenCV是一個(gè)強(qiáng)大的計(jì)算機(jī)視覺庫,提供了豐富的圖像處理和計(jì)算機(jī)視覺算法,本文將向你介紹在Mac上安裝OpenCV的步驟,并演示如何使用Go的OpenCV綁定庫進(jìn)行人臉識(shí)別,需要的朋友可以參考下2023-07-07Golang標(biāo)準(zhǔn)庫container/list的用法圖文詳解
提到單向鏈表,大家應(yīng)該是比較熟悉的了,這篇文章主要為大家詳細(xì)介紹了Golang標(biāo)準(zhǔn)庫container/list的用法相關(guān)知識(shí),感興趣的小伙伴可以了解下2024-01-01