-
Notifications
You must be signed in to change notification settings - Fork 43
/
aes_ecb.go
84 lines (81 loc) · 2.5 KB
/
aes_ecb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package codec
//func _AESECBEncryptWithPadding(key []byte, i interface{}, iv []byte, padding func(i []byte) []byte) ([]byte, error) {
// data := interfaceToBytes(i)
// block, err := aes.NewCipher(key)
// if err != nil {
// return nil, err
// }
//
// data = padding(data)
//
// encrypted := make([]byte, len(data))
// size := block.BlockSize()
// if iv == nil {
// iv = key[:size]
// }
//
// for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
// block.Encrypt(encrypted[bs:be], data[bs:be])
// }
// return encrypted, nil
//}
//
//func AESECBEncrypt(key []byte, i interface{}, iv []byte) ([]byte, error) {
// return _AESECBEncryptWithPadding(key, i, iv, PKCS7Padding)
//}
//
//var AESECBEncryptWithPKCS7Padding = AESECBEncrypt
//
//func AESECBEncryptWithZeroPadding(key []byte, i interface{}, iv []byte) ([]byte, error) {
// return _AESECBEncryptWithPadding(key, i, iv, func(i []byte) []byte {
// return ZeroPadding(i, aes.BlockSize)
// })
//}
//
//func _AESECBDecryptWithPadding(key []byte, i interface{}, iv []byte, padding func([]byte) []byte) ([]byte, error) {
// crypted := interfaceToBytes(i)
// block, err := aes.NewCipher(key)
// if err != nil {
// return nil, err
// }
//
// decrypted := make([]byte, len(crypted))
// size := block.BlockSize()
// if iv == nil {
// iv = key[:size]
// }
// if len(iv) < size {
// iv = padding(iv)
// } else if len(iv) > size {
// iv = iv[:size]
// }
//
// //if len(crypted)%block.BlockSize() != 0 {
// // panic("crypto/cipher: input not full blocks")
// //}
// //if len(decrypted) < len(crypted) {
// // panic("crypto/cipher: output smaller than input")
// //}
//
// for bs, be := 0, size; bs < len(crypted); bs, be = bs+size, be+size {
// block.Decrypt(decrypted[bs:be], crypted[bs:be])
// }
//
// decrypted = padding(decrypted)
// return decrypted, nil
//}
//
//func AESECBDecryptWithPKCS7Padding(key []byte, i interface{}, iv []byte) ([]byte, error) {
// return _AESECBDecryptWithPadding(key, i, iv, PKCS7UnPadding)
//}
//func AESECBDecryptWithZeroPadding(key []byte, i interface{}, iv []byte) ([]byte, error) {
// return _AESECBDecryptWithPadding(key, i, iv, func(i []byte) []byte {
// return ZeroUnPadding(i)
// })
//}
var AESECBDecrypt = AESDecryptECBWithPKCSPadding
var AESECBEncrypt = AESEncryptECBWithPKCSPadding
var AESEncryptECBWithPKCSPadding = AESEncFactory(PKCS5Padding, ECB)
var AESEncryptECBWithZeroPadding = AESEncFactory(ZeroPadding, ECB)
var AESDecryptECBWithPKCSPadding = AESDecFactory(PKCS5UnPadding, ECB)
var AESDecryptECBWithZeroPadding = AESDecFactory(ZeroUnPadding, ECB)