-
Notifications
You must be signed in to change notification settings - Fork 0
/
hwcrypt.go
51 lines (43 loc) · 1017 Bytes
/
hwcrypt.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"log"
"fmt"
"io"
)
//https://golang.org/src/crypto/cipher/example_test.go
func Encrypt(data []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
cipher_text := make([]byte, aes.BlockSize + len(data))
iv := cipher_text[:aes.BlockSize]
_, err = io.ReadFull(rand.Reader, iv);
if err != nil {
log.Panic(err)
}
cip := cipher.NewCFBEncrypter(block, iv)
cip.XORKeyStream(cipher_text[aes.BlockSize:], data)
return cipher_text
}
func Decrypt(data []byte, key []byte) []byte{
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
}
iv := data[:aes.BlockSize]
cipher_text := data[aes.BlockSize:]
cip := cipher.NewCFBDecrypter(block, iv)
cip.XORKeyStream(cipher_text, cipher_text)
return cipher_text
}
func main() {
data := []byte{42, 69}
key := []byte{11,22,33,44,11,22,33,44,11,22,33,44,11,22,33,44}
ec := Encrypt(data, key)
dc := Decrypt(ec, key)
fmt.Println(dc)
}