-
Notifications
You must be signed in to change notification settings - Fork 4
/
transform.go
61 lines (53 loc) · 1.43 KB
/
transform.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
package transform
import (
"crypto/aes"
"crypto/cipher"
"fmt"
)
const (
secret1 = "ay3$&dw*ndAD!9)<" // 'a'
secret2 = "7e3*WwI(@Dczxcue" // 'b'
)
/*
//From java:
SecretKeySpec sks = new SecretKeySpec(secret1.getBytes("ASCII"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
IvParameterSpec ips = new IvParameterSpec(secret2.getBytes());
cipher.init(Cipher.DECRYPT_MODE, sks, ips);
byte res[] = cipher.doFinal(decb);
String sres = new String(res);
System.out.println("Result: \"" + sres + "\"");
*/
func Decrypt(ciphertext []byte) []byte {
mod := len(ciphertext) % 16
if mod != 0 {
// fmt.Printf("(Cipherlen %d bytes (%d))", len(ciphertext), mod)
ciphertext = ciphertext[:len(ciphertext)-mod]
}
key := []byte(secret1)
c, err := aes.NewCipher(key)
if err != nil {
fmt.Printf("newcipher(): %s\n", err)
}
iv := []byte(secret2)
mode := cipher.NewCBCDecrypter(c, iv)
res := make([]byte, len(ciphertext))
mode.CryptBlocks(res, ciphertext)
return res
}
func Encrypt(plaintext []byte) []byte {
for len(plaintext)%16 != 0 {
//fmt.Printf("(Plainlen %d bytes (%d))\n", len(plaintext), len(plaintext)%16)
plaintext = append(plaintext, 0)
}
key := []byte(secret1)
c, err := aes.NewCipher(key)
if err != nil {
fmt.Printf("newcipher(): %s\n", err)
}
iv := []byte(secret2)
mode := cipher.NewCBCEncrypter(c, iv)
res := make([]byte, len(plaintext))
mode.CryptBlocks(res, plaintext)
return res
}