Skip to content

Commit c543ef5

Browse files
committed
feat: auto pad the length of the key
1 parent f138978 commit c543ef5

File tree

4 files changed

+93
-11
lines changed

4 files changed

+93
-11
lines changed

aes.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package openssl
22

33
import (
4+
"bytes"
45
"crypto/aes"
6+
"crypto/cipher"
57
)
68

79
// AesECBEncrypt
810
func AesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
9-
block, err := aes.NewCipher(key)
11+
block, err := AesNewCipher(key)
1012
if err != nil {
1113
return nil, err
1214
}
@@ -15,7 +17,7 @@ func AesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1517

1618
// AesECBDecrypt
1719
func AesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
18-
block, err := aes.NewCipher(key)
20+
block, err := AesNewCipher(key)
1921
if err != nil {
2022
return nil, err
2123
}
@@ -25,7 +27,7 @@ func AesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2527

2628
// AesCBCEncrypt
2729
func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
28-
block, err := aes.NewCipher(key)
30+
block, err := AesNewCipher(key)
2931
if err != nil {
3032
return nil, err
3133
}
@@ -35,10 +37,37 @@ func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3537

3638
// AesCBCDecrypt
3739
func AesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
38-
block, err := aes.NewCipher(key)
40+
block, err := AesNewCipher(key)
3941
if err != nil {
4042
return nil, err
4143
}
4244

4345
return CBCDecrypt(block, src, iv, padding)
4446
}
47+
48+
// AesNewCipher creates and returns a new AES cipher.Block.
49+
// it will automatically pad the length of the key.
50+
func AesNewCipher(key []byte) (cipher.Block, error) {
51+
return aes.NewCipher(aesKeyPending(key))
52+
}
53+
54+
// aesKeyPending The length of the key can be 16/24/32 characters (128/192/256 bits)
55+
func aesKeyPending(key []byte) []byte {
56+
k := len(key)
57+
count := 0
58+
switch true {
59+
case k <= 16:
60+
count = 16 - k
61+
case k <= 24:
62+
count = 24 - k
63+
case k <= 32:
64+
count = 32 - k
65+
default:
66+
return key[:32]
67+
}
68+
if count == 0 {
69+
return key
70+
}
71+
72+
return append(key, bytes.Repeat([]byte{0}, count)...)
73+
}

aes_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package openssl
33
import (
44
"encoding/base64"
55
"github.com/stretchr/testify/assert"
6+
"reflect"
67
"testing"
78
)
89

@@ -94,3 +95,26 @@ func TestAesCBCDecrypt(t *testing.T) {
9495
t.Log(string(dst))
9596
assert.Equal(t, dst, []byte("123456"))
9697
}
98+
99+
func Test_aesKeyPending(t *testing.T) {
100+
type args struct {
101+
key []byte
102+
}
103+
tests := []struct {
104+
name string
105+
args args
106+
want []byte
107+
}{
108+
{name: "key < 16", args: args{key: []byte("12354678901234")}, want: append([]byte("12354678901234"), []byte{0, 0}...)},
109+
{name: "key < 24", args: args{key: []byte("1235467890123546789012")}, want: append([]byte("1235467890123546789012"), []byte{0, 0}...)},
110+
{name: "key < 32", args: args{key: []byte("123546789012354678901235467890")}, want: append([]byte("123546789012354678901235467890"), []byte{0, 0}...)},
111+
{name: "key > 32", args: args{key: []byte("1235467890123546789012354678901234")}, want: []byte("12354678901235467890123546789012")},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
if got := aesKeyPending(tt.args.key); !reflect.DeepEqual(got, tt.want) {
116+
t.Errorf("aesKeyPending() = %v, want %v", got, tt.want)
117+
}
118+
})
119+
}
120+
}

cbc.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package openssl
22

33
import (
4+
"bytes"
45
"crypto/cipher"
5-
"errors"
66
)
77

88
// CBCEncrypt
@@ -13,7 +13,9 @@ func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err
1313
encryptData := make([]byte, len(src))
1414

1515
if len(iv) != block.BlockSize() {
16-
return nil, errors.New("CBCEncrypt: IV length must equal block size")
16+
// auto pad length to block size
17+
iv = cbcIVPending(iv, block.BlockSize())
18+
//return nil, errors.New("CBCEncrypt: IV length must equal block size")
1719
}
1820

1921
mode := cipher.NewCBCEncrypter(block, iv)
@@ -28,11 +30,25 @@ func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, err
2830
dst := make([]byte, len(src))
2931

3032
if len(iv) != block.BlockSize() {
31-
return nil, errors.New("CBCDecrypt: IV length must equal block size")
33+
// auto pad length to block size
34+
iv = cbcIVPending(iv, block.BlockSize())
35+
//return nil, errors.New("CBCDecrypt: IV length must equal block size")
3236
}
3337

3438
mode := cipher.NewCBCDecrypter(block, iv)
3539
mode.CryptBlocks(dst, src)
3640

3741
return UnPadding(padding, dst)
3842
}
43+
44+
// cbcIVPending auto pad length to block size
45+
func cbcIVPending(iv []byte, blockSize int)[]byte {
46+
k := len(iv)
47+
if k < blockSize{
48+
return append(iv, bytes.Repeat([]byte{0}, blockSize - k)...)
49+
}else if k > blockSize{
50+
return iv[0: blockSize]
51+
}
52+
53+
return iv
54+
}

des.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package openssl
22

33
import (
4+
"bytes"
5+
"crypto/cipher"
46
"crypto/des"
57
)
68

79
// DesECBEncrypt
810
func DesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
9-
block, err := des.NewCipher(key)
11+
block, err := DesNewCipher(key)
1012
if err != nil {
1113
return nil, err
1214
}
@@ -15,7 +17,7 @@ func DesECBEncrypt(src, key []byte, padding string) ([]byte, error) {
1517

1618
// DesECBDecrypt
1719
func DesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
18-
block, err := des.NewCipher(key)
20+
block, err := DesNewCipher(key)
1921
if err != nil {
2022
return nil, err
2123
}
@@ -25,7 +27,7 @@ func DesECBDecrypt(src, key []byte, padding string) ([]byte, error) {
2527

2628
// DesCBCEncrypt
2729
func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
28-
block, err := des.NewCipher(key)
30+
block, err := DesNewCipher(key)
2931
if err != nil {
3032
return nil, err
3133
}
@@ -35,10 +37,21 @@ func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error) {
3537

3638
// DesCBCDecrypt
3739
func DesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error) {
38-
block, err := des.NewCipher(key)
40+
block, err := DesNewCipher(key)
3941
if err != nil {
4042
return nil, err
4143
}
4244

4345
return CBCDecrypt(block, src, iv, padding)
4446
}
47+
48+
// DesNewCipher creates and returns a new DES cipher.Block.
49+
func DesNewCipher(key []byte) (cipher.Block, error) {
50+
if len(key) < 8 {
51+
key = append(key, bytes.Repeat([]byte{0}, 8-len(key))...)
52+
} else if len(key) > 8 {
53+
key = key[:8]
54+
}
55+
56+
return des.NewCipher(key)
57+
}

0 commit comments

Comments
 (0)