golang常见加密解密

oooxxxooo · · 1007 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

RSA,AES,BASE64,HMAC,MD5,SHA1,SHA256,SHA512

RSA:
// RsaEncrypt rsa encrypt
func RsaEncrypt(plainText, publicKey []byte) ([]byte, error) {
// decrypt the public key in pem format
if len(publicKey) == 0 {
publicKey = rsaPublicKey
}
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
// parsing the public key
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// types of assertions
pub := pubInterface.(*rsa.PublicKey)
// encode
return rsa.EncryptPKCS1v15(rand.Reader, pub, plainText)
}

// RsaDecrypt rsa decrypt
func RsaDecrypt(cipherText, privateKey []byte) ([]byte, error) {
// decode
if len(privateKey) == 0 {
privateKey = rsaPrivateKey
}
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error")
}
// parse private keys in PKCS1 format
private, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// decode
return rsa.DecryptPKCS1v15(rand.Reader, private, cipherText)
}

AES:
func AesCbcEncrypt(plainText, secretKey, iv []byte) ([]byte, error) {
block, err := aes.NewCipher(secretKey) // NewCipher The function limits the length of the input k to 16,24 or 32
if err != nil {
return nil, err
}
blockSize := block.BlockSize() // gets the length of the block of the secret key
plainText = PKCS7Padding(plainText, blockSize) // the completion code 补全码
blockMode := cipher.NewCBCEncrypter(block, iv) // encryption mode
crypt := make([]byte, len(plainText)) // create array
blockMode.CryptBlocks(crypt, plainText) // encode
return []byte(base64.StdEncoding.EncodeToString(crypt)), nil
}

func AesCbcDecrypt(cipherText, secretKey, iv []byte) ([]byte, error) {
cryptByte, err := base64.StdEncoding.DecodeString(string(cipherText)) // to byte array
if err != nil {
return nil, err
}
block, err := aes.NewCipher(secretKey) // group secret key
if err != nil {
return nil, err
}
blockMode := cipher.NewCBCDecrypter(block, iv) // encryption mode
plainText := make([]byte, len(cryptByte)) // create array
blockMode.CryptBlocks(plainText, cryptByte) // decode
return PKCS7UnPadding(plainText), nil // to completion 去补全码
}

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(plantText []byte) []byte {
length := len(plantText)
unPadding := int(plantText[length-1])
return plantText[:(length - unPadding)]
}

// AesEcbEncrypt aes ecb encrypt
func AesEcbEncrypt(src, key []byte) (encrypted []byte, err error) {
cipherText, err := aes.NewCipher(GenerateKey(key))
if err != nil {
return nil, err
}
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, cipherText.BlockSize(); bs <= len(src); bs, be = bs+cipherText.BlockSize(), be+cipherText.BlockSize() {
cipherText.Encrypt(encrypted[bs:be], plain[bs:be])
}
return encrypted, nil
}

func AesEcbDecrypt(encrypted, key []byte) (decrypted []byte, err error) {
cipherText, err := aes.NewCipher(GenerateKey(key))
if err != nil {
return nil, err
}
decrypted = make([]byte, len(encrypted))
for bs, be := 0, cipherText.BlockSize(); bs < len(encrypted); bs, be = bs+cipherText.BlockSize(), be+cipherText.BlockSize() {
cipherText.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim], nil
}

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
}

https://github.com/xooooooox/pwd


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:oooxxxooo

查看原文:golang常见加密解密

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1007 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传