ecb.go 3.2 KB

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