des.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package xorm
  2. import (
  3. "bytes"
  4. "crypto/cipher"
  5. "crypto/des"
  6. "encoding/base64"
  7. // "log"
  8. )
  9. type DesEncrypt struct {
  10. PubKey string
  11. }
  12. type TripleDesEncrypt struct {
  13. PubKey string
  14. }
  15. func (this *DesEncrypt) getKey() []byte {
  16. strKey := this.PubKey
  17. keyLen := len(strKey)
  18. if keyLen < 8 {
  19. rs := []rune(tempkey)
  20. strKey = strKey + string(rs[0:8-keyLen])
  21. }
  22. arrKey := []byte(strKey)
  23. return arrKey[:8]
  24. }
  25. func (this *TripleDesEncrypt) getKey() []byte {
  26. strKey := this.PubKey
  27. keyLen := len(strKey)
  28. if keyLen < 24 {
  29. rs := []rune(tempkey)
  30. strKey = strKey + string(rs[0:24-keyLen])
  31. }
  32. arrKey := []byte(strKey)
  33. return arrKey[:24]
  34. }
  35. func (this *DesEncrypt) Encrypt(strMesg string) ([]byte, error) {
  36. key := this.getKey()
  37. origData := []byte(strMesg)
  38. block, err := des.NewCipher(key)
  39. if err != nil {
  40. return nil, err
  41. }
  42. origData = PKCS5Padding(origData, block.BlockSize())
  43. blockMode := cipher.NewCBCEncrypter(block, key)
  44. crypted := make([]byte, len(origData))
  45. blockMode.CryptBlocks(crypted, origData)
  46. return crypted, nil
  47. }
  48. func (this *DesEncrypt) Decrypt(crypted []byte) (decrypted []byte, err error) {
  49. key := this.getKey()
  50. block, err := des.NewCipher(key)
  51. if err != nil {
  52. return nil, err
  53. }
  54. crypted, err = base64.StdEncoding.DecodeString(string(crypted))
  55. if err != nil {
  56. return nil, err
  57. }
  58. blockMode := cipher.NewCBCDecrypter(block, key)
  59. decrypted = make([]byte, len(crypted))
  60. blockMode.CryptBlocks(decrypted, crypted)
  61. decrypted = PKCS5UnPadding(decrypted)
  62. return decrypted, nil
  63. }
  64. // 3DES加密
  65. func (this *TripleDesEncrypt) Encrypt(strMesg string) ([]byte, error) {
  66. key := this.getKey()
  67. origData := []byte(strMesg)
  68. block, err := des.NewTripleDESCipher(key)
  69. if err != nil {
  70. return nil, err
  71. }
  72. origData = PKCS5Padding(origData, block.BlockSize())
  73. blockMode := cipher.NewCBCEncrypter(block, key[:8])
  74. crypted := make([]byte, len(origData))
  75. blockMode.CryptBlocks(crypted, origData)
  76. return crypted, nil
  77. }
  78. // 3DES解密
  79. func (this *TripleDesEncrypt) Decrypt(crypted []byte) ([]byte, error) {
  80. key := this.getKey()
  81. block, err := des.NewTripleDESCipher(key)
  82. if err != nil {
  83. return nil, err
  84. }
  85. crypted, err = base64.StdEncoding.DecodeString(string(crypted))
  86. if err != nil {
  87. return nil, err
  88. }
  89. blockMode := cipher.NewCBCDecrypter(block, key[:8])
  90. origData := make([]byte, len(crypted))
  91. blockMode.CryptBlocks(origData, crypted)
  92. origData = PKCS5UnPadding(origData)
  93. return origData, nil
  94. }
  95. func ZeroPadding(ciphertext []byte, blockSize int) []byte {
  96. padding := blockSize - len(ciphertext)%blockSize
  97. padtext := bytes.Repeat([]byte{0}, padding)
  98. return append(ciphertext, padtext...)
  99. }
  100. func ZeroUnPadding(origData []byte) []byte {
  101. return bytes.TrimRightFunc(origData, func(r rune) bool {
  102. return r == rune(0)
  103. })
  104. }
  105. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  106. padding := blockSize - len(ciphertext)%blockSize
  107. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  108. return append(ciphertext, padtext...)
  109. }
  110. func PKCS5UnPadding(origData []byte) []byte {
  111. length := len(origData)
  112. // 去掉最后一个字节 unpadding 次
  113. unpadding := int(origData[length-1])
  114. return origData[:(length - unpadding)]
  115. }