@@ -8,13 +8,13 @@ import (
88 "encoding/asn1"
99)
1010
11- // pbe 数据
11+ // pbe parameters
1212type pbeParam struct {
1313 Salt []byte
1414 IterationCount int
1515}
1616
17- // cbc 模式加密
17+ // Cipher Block CBC mode
1818type CipherBlockCBC struct {
1919 // 对称加密
2020 cipherFunc func (key []byte ) (cipher.Block , error )
@@ -34,7 +34,7 @@ type CipherBlockCBC struct {
3434 oid asn1.ObjectIdentifier
3535}
3636
37- // 值大小
37+ // Key Size
3838func (this CipherBlockCBC ) KeySize () int {
3939 return this .keySize
4040}
@@ -44,85 +44,103 @@ func (this CipherBlockCBC) OID() asn1.ObjectIdentifier {
4444 return this .oid
4545}
4646
47- // 加密
47+ // with saltSize
48+ func (this CipherBlockCBC ) WithSaltSize (saltSize int ) CipherBlockCBC {
49+ this .saltSize = saltSize
50+
51+ return this
52+ }
53+
54+ // Encrypt data
4855func (this CipherBlockCBC ) Encrypt (password , plaintext []byte ) ([]byte , []byte , error ) {
49- // 加密数据补码
56+ encrypted , salt , iterationCount , err := this .encrypt (password , plaintext )
57+ if err != nil {
58+ return nil , nil , err
59+ }
60+
61+ // Marshal pbe param
62+ paramBytes , err := asn1 .Marshal (pbeParam {
63+ Salt : salt ,
64+ IterationCount : iterationCount ,
65+ })
66+ if err != nil {
67+ return nil , nil , err
68+ }
69+
70+ return encrypted , paramBytes , nil
71+ }
72+
73+ // Decrypt data
74+ func (this CipherBlockCBC ) Decrypt (password , params , ciphertext []byte ) ([]byte , error ) {
75+ var param pbeParam
76+ if _ , err := asn1 .Unmarshal (params , & param ); err != nil {
77+ return nil , errors .New ("go-cryptobin/jceks: invalid PBE parameters" )
78+ }
79+
80+ return this .decrypt (password , param .Salt , param .IterationCount , ciphertext )
81+ }
82+
83+ func (this CipherBlockCBC ) encrypt (password , plaintext []byte ) (encrypted , salt []byte , iterationCount int , err error ) {
84+ // pkcs7 padding
5085 plaintext = pkcs7Padding (plaintext , this .blockSize )
5186
52- salt , err : = genRandom (this .saltSize )
87+ salt , err = genRandom (this .saltSize )
5388 if err != nil {
54- return nil , nil , errors .New (err .Error () + " failed to generate salt" )
89+ err = errors .New ("go-cryptobin/jceks: failed to generate salt" )
90+ return
5591 }
5692
5793 key , iv := this .derivedKeyFunc (string (password ), string (salt ), this .iterationCount , this .keySize , this .blockSize , this .hashFunc )
5894 if key == nil && iv == nil {
59- return nil , nil , fmt .Errorf ("unexpected salt length: %d" , len (salt ))
95+ err = fmt .Errorf ("go-cryptobin/jceks: unexpected salt length: %d" , len (salt ))
96+ return
6097 }
6198
6299 block , err := this .cipherFunc (key )
63100 if err != nil {
64- return nil , nil , errors .New ("pkcs8:" + err .Error () + " failed to create cipher" )
101+ err = fmt .Errorf ("go-cryptobin/jceks: failed to create cipher: %s" , err .Error ())
102+ return
65103 }
66104
67105 // 需要保存的加密数据
68- encrypted : = make ([]byte , len (plaintext ))
106+ encrypted = make ([]byte , len (plaintext ))
69107
70108 enc := cipher .NewCBCEncrypter (block , iv )
71109 enc .CryptBlocks (encrypted , plaintext )
72110
73- // 返回数据
74- paramBytes , err := asn1 .Marshal (pbeParam {
75- Salt : salt ,
76- IterationCount : this .iterationCount ,
77- })
78- if err != nil {
79- return nil , nil , err
80- }
111+ iterationCount = this .iterationCount
81112
82- return encrypted , paramBytes , nil
113+ return
83114}
84115
85- // 解密
86- func (this CipherBlockCBC ) Decrypt (password , params , ciphertext []byte ) ([]byte , error ) {
87- var param pbeParam
88- if _ , err := asn1 .Unmarshal (params , & param ); err != nil {
89- return nil , errors .New ("pkcs8: invalid PBES2 parameters" )
90- }
91-
92- key , iv := this .derivedKeyFunc (string (password ), string (param .Salt ), param .IterationCount , this .keySize , this .blockSize , this .hashFunc )
116+ func (this CipherBlockCBC ) decrypt (password , salt []byte , iterationCount int , ciphertext []byte ) ([]byte , error ) {
117+ key , iv := this .derivedKeyFunc (string (password ), string (salt ), iterationCount , this .keySize , this .blockSize , this .hashFunc )
93118 if key == nil && iv == nil {
94- return nil , fmt .Errorf ("unexpected salt length: %d" , len (param . Salt ))
119+ return nil , fmt .Errorf ("go-cryptobin/jceks: unexpected salt length: %d" , len (salt ))
95120 }
96121
97122 block , err := this .cipherFunc (key )
98123 if err != nil {
99124 return nil , err
100125 }
101126
102- // 判断数据是否为填充数据
127+ // check ciphertext length
103128 blockSize := block .BlockSize ()
104129 dlen := len (ciphertext )
105130 if dlen == 0 || dlen % blockSize != 0 {
106- return nil , errors .New ("pkcs8 : invalid padding" )
131+ return nil , errors .New ("go-cryptobin/jceks : invalid padding" )
107132 }
108133
109134 plaintext := make ([]byte , len (ciphertext ))
110135
111136 mode := cipher .NewCBCDecrypter (block , iv )
112137 mode .CryptBlocks (plaintext , ciphertext )
113138
114- // 解析加密数据
139+ // pkcs7 UnPadding
115140 plaintext , err = pkcs7UnPadding (plaintext )
116141 if err != nil {
117142 return nil , err
118143 }
119144
120145 return plaintext , nil
121146}
122-
123- // 设置 saltSize
124- func (this CipherBlockCBC ) WithSaltSize (saltSize int ) CipherBlockCBC {
125- this .saltSize = saltSize
126-
127- return this
128- }
0 commit comments