forked from AtlasInsideCorp/AtlasInsideAES
/
crypto_aes.go
69 lines (62 loc) · 1.82 KB
/
crypto_aes.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
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base64"
"golang.org/x/crypto/pbkdf2"
)
const IterationCount = 65536
const SaltLength = 16
const KeyLength = 16
func setKey(key []byte) (cipher.Block, []byte, error) {
h := sha1.New()
h.Write(key)
salt := h.Sum(nil)
keyEnc := pbkdf2.Key(key, salt, IterationCount, KeyLength, sha1.New)
block, err := aes.NewCipher(keyEnc)
if err != nil {
return nil, nil, err
}
return block, salt[:SaltLength], nil
}
func Encrypt(src string, key string) (string, error) {
if len(src) == 0 {
return "", &InvalidEncryptedDataError{"Invalid crypto"}
}
blkEncrypt, ivEncrypt, err := setKey([]byte(key))
if err != nil {
return "", &InvalidAESKeyError{"Invalid crypto"}
}
ecb := cipher.NewCBCEncrypter(blkEncrypt, ivEncrypt)
content := []byte(src)
content = pkcs5Padding(content, blkEncrypt.BlockSize())
encrypted := make([]byte, len(content))
ecb.CryptBlocks(encrypted, content)
b64 := base64.StdEncoding.EncodeToString(encrypted)
return b64, nil
}
func Decrypt(crypt string, key string) (string, error) {
encryptedData, _ := base64.StdEncoding.DecodeString(crypt)
if len(crypt) == 0 {
return "", &InvalidPassphraseError{"Invalid crypto"}
}
blk, iv, err := setKey([]byte(key))
if err != nil {
return "", &InvalidAESKeyError{"Invalid crypto"}
}
ecb := cipher.NewCBCDecrypter(blk, iv)
decrypted := make([]byte, len(encryptedData))
ecb.CryptBlocks(decrypted, encryptedData)
return string(pkcs5Trimming(decrypted)), nil
}
func pkcs5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padded := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padded...)
}
func pkcs5Trimming(encrypt []byte) []byte {
padding := encrypt[len(encrypt)-1]
return encrypt[:len(encrypt)-int(padding)]
}