-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc7.go
61 lines (51 loc) · 1.62 KB
/
c7.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
package main
import (
"crypto/aes"
"fmt"
)
// decryptAesEcb decrypts a cipher text encrypted using AES-128 in ECB mode with
// the given key.
func decryptAesEcb(cipherText, key []byte) ([]byte, error) {
if len(cipherText)%len(key) != 0 {
const formatStr = "cipher text's length (%d) is not a multiple of the decryption key's length (%d)"
return nil, fmt.Errorf(formatStr, len(cipherText), len(key))
}
decrypter, err := aesDecrypter(key)
if err != nil {
return nil, err
}
var (
blockSize = len(key)
nBlocks = (len(cipherText) + blockSize - 1) / blockSize
plainText = make([]byte, 0, len(cipherText))
)
for b := range nBlocks {
var (
start = b * blockSize
end = start + blockSize
)
plainText = append(plainText, decrypter(cipherText[start:end])...)
}
return plainText, nil
}
// decryptAesEcbString is a wrapper of decryptAesEcb for when you have a cipher
// text to decrypt and a key to decrypt it as strings.
func decryptAesEcbString(cipherText, key string) (string, error) {
plainText, err := decryptAesEcb([]byte(cipherText), []byte(key))
return string(plainText), err
}
// aesDecrypter initializes an AES decryption operation using the provided key.
// It returns an AESECBWorker which performs the decryption of a byte slice
// with the given key.
func aesDecrypter(key []byte) (aesWorker, error) {
aesCipher, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("instantiating AES cipher: %w", err)
}
decrypter := func(cipherText []byte) []byte {
plainText := make([]byte, len(cipherText))
aesCipher.Decrypt(plainText, cipherText)
return plainText
}
return decrypter, nil
}