aes.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package aes
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. )
  7. const (
  8. Zero string = "Zero"
  9. PKCS5 string = "PKCS5"
  10. PKCS7 string = "PKCS7"
  11. )
  12. // plantText: plant text
  13. // key: size 16,24, 32 match AES-128, AES-192, AES-256
  14. // padding: one of Zero,PKCS5,PKCS7,default PKCS5
  15. func CBCEncrypt(plantText, key []byte, padding string) ([]byte, error) {
  16. block, err := aes.NewCipher(key)
  17. if err != nil {
  18. return nil, err
  19. }
  20. switch padding {
  21. case Zero:
  22. plantText = ZeroPadding(plantText, block.BlockSize())
  23. case PKCS7:
  24. plantText = PKCS7Padding(plantText, block.BlockSize())
  25. default:
  26. plantText = PKCS5Padding(plantText, block.BlockSize())
  27. }
  28. blockModel := cipher.NewCBCEncrypter(block, key)
  29. ciphertext := make([]byte, len(plantText))
  30. blockModel.CryptBlocks(ciphertext, plantText)
  31. return ciphertext, nil
  32. }
  33. // cipherText: cipher text
  34. // key: size 16,24, 32 match AES-128, AES-192, AES-256
  35. // unpadding: one of Zero,PKCS5,PKCS7,default PKCS5
  36. func CBCDecrypt(ciphertext, key []byte, unpadding string) ([]byte, error) {
  37. keyBytes := []byte(key)
  38. block, err := aes.NewCipher(keyBytes)
  39. if err != nil {
  40. return nil, err
  41. }
  42. blockModel := cipher.NewCBCDecrypter(block, keyBytes)
  43. plantText := make([]byte, len(ciphertext))
  44. blockModel.CryptBlocks(plantText, ciphertext)
  45. // fmt.Println(plantText)
  46. switch unpadding {
  47. case Zero:
  48. plantText = ZeroUnPadding(plantText)
  49. case PKCS7:
  50. plantText = PKCS7UnPadding(plantText)
  51. default:
  52. plantText = PKCS5UnPadding(plantText)
  53. }
  54. return plantText, nil
  55. }
  56. func CBCDecryptNoPadding(ciphertext, key []byte) ([]byte, error) {
  57. keyBytes := key
  58. block, err := aes.NewCipher(keyBytes)
  59. if err != nil {
  60. return nil, err
  61. }
  62. blockModel := cipher.NewCBCDecrypter(block, keyBytes)
  63. plantText := make([]byte, len(ciphertext))
  64. blockModel.CryptBlocks(plantText, ciphertext)
  65. return plantText, nil
  66. }
  67. func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  68. padding := blockSize - len(ciphertext)%blockSize
  69. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  70. return append(ciphertext, padtext...)
  71. }
  72. func PKCS7UnPadding(plantText []byte) []byte {
  73. length := len(plantText)
  74. unpadding := int(plantText[length-1])
  75. return plantText[:(length - unpadding)]
  76. }
  77. func ZeroPadding(ciphertext []byte, blockSize int) []byte {
  78. padding := blockSize - len(ciphertext)%blockSize
  79. padtext := bytes.Repeat([]byte{0}, padding)
  80. return append(ciphertext, padtext...)
  81. }
  82. func ZeroUnPadding(origData []byte) []byte {
  83. length := len(origData)
  84. unpadding := int(origData[length-1])
  85. return origData[:(length - unpadding)]
  86. }
  87. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  88. padding := blockSize - len(ciphertext)%blockSize
  89. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  90. return append(ciphertext, padtext...)
  91. }
  92. func PKCS5UnPadding(origData []byte) []byte {
  93. length := len(origData)
  94. unpadding := int(origData[length-1])
  95. return origData[:(length - unpadding)]
  96. }