123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package encrypt
- import (
- "bytes"
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "encoding/base64"
- "encoding/hex"
- "io"
- )
- /*CBC加密 按照golang标准库的例子代码
- 不过里面没有填充的部分,所以补上,根据key来决定填充blocksize
- */
- //使用PKCS7进行填充,IOS也是7
- 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 ZeroPadding(ciphertext []byte, blockSize int) []byte {
- padding := blockSize - len(ciphertext)%blockSize
- padtext := bytes.Repeat([]byte{0}, padding)//用0去填充
- return append(ciphertext, padtext...)
- }
- //aes加密,填充模式由key决定,16位,24,32分别对应AES-128, AES-192, or AES-256.源码好像是写死16了
- func AesCBCEncrypt(rawData, key []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- //填充原文
- blockSize := block.BlockSize()
- rawData = pkcs7Padding(rawData, blockSize)
- //初始向量IV必须是唯一,但不需要保密
- cipherText := make([]byte, blockSize+len(rawData))
- //block大小 16
- iv := cipherText[:blockSize]
- if _, err := io.ReadFull(rand.Reader, iv); err != nil {
- panic(err)
- }
- //block大小和初始向量大小一定要一致
- mode := cipher.NewCBCEncrypter(block, iv)
- mode.CryptBlocks(cipherText[blockSize:], rawData)
- return cipherText, nil
- }
- func AesCBCDecrypt(encryptData, key []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- blockSize := block.BlockSize()
- if len(encryptData) < blockSize {
- panic("ciphertext too short")
- }
- // CBC mode always works in whole blocks.
- if len(encryptData)%blockSize != 0 {
- panic("ciphertext is not a multiple of the block size")
- }
- mode := cipher.NewCBCDecrypter(block, key)
- // CryptBlocks can work in-place if the two arguments are the same.
- mode.CryptBlocks(encryptData, encryptData)
- //解填充
- encryptData = ZeroPadding(encryptData, blockSize)
- return encryptData, nil
- }
- func Encrypt(rawData, key []byte) (string, error) {
- data, err := AesCBCEncrypt(rawData, key)
- if err != nil {
- return "", err
- }
- return base64.StdEncoding.EncodeToString(data), nil
- }
- func Decrypt(rawData string, key []byte) (string, error) {
- data, err := hex.DecodeString(rawData)
- if err != nil {
- return "", err
- }
- dnData, err := AesCBCDecrypt(data, key)
- if err != nil {
- return "", err
- }
- return string(dnData), nil
- }
|