AES算法。

使用方式

import "github.com/gogf/gf/v2/crypto/gaes"

接口文档

https://pkg.go.dev/github.com/gogf/gf/v2/crypto/gaes


温馨提示:

如果待解密数据经过其它编码,则要先解码再解密,如base64.decode

反过来也一样

如果希望加密完的数据编码,则将结果编码即可,如base64.encode

  • No labels

7 Comments

  1. ```go

    func main() {
    // g.Server().Run()



    key := []byte("1234567890123456")
    res, _ := gaes.Encrypt([]byte("shit"), key)
    fmt.Println(string(res)) //直接转换它是这样的:�8P@�qJ?n��T�Z�

    //使用encoding/hex对它进行处理下感觉会更好些
    res2 := hex.EncodeToString(res)
    fmt.Println(res2) //a63850409c714a3f6ec2ef5415ec5adf

    res3, _ := hex.DecodeString(res2)
    fmt.Println(res3) //[166 56 80 64 156 113 74 63 110 194 239 84 21 236 90 223]

    }

    ```

    1. 原始函数是不会做这种额外处理的(保证一致性),都是按自己需求再做处理,你做hex,其它人可能做base64。

      1. 嗯嗯,多谢提醒,因为是新手有很多不懂的地方,所以就发出来抛砖引玉来了,哈哈(big grin)

  2. 你给的aes很鸡肋,没有处理补齐问题。 分享一段代码,供参考

    package crypto
    
    import (
    	"crypto/aes"
    	"crypto/cipher"
    	"errors"
    	//"fmt"
    )
    
    func AES_Encrypt_ECB(plain, key []byte) ([]byte, error) {
    	bytCipher := make([]byte, 0)
    
    	//fmt.Printf("plain len= %v\n", len(plain))
    	cipher, err := aes.NewCipher(key)
    	if err != nil {
    		return nil, err
    	}
    
    	blockSize := cipher.BlockSize()
    	if len(plain)%blockSize != 0 {
    		return nil, errors.New("data isn't block size count!")
    	}
    
    	//fmt.Printf("blockSize: %v\n", blockSize)
    	cipherLen := len(plain)
    	bytCipher = make([]byte, cipherLen)
    	for i := 0; i < cipherLen; i += blockSize {
    		//fmt.Printf("bytCipher[%d:%d], bytPlain[%d:%d]\n", i, i+blockSize, i, i+blockSize)
    		cipher.Encrypt(bytCipher[i:i+blockSize], plain[i:i+blockSize])
    	}
    
    	return bytCipher, nil
    }
    
    func AES_Decrypt_ECB(bytCipher, key []byte) ([]byte, error) {
    	bytPlain := make([]byte, 0)
    
    	cipher, err := aes.NewCipher(key)
    	if err != nil {
    		return nil, err
    	}
    
    	blockSize := cipher.BlockSize()
    	if len(bytCipher)%blockSize != 0 {
    		return nil, errors.New("data isn't block size interger count!")
    	}
    
    	cipherLen := len(bytCipher)
    	bytPlain = make([]byte, cipherLen)
    	for i := 0; i < cipherLen; i += blockSize {
    		cipher.Decrypt(bytPlain[i:i+blockSize], bytCipher[i:i+blockSize])
    	}
    
    	return bytPlain, nil
    }
    
    // ================== CBC ========================
    
    func AES_Encrypt_CBC(plantText, key []byte) ([]byte, error) {
    	block, err := aes.NewCipher(key) 
    	if err != nil {
    		return nil, err
    	}
    	plantText = Padding_PKCS7(plantText, block.BlockSize())
    
    	blockModel := cipher.NewCBCEncrypter(block, key)
    
    	ciphertext := make([]byte, len(plantText))
    
    	blockModel.CryptBlocks(ciphertext, plantText)
    	return ciphertext, nil
    }
    
    func AES_Decrypt_CBC(ciphertext, key []byte) ([]byte, error) {
    	keyBytes := []byte(key)
    	block, err := aes.NewCipher(keyBytes) //选择加密算法
    	if err != nil {
    		return nil, err
    	}
    
    	length := len(ciphertext)
    	if length%block.BlockSize() != 0 {
    		return nil, errors.New("data invalid")
    	}
    
    	blockModel := cipher.NewCBCDecrypter(block, keyBytes)
    	plantText := make([]byte, len(ciphertext))
    	blockModel.CryptBlocks(plantText, ciphertext)
    	plantText = UnPadding_PKCS7(plantText, block.BlockSize())
    	return plantText, nil
    }
    
    
  3. AES 使用的应当是PKCS7UnPadding,因为block size不可能为8,代码里面的PKCS5Unpadding其实也是PKCS7写法,并不是实际的PCKS5(block size == 8),规范中PKCS5是PKCS7的子集,在JAVA中使用PKCS5其实也就是在使用PKCS7,无人在乎5或7,但go不是java,基于对gf的信任,此处使用PKCS5可能对一些人造成误导(比如我,翻阅了很多资料才搞清楚),是否需要重命名函数?

    func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) 
      1. 留意一下pr的后续