-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaes.go
More file actions
96 lines (80 loc) · 2.63 KB
/
aes.go
File metadata and controls
96 lines (80 loc) · 2.63 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
package xutil
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
)
type AESMode string
const (
AESModeCBC = AESMode("CBC")
AESModeCFB = AESMode("CFB")
AESModeOFB = AESMode("OFB")
)
var ErrUnknownAESMode = errors.New("unknown AES mode")
// AESEncrypt AES encryption, supports CBC,CFB,OFB mode, PKCS7Padding padding.
// key128 := "abcdefghijklmnop" // 16 bytes = 128 bits
// key192 := "abcdefghijklmnopqrstuvwx" // 24 bytes = 192 bits
// key256 := "abcdefghijklmnopabcdefghijklmnop" // 32 bytes = 256 bits
func AESEncrypt(plainText string, mode AESMode, key, iv string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
plainTextBytes := PKCS7Padding([]byte(plainText), block.BlockSize())
cipherText := make([]byte, len(plainTextBytes))
switch mode {
case AESModeCBC:
cbc := cipher.NewCBCEncrypter(block, []byte(iv))
cbc.CryptBlocks(cipherText, plainTextBytes)
case AESModeCFB:
cfb := cipher.NewCFBEncrypter(block, []byte(iv))
cfb.XORKeyStream(cipherText, plainTextBytes)
case AESModeOFB:
ofb := cipher.NewOFB(block, []byte(iv))
ofb.XORKeyStream(cipherText, plainTextBytes)
default:
return "", ErrUnknownAESMode
}
return base64.StdEncoding.EncodeToString(cipherText), nil
}
func AESDecrypt(cipherText string, mode AESMode, key, iv string) (string, error) {
cipherTextBytes, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return "", err
}
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
decryptedText := make([]byte, len(cipherTextBytes))
switch mode {
case AESModeCBC:
cbc := cipher.NewCBCDecrypter(block, []byte(iv))
cbc.CryptBlocks(decryptedText, cipherTextBytes)
case AESModeCFB:
cfb := cipher.NewCFBDecrypter(block, []byte(iv))
cfb.XORKeyStream(decryptedText, cipherTextBytes)
case AESModeOFB:
ofb := cipher.NewOFB(block, []byte(iv))
ofb.XORKeyStream(decryptedText, cipherTextBytes)
default:
return "", ErrUnknownAESMode
}
decryptedText = PKCS7UnPadding(decryptedText)
return string(decryptedText), nil
}
// PKCS7Padding adds padding to the input plainTextByte according to PKCS7 mode
func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// PKCS7UnPadding removes padding from the input cipherTextBytes according to PKCS7 mode
func PKCS7UnPadding(origData []byte) []byte {
length := len(origData)
// subtract the number of padding bytes
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}