-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecrypt.go
43 lines (40 loc) · 1.06 KB
/
decrypt.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
package stringEncrypt
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
)
//
// Code taken from http://stackoverflow.com/questions/18817336/golang-encrypting-a-string-with-aes-and-base64
//
// This is all rewritten from Intermernet's answer on the above stackoverflow page.
//
// Decrypt will take secret key string and encrypted string and return clear string
func Decrypt(key string, text string) (string, error) {
rawkey, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return "", err
}
dectext, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return "", err
}
btext := []byte(dectext)
cipherblock, err := aes.NewCipher([]byte(rawkey))
if err != nil {
return "", err
}
if len(btext) < aes.BlockSize {
return "", errors.New("ciphertext too short")
}
iv := btext[:aes.BlockSize]
btext = btext[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(cipherblock, iv)
cfb.XORKeyStream(btext, btext)
data, err := base64.StdEncoding.DecodeString(string(btext))
if err != nil {
return "", err
}
return string(data), nil
}