@@ -5,108 +5,78 @@ import (
55 "crypto/aes"
66 "crypto/cipher"
77 "encoding/base64"
8+ "fmt"
9+ "strings"
810
911 "github.com/juju/errors"
1012)
1113
12- //Decrypt 解密.
13- func Decrypt (crypted string , key []byte ) ([]byte , error ) {
14- raw , err := base64 .StdEncoding .DecodeString (crypted )
15- if err != nil {
16- return nil , errors .Trace (err )
14+ func byteKey (key string ) []byte {
15+ for i := 16 ; i < 33 ; i += 8 {
16+ key += strings .Repeat ("\x00 " , i - len (key ))
17+ if len (key ) == i {
18+ break
19+ }
1720 }
21+ return []byte (key )
22+ }
1823
19- block , err := aes .NewCipher (key )
24+ // Encrypt aes 加密.
25+ func Encrypt (encodeStr string , key string ) (string , error ) {
26+ encodeBytes := []byte (encodeStr )
27+ encodeKey := byteKey (key )
28+ block , err := aes .NewCipher (encodeKey )
2029 if err != nil {
21- return nil , errors .Trace (err )
30+ return "" , errors .Trace (err )
2231 }
32+ blockSize := block .BlockSize ()
33+ encodeBytes = pkcs5Padding (encodeBytes , blockSize )
34+ blockMode := cipher .NewCBCEncrypter (block , encodeKey [:blockSize ])
35+ crypted := make ([]byte , len (encodeBytes ))
36+ blockMode .CryptBlocks (crypted , encodeBytes )
2337
24- blockMode := NewECBDecrypter (block )
25- data := make ([]byte , len (raw ))
26- blockMode .CryptBlocks (data , raw )
27- data = pkcs5UnPadding (data )
28-
29- return data , nil
38+ return base64 .URLEncoding .EncodeToString (crypted ), nil
3039}
3140
32- //Encrypt 加密.
33- func Encrypt (src string , key []byte ) (string , error ) {
34- block , err := aes .NewCipher (key )
41+ // Decrypt aes 解密.
42+ func Decrypt (decodeStr string , key string ) (string , error ) {
43+ decodeKey := byteKey (key )
44+ decodeBytes , err := base64 .URLEncoding .DecodeString (decodeStr )
3545 if err != nil {
36- return "" , err
46+ return "" , errors .Trace (err )
47+ }
48+ block , err := aes .NewCipher (decodeKey )
49+ if err != nil {
50+ return "" , errors .Trace (err )
51+ }
52+ blockSize := block .BlockSize ()
53+ blockMode := cipher .NewCBCDecrypter (block , decodeKey [:blockSize ])
54+ origData := make ([]byte , len (decodeBytes ))
55+ fmt .Printf ("ori:%d, dec:%d\n " , len (origData ), len (decodeBytes ))
56+ blockMode .CryptBlocks (origData , decodeBytes )
57+ origData , err = pkcs5UnPadding (origData )
58+ if err != nil {
59+ return "" , errors .Trace (err )
3760 }
3861
39- ecb := NewECBEncrypter (block )
40- content := pkcs5Padding ([]byte (src ), block .BlockSize ())
41- buf := make ([]byte , len (content ))
42- ecb .CryptBlocks (buf , content )
43-
44- return base64 .StdEncoding .EncodeToString (buf ), nil
62+ return string (origData ), nil
4563}
4664
65+ // pkcs5Padding pkcs5 添加数据.
4766func pkcs5Padding (ciphertext []byte , blockSize int ) []byte {
4867 padding := blockSize - len (ciphertext )% blockSize
4968 padtext := bytes .Repeat ([]byte {byte (padding )}, padding )
5069 return append (ciphertext , padtext ... )
5170}
5271
53- func pkcs5UnPadding (origData []byte ) []byte {
72+ // pkcs5UnPadding pkcs5 删除数据.
73+ func pkcs5UnPadding (origData []byte ) ([]byte , error ) {
5474 length := len (origData )
55- // 去掉最后一个字节 unpadding 次
56- unpadding := int (origData [length - 1 ])
57- return origData [:(length - unpadding )]
58- }
59-
60- type ecb struct {
61- b cipher.Block
62- blockSize int
63- }
64-
65- func newECB (b cipher.Block ) * ecb {
66- return & ecb {
67- b : b ,
68- blockSize : b .BlockSize (),
69- }
70- }
7175
72- type ecbEncrypter ecb
73-
74- // NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.
75- func NewECBEncrypter (b cipher.Block ) cipher.BlockMode {
76- return (* ecbEncrypter )(newECB (b ))
77- }
78- func (x * ecbEncrypter ) BlockSize () int { return x .blockSize }
79- func (x * ecbEncrypter ) CryptBlocks (dst , src []byte ) {
80- if len (src )% x .blockSize != 0 {
81- panic ("crypto/cipher: input not full blocks" )
76+ if length <= 0 {
77+ return nil , errors .New ("pkcs5Padding len(origData) <= 0 error" )
8278 }
83- if len (dst ) < len (src ) {
84- panic ("crypto/cipher: output smaller than input" )
85- }
86- for len (src ) > 0 {
87- x .b .Encrypt (dst , src [:x .blockSize ])
88- src = src [x .blockSize :]
89- dst = dst [x .blockSize :]
90- }
91- }
9279
93- type ecbDecrypter ecb
94-
95- // NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.
96- func NewECBDecrypter (b cipher.Block ) cipher.BlockMode {
97- return (* ecbDecrypter )(newECB (b ))
98- }
99- func (x * ecbDecrypter ) BlockSize () int { return x .blockSize }
100- func (x * ecbDecrypter ) CryptBlocks (dst , src []byte ) {
101- if len (src )% x .blockSize != 0 {
102- panic ("crypto/cipher: input not full blocks" )
103- }
104- if len (dst ) < len (src ) {
105- panic ("crypto/cipher: output smaller than input" )
106- }
107- for len (src ) > 0 {
108- x .b .Decrypt (dst , src [:x .blockSize ])
109- src = src [x .blockSize :]
110- dst = dst [x .blockSize :]
111- }
80+ unpadding := int (origData [length - 1 ])
81+ return origData [:(length - unpadding )], nil
11282}
0 commit comments