/
des_encrypt_decrypt.go
57 lines (36 loc) · 1.04 KB
/
des_encrypt_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
// "bufio"
"bytes"
"crypto/cipher"
"crypto/des"
"fmt"
"io"
)
func main() {
c, err := des.NewCipher([]byte("32129232"))
if err != nil {
panic(err)
}
message := bytes.NewBufferString("this isn't a really secret, do you know that? no secret is best secret.")
fmt.Println("original bytes:")
fmt.Println([]byte(message.String()))
encrypted := bytes.NewBuffer(nil)
// var encrypted bytes.Buffer
b8 := make([]byte, 8)
w := cipher.StreamWriter{S: cipher.NewOFB(c, b8), W: encrypted}
io.Copy(w, message)
fmt.Println("encrypted bytes:")
fmt.Println(encrypted.Bytes())
encryptedOutput := fmt.Sprintf("%02x", encrypted.Bytes())
fmt.Println(encryptedOutput)
digital := fmt.Sprintf("%x", []byte("that's cool!"))
var m []byte
fmt.Sscanf(digital, "%x", &m)
fmt.Println(string(m))
c2, err := des.NewCipher([]byte("32129232"))
w2 := cipher.StreamReader{S: cipher.NewOFB(c2, b8), R: encrypted}
decrypted_message := bytes.NewBuffer(nil)
io.Copy(decrypted_message, w2)
fmt.Println(decrypted_message.String())
}