-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecb.go
41 lines (35 loc) · 914 Bytes
/
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
package aes
import (
"crypto/aes"
)
/*
File name : ecb.go
Author : miaoyc
Create date : 2022/8/31 18:19
Update date : 2024/7/11 14:09
Description : ecb加密
*/
// EcbEncrypt ecb加密
func EcbEncrypt(data, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
data = PKCS7Padding(data, block.BlockSize())
decrypted := make([]byte, len(data))
size := block.BlockSize()
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
block.Encrypt(decrypted[bs:be], data[bs:be])
}
return decrypted, nil
}
// EcbDecrypt ecb解密
func EcbDecrypt(data, key []byte) ([]byte, error) {
block, _ := aes.NewCipher(key)
decrypted := make([]byte, len(data))
size := block.BlockSize()
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
block.Decrypt(decrypted[bs:be], data[bs:be])
}
return PKCS7UnPadding(decrypted), nil
}