aes128.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. )
  8. const (
  9. sKey = "dde4b1f8a9e6b814"
  10. ivParameter = "d690b1f8a957b834"
  11. )
  12. //加密
  13. func PswEncrypt(src, skey, ivParam string)(string){
  14. if skey == ""{
  15. skey = sKey
  16. }
  17. if ivParam == ""{
  18. ivParam = ivParameter
  19. }
  20. key := []byte(skey)
  21. iv := []byte(ivParam)
  22. result, err := Aes128Encrypt([]byte(src), key, iv)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return base64.RawStdEncoding.EncodeToString(result)
  27. }
  28. //解密
  29. func PswDecrypt(src, skey, ivParam string)(string) {
  30. if skey == ""{
  31. skey = sKey
  32. }
  33. if ivParam == ""{
  34. ivParam = ivParameter
  35. }
  36. key := []byte(skey)
  37. iv := []byte(ivParam)
  38. var result []byte
  39. var err error
  40. result,err=base64.RawStdEncoding.DecodeString(src)
  41. if err != nil {
  42. panic(err)
  43. }
  44. origData, err := Aes128Decrypt(result, key, iv)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return string(origData)
  49. }
  50. func Aes128Encrypt(origData, key []byte,IV []byte) ([]byte, error) {
  51. if key == nil || len(key) != 16 {
  52. return nil, nil
  53. }
  54. if IV != nil && len(IV) != 16 {
  55. return nil, nil
  56. }
  57. block, err := aes.NewCipher(key)
  58. if err != nil {
  59. return nil, err
  60. }
  61. blockSize := block.BlockSize()
  62. origData = PKCS5Padding(origData, blockSize)
  63. blockMode := cipher.NewCBCEncrypter(block, IV[:blockSize])
  64. crypted := make([]byte, len(origData))
  65. // 根据CryptBlocks方法的说明,如下方式初始化crypted也可以
  66. blockMode.CryptBlocks(crypted, origData)
  67. return crypted, nil
  68. }
  69. func Aes128Decrypt(crypted, key []byte,IV []byte) ([]byte, error) {
  70. if key == nil || len(key) != 16 {
  71. return nil, nil
  72. }
  73. if IV != nil && len(IV) != 16 {
  74. return nil, nil
  75. }
  76. block, err := aes.NewCipher(key)
  77. if err != nil {
  78. return nil, err
  79. }
  80. blockSize := block.BlockSize()
  81. blockMode := cipher.NewCBCDecrypter(block,IV[:blockSize])
  82. origData := make([]byte, len(crypted))
  83. blockMode.CryptBlocks(origData, crypted)
  84. origData = PKCS5UnPadding(origData)
  85. return origData, nil
  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 次
  95. unpadding := int(origData[length-1])
  96. return origData[:(length - unpadding)]
  97. }