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
}
有疑问加站长微信联系(非本文作者)