encrypt_des.go 3.3 KB

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