ecb.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package config
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "fmt"
  8. "strings"
  9. )
  10. func Base64URLDecode(data string) ([]byte, error) {
  11. var missing = (4 - len(data)%4) % 4
  12. data += strings.Repeat("=", missing)
  13. return base64.URLEncoding.DecodeString(data)
  14. }
  15. func Base64UrlSafeEncode(source []byte) string {
  16. // Base64 Url Safe is the same as Base64 but does not contain '/' and '+' (replaced by '_' and '-') and trailing '=' are removed.
  17. bytearr := base64.StdEncoding.EncodeToString(source)
  18. safeurl := strings.Replace(string(bytearr), "/", "_", -1)
  19. safeurl = strings.Replace(safeurl, "+", "-", -1)
  20. safeurl = strings.Replace(safeurl, "=", "", -1)
  21. return safeurl
  22. }
  23. func AesDecrypt(crypted, key []byte) ([]byte, error) {
  24. block, err := aes.NewCipher(key)
  25. if err != nil {
  26. return nil, err
  27. }
  28. blockMode := NewECBDecrypter(block)
  29. origData := make([]byte, len(crypted))
  30. blockMode.CryptBlocks(origData, crypted)
  31. origData = PKCS5UnPadding(origData)
  32. return origData, nil
  33. }
  34. func AesEncrypt(src, key string) ([]byte, error) {
  35. block, err := aes.NewCipher([]byte(key))
  36. if err != nil {
  37. return nil, err
  38. }
  39. ecb := NewECBEncrypter(block)
  40. content := []byte(src)
  41. content = PKCS5Padding(content, block.BlockSize())
  42. crypted := make([]byte, len(content))
  43. ecb.CryptBlocks(crypted, content)
  44. return crypted, nil
  45. }
  46. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  47. padding := blockSize - len(ciphertext)%blockSize
  48. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  49. return append(ciphertext, padtext...)
  50. }
  51. func PKCS5UnPadding(origData []byte) []byte {
  52. length := len(origData)
  53. unpadding := int(origData[length-1])
  54. return origData[:(length - unpadding)]
  55. }
  56. type ecb struct {
  57. b cipher.Block
  58. blockSize int
  59. }
  60. func newECB(b cipher.Block) *ecb {
  61. return &ecb{
  62. b: b,
  63. blockSize: b.BlockSize(),
  64. }
  65. }
  66. type ecbEncrypter ecb
  67. // NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.
  68. func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
  69. return (*ecbEncrypter)(newECB(b))
  70. }
  71. func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
  72. func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
  73. if len(src)%x.blockSize != 0 {
  74. fmt.Println("crypto/cipher: input not full blocks")
  75. return
  76. }
  77. if len(dst) < len(src) {
  78. fmt.Println("crypto/cipher: output smaller than input")
  79. return
  80. }
  81. for len(src) > 0 {
  82. x.b.Encrypt(dst, src[:x.blockSize])
  83. src = src[x.blockSize:]
  84. dst = dst[x.blockSize:]
  85. }
  86. }
  87. type ecbDecrypter ecb
  88. // NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.
  89. func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
  90. return (*ecbDecrypter)(newECB(b))
  91. }
  92. func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
  93. func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
  94. if len(src)%x.blockSize != 0 {
  95. fmt.Println("crypto/cipher: input not full blocks")
  96. return
  97. }
  98. if len(dst) < len(src) {
  99. fmt.Println("crypto/cipher: output smaller than input")
  100. return
  101. }
  102. for len(src) > 0 {
  103. x.b.Decrypt(dst, src[:x.blockSize])
  104. src = src[x.blockSize:]
  105. dst = dst[x.blockSize:]
  106. }
  107. }
  108. func Base64AESECBEncrypt(p, key string) (string, error) {
  109. c, err := AesEncrypt(p, key)
  110. if err != nil {
  111. return "", err
  112. }
  113. return base64.StdEncoding.EncodeToString(c), nil
  114. }