Openssl encryption:OpenSSL庫的功能包裝,用於對稱和非對稱加密和解密。git
go get -u github.com/thinkoner/openssl
密鑰的長度能夠是16/24/32個字符(128/192/256位)。github
AES-ECB:加密
src := []byte("123456") key := []byte("1234567890123456") dst , _ := openssl.AesECBEncrypt(src, key, openssl.PKCS7_PADDING) fmt.Printf(base64.StdEncoding.EncodeToString(dst)) // yXVUkR45PFz0UfpbDB8/ew== dst , _ = openssl.AesECBDecrypt(dst, key, openssl.PKCS7_PADDING) fmt.Println(string(dst)) // 123456
AES-CBC:code
src := []byte("123456") key := []byte("1234567890123456") iv := []byte("1234567890123456") dst , _ := openssl.AesCBCEncrypt(src, key, iv, openssl.PKCS7_PADDING) fmt.Println(base64.StdEncoding.EncodeToString(dst)) // 1jdzWuniG6UMtoa3T6uNLA== dst , _ = openssl.AesCBCDecrypt(dst, key, iv, openssl.PKCS7_PADDING) fmt.Println(string(dst)) // 123456
密鑰的長度必須爲8個字符(64位)。ssl
DES-ECB:get
openssl.DesECBEncrypt(src, key, openssl.PKCS7_PADDING) openssl.DesECBDecrypt(src, key, openssl.PKCS7_PADDING)
DES-CBC:openssl
openssl.DesCBCEncrypt(src, key, iv, openssl.PKCS7_PADDING) openssl.DesCBCDecrypt(src, key, iv, openssl.PKCS7_PADDING)
密鑰的長度必須爲24個字符(192位)。string
3DES-ECB:it
openssl.Des3ECBEncrypt(src, key, openssl.PKCS7_PADDING) openssl.Des3ECBDecrypt(src, key, openssl.PKCS7_PADDING)
3DES-CBC:io
openssl.Des3CBCEncrypt(src, key, iv, openssl.PKCS7_PADDING) openssl.Des3CBCDecrypt(src, key, iv, openssl.PKCS7_PADDING)