This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypter.go
166 lines (132 loc) · 3.76 KB
/
crypter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2020-2024 NGR Softlab
package crypter
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"strings"
)
//////////////////////////////////////////////
// TODO: merge Encrypt/Encrypt2 and Decrypt1&Encrypt2
// TODO: use ngr-logging lib
//////////////////////////////////////////////
func addBase64Padding(value string) string {
m := len(value) % 4
if m != 0 {
value += strings.Repeat("=", 4-m)
}
return value
}
func removeBase64Padding(value string) string {
return strings.Replace(value, "=", "", -1)
}
func Pad(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padText...)
}
func UnPad(src []byte) ([]byte, error) {
length := len(src)
unPadding := int(src[length-1])
if unPadding > length {
return nil, errors.New("unpad error, this could happen when incorrect encryption key is used")
}
return src[:(length - unPadding)], nil
}
func Encrypt(key []byte, text string) (string, error) {
if text == "" {
return "", errors.New("no password error")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
msg := Pad([]byte(text))
cipherText := make([]byte, aes.BlockSize+len(msg))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(cipherText[aes.BlockSize:], msg)
finalMsg := removeBase64Padding(base64.URLEncoding.EncodeToString(cipherText))
return finalMsg, nil
}
func Decrypt(key []byte, text string) (string, error) {
if text == "" {
return "", errors.New("no password error")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
decodedMsg, err := base64.URLEncoding.DecodeString(addBase64Padding(text))
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multipe of decoded message length")
}
iv := decodedMsg[:aes.BlockSize]
msg := decodedMsg[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(msg, msg)
unPadMsg, err := UnPad(msg)
if err != nil {
return "", err
}
return string(unPadMsg), nil
}
///////////////////////////////
////////// LONG KEY ///////////
///////////////////////////////
func Encrypt2(key []byte, message []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := message
b = PKCS5Padding(b, aes.BlockSize)
encMessage := make([]byte, len(b))
iv := key[:aes.BlockSize]
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encMessage, b)
bufF := make([]byte, base64.StdEncoding.EncodedLen(len(encMessage)))
base64.StdEncoding.Encode(bufF, encMessage)
return bufF, nil
}
func Decrypt2(key []byte, encMessage []byte) ([]byte, error) {
decodedBytes := make([]byte, len(encMessage))
n, err := base64.StdEncoding.Decode(decodedBytes, encMessage)
if err != nil {
return []byte(""), fmt.Errorf("bad decrypt: %s", err.Error())
}
bytesToDecrypt := decodedBytes[:n]
iv := key[:aes.BlockSize]
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if (len(bytesToDecrypt) % aes.BlockSize) != 0 {
return nil, errors.New("encMessage bad length")
}
decryptedBytes := make([]byte, len(bytesToDecrypt))
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(decryptedBytes, bytesToDecrypt)
return PKCS5UnPadding(decryptedBytes), nil
}
func PKCS5Padding(cipher []byte, blockSize int) []byte {
padding := blockSize - len(cipher)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipher, padText...)
}
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unPadding := int(src[length-1])
return src[:(length - unPadding)]
}