-
Notifications
You must be signed in to change notification settings - Fork 0
/
AESFortified.go
73 lines (49 loc) · 1.32 KB
/
AESFortified.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
70
71
72
73
package crypto
import (
"errors"
"strconv"
"strings"
)
func AESFortifiedDecrypt(value string, password string) (string, error) {
inputBytes, err := Base64Decode(value)
if err != nil {
return "", err
}
strongPassword := PasswordFortify(password)
first, err := AESDecrypt(inputBytes, strongPassword[:32])
if err != nil {
return "", errors.New("xor. " + err.Error())
}
v4, err := Base64Decode(string(first))
if err != nil {
return "", errors.New("base64.1. " + err.Error())
}
a := strings.Split(string(v4), "_")
if len(a) < 2 {
return "", errors.New("password incorrect")
}
upTo, err := strconv.Atoi(a[0])
if err != nil {
return "", errors.New("atoi. " + err.Error())
}
v1 := a[1][0:upTo]
v2, err := Base64Decode(v1)
if err != nil {
return "", errors.New("base64.2. " + err.Error())
}
return string(v2), nil
}
func AESFortifiedEncrypt(value string, password string) (string, error) {
strongPassword := PasswordFortify(password)
v1 := Base64Encode([]byte(value))
v2 := strconv.Itoa(len(v1)) + "_" + v1
randomBlock := StrRandom(RequiredBlockLength(len(v2)))
v3 := v2 + "" + randomBlock[len(v2):]
v4 := Base64Encode([]byte(v3))
outputBytes, err := AESEncrypt([]byte(v4), strongPassword[:32])
if err != nil {
return "", err
}
output := Base64Encode(outputBytes)
return output, nil
}