aes.go 677 B

12345678910111213141516171819202122232425
  1. package yu_crypto
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. )
  6. func AESCBCPKCS7En(src []byte, key []byte, iv []byte) []byte {
  7. block, _ := aes.NewCipher(key)
  8. ecb := cipher.NewCBCEncrypter(block, iv)
  9. content := make([]byte, 0, len(src)+block.BlockSize())
  10. content = append(content, src...)
  11. content = PKCS7Padding(content, block.BlockSize())
  12. crypted := make([]byte, len(content))
  13. ecb.CryptBlocks(crypted, content)
  14. return crypted
  15. }
  16. func AESCBCPKCS7De(src []byte, key []byte, iv []byte) []byte {
  17. block, _ := aes.NewCipher(key)
  18. ecb := cipher.NewCBCDecrypter(block, iv)
  19. decrypted := make([]byte, len(src))
  20. ecb.CryptBlocks(decrypted, src)
  21. return PKCS7Trimming(decrypted)
  22. }