encrypt.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package encrypt
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/md5"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "math/rand"
  9. "strings"
  10. "time"
  11. )
  12. var random *rand.Rand
  13. var interPwd = "24a1d6ff39d8"
  14. func init() {
  15. random = rand.New(rand.NewSource(time.Now().UnixNano()))
  16. }
  17. //func PKCS7UnPadding(origData []byte) []byte {
  18. // length := len(origData)
  19. // unpadding := int(origData[length-1])
  20. // return origData[:(length - unpadding)]
  21. //}
  22. //
  23. //func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
  24. // padding := blockSize - len(ciphertext)%blockSize
  25. // padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  26. // return append(ciphertext, padtext...)
  27. //}
  28. func AesEncryptTS(origData, key []byte, iv []byte) ([]byte, error) {
  29. block, err := aes.NewCipher(key)
  30. if err != nil {
  31. return nil, err
  32. }
  33. blockSize := block.BlockSize()
  34. origData = PKCS7Padding(origData, blockSize)
  35. blockMode := cipher.NewCBCEncrypter(block, iv[:blockSize])
  36. crypted := make([]byte, len(origData))
  37. blockMode.CryptBlocks(crypted, origData)
  38. return crypted, nil
  39. }
  40. //aes cbc pkcs7
  41. func AesEncrypt(origData, key []byte) ([]byte, error) {
  42. block, err := aes.NewCipher(key)
  43. if err != nil {
  44. return nil, err
  45. }
  46. blockSize := block.BlockSize()
  47. origData = PKCS7Padding(origData, blockSize)
  48. blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
  49. crypted := make([]byte, len(origData))
  50. blockMode.CryptBlocks(crypted, origData)
  51. return crypted, nil
  52. }
  53. func AesECBPKCS5Encrypt(origData, key []byte) ([]byte, error) {
  54. block, err := aes.NewCipher(key)
  55. if err != nil {
  56. return nil, err
  57. }
  58. blockMode := NewECBEncrypter(block)
  59. origData = PKCS5Padding(origData, block.BlockSize())
  60. crypted := make([]byte, len(origData))
  61. blockMode.CryptBlocks(crypted, origData)
  62. return crypted, nil
  63. }
  64. func AesDecryptTS(crypted, key []byte, iv []byte) ([]byte, error) {
  65. block, err := aes.NewCipher(key)
  66. if err != nil {
  67. return nil, err
  68. }
  69. blockSize := block.BlockSize()
  70. blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize])
  71. origData := make([]byte, len(crypted))
  72. blockMode.CryptBlocks(origData, crypted)
  73. origData = PKCS7UnPadding(origData)
  74. return origData, nil
  75. }
  76. func AesDecryptPKCS5(crypted, key []byte, iv []byte) ([]byte, error) {
  77. block, err := aes.NewCipher(key)
  78. if err != nil {
  79. return nil, err
  80. }
  81. //blockSize := block.BlockSize()
  82. blockMode := cipher.NewCBCDecrypter(block, iv)
  83. origData := make([]byte, len(crypted))
  84. blockMode.CryptBlocks(origData, crypted)
  85. origData = PKCS5UnPadding(origData)
  86. return origData, nil
  87. }
  88. func AesDecrypt(crypted, key []byte) ([]byte, error) {
  89. block, err := aes.NewCipher(key)
  90. if err != nil {
  91. return nil, err
  92. }
  93. blockSize := block.BlockSize()
  94. blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
  95. origData := make([]byte, len(crypted))
  96. blockMode.CryptBlocks(origData, crypted)
  97. origData = PKCS7UnPadding(origData)
  98. return origData, nil
  99. }
  100. func AesECBPKCS5Decrypt(crypted, key []byte) ([]byte, error) {
  101. block, err := aes.NewCipher(key)
  102. if err != nil {
  103. return nil, err
  104. }
  105. //blockSize := block.BlockSize()
  106. blockMode := NewECBDecrypter(block)
  107. origData := make([]byte, len(crypted))
  108. blockMode.CryptBlocks(origData, crypted)
  109. origData = PKCS5UnPadding(origData)
  110. return origData, nil
  111. }
  112. func RandSeq(n int) string {
  113. letters := []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
  114. b := make([]rune, n)
  115. for i := range b {
  116. b[i] = letters[rand.Intn(len(letters))]
  117. }
  118. return string(b)
  119. }
  120. func EncryptKey(oriKey []byte) (string, error) {
  121. txtStream := base64.StdEncoding.EncodeToString(oriKey)
  122. return encData(txtStream, interPwd)
  123. }
  124. func EncryptKeySimple(oriKey []byte) (string, error) {
  125. oriKey = []byte(string(oriKey) + "123456")
  126. txtStream := base64.StdEncoding.EncodeToString(oriKey)
  127. return txtStream, nil
  128. }
  129. func encData(txtStream string, secretKey string) (string, error) {
  130. password := secretKey
  131. lockstream := "GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456"
  132. //随机找一个数字,并从密锁串中找到一个密锁值
  133. lockLen := len(lockstream)
  134. lockCount := random.Int31n(int32(lockLen))
  135. randomLock := string([]rune(lockstream)[lockCount])
  136. password = getMD5Hash(password + randomLock)
  137. txtStream = base64.StdEncoding.EncodeToString([]byte(txtStream))
  138. tmpStream := ""
  139. j := 0
  140. k := 0
  141. for i := 0; i < len(txtStream); i++ {
  142. if k == len(password) {
  143. k = 0
  144. }
  145. findChar := string([]rune(txtStream)[i])
  146. j = (strings.Index(lockstream, findChar) + int(lockCount) + int(password[k])) % lockLen
  147. tmpStream += string([]rune(lockstream)[j])
  148. k++
  149. }
  150. return tmpStream + randomLock, nil
  151. }
  152. func getMD5Hash(text string) string {
  153. hash := md5.Sum([]byte(text))
  154. return hex.EncodeToString(hash[:])
  155. }