crypto.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/des"
  7. "crypto/rand"
  8. "encoding/hex"
  9. "fmt"
  10. "io"
  11. )
  12. var JS_KEY = []byte("mianbaon")
  13. var GO_KEY = []byte("mirendemianbaona")
  14. // Go DES ECB加密
  15. func EncryptDES_ECB(src string, key string) string {
  16. keyByte := JS_KEY
  17. if len(key) == 8 {
  18. keyByte = []byte(key)
  19. }
  20. data := []byte(src)
  21. block, err := des.NewCipher(keyByte)
  22. if err != nil {
  23. panic(err)
  24. }
  25. bs := block.BlockSize()
  26. //对明文数据进行补码
  27. data = PKCS5Padding(data, bs)
  28. if len(data)%bs != 0 {
  29. panic("Need a multiple of the blocksize")
  30. }
  31. out := make([]byte, len(data))
  32. dst := out
  33. for len(data) > 0 {
  34. //对明文按照blocksize进行分块加密
  35. //必要时可以使用go关键字进行并行加密
  36. block.Encrypt(dst, data[:bs])
  37. data = data[bs:]
  38. dst = dst[bs:]
  39. }
  40. return fmt.Sprintf("%X", out)
  41. }
  42. // Go DES ECB解密
  43. func DecryptDES_ECB(src string, key string) string {
  44. data, err := hex.DecodeString(src)
  45. if err != nil {
  46. panic(err)
  47. }
  48. keyByte := JS_KEY
  49. if len(key) == 8 {
  50. keyByte = []byte(key)
  51. }
  52. block, err := des.NewCipher(keyByte)
  53. if err != nil {
  54. panic(err)
  55. }
  56. bs := block.BlockSize()
  57. if len(data)%bs != 0 {
  58. panic("crypto/cipher: input not full blocks")
  59. }
  60. out := make([]byte, len(data))
  61. dst := out
  62. for len(data) > 0 {
  63. block.Decrypt(dst, data[:bs])
  64. data = data[bs:]
  65. dst = dst[bs:]
  66. }
  67. out = PKCS5UnPadding(out)
  68. return string(out)
  69. }
  70. // 明文补码算法
  71. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  72. padding := blockSize - len(ciphertext)%blockSize
  73. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  74. return append(ciphertext, padtext...)
  75. }
  76. // 明文减码算法
  77. func PKCS5UnPadding(origData []byte) []byte {
  78. length := len(origData)
  79. unpadding := int(origData[length-1])
  80. return origData[:(length - unpadding)]
  81. }
  82. // 加密
  83. func Encrypt(plaintextStr string) (string, error) {
  84. // 将字符串形式的 plaintext 转换为字节切片
  85. plaintext := []byte(plaintextStr)
  86. // 创建一个新的 AES 密码块
  87. block, err := aes.NewCipher(GO_KEY)
  88. if err != nil {
  89. return "", err
  90. }
  91. // 创建一个新的 GCM 模式的 AEAD(Authenticated Encryption with Associated Data)
  92. aead, err := cipher.NewGCM(block)
  93. if err != nil {
  94. return "", err
  95. }
  96. // 生成一个随机的 nonce(在 GCM 模式中,nonce 应该是唯一的)
  97. nonce := make([]byte, aead.NonceSize())
  98. if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
  99. return "", err
  100. }
  101. // 加密数据并添加认证标签
  102. ciphered := aead.Seal(nil, nonce, plaintext, nil)
  103. // 将 nonce 和 ciphertext 连接起来
  104. encryptedData := append(nonce, ciphered...)
  105. // 将加密后的数据转换为十六进制字符串并返回
  106. return hex.EncodeToString(encryptedData), nil
  107. }
  108. // 解密
  109. func Decrypt(cipheredStr string) (string, error) {
  110. // 将字符串形式的 ciphered 转换为字节切片
  111. ciphered, err := hex.DecodeString(cipheredStr)
  112. if err != nil {
  113. return "", err
  114. }
  115. // 创建一个新的 AES 密码块
  116. block, err := aes.NewCipher(GO_KEY)
  117. if err != nil {
  118. return "", err
  119. }
  120. // 创建一个新的 GCM 模式的 AEAD
  121. aead, err := cipher.NewGCM(block)
  122. if err != nil {
  123. return "", err
  124. }
  125. // 从 ciphered 中分离出 nonce 和实际的加密数据
  126. nonceSize := aead.NonceSize()
  127. nonce, ciphered := ciphered[:nonceSize], ciphered[nonceSize:]
  128. // 解密数据并验证认证标签
  129. plaintext, err := aead.Open(nil, nonce, ciphered, nil)
  130. if err != nil {
  131. return "", err
  132. }
  133. // 将解密后的字节切片转换为字符串并返回
  134. return string(plaintext), nil
  135. }