golangsha1解码_golang常见加密解密

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值