-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (48 loc) · 2.14 KB
/
main.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
package main
import (
"bytes"
"cryptolib/lib/aes"
"cryptolib/lib/rsa"
"fmt"
)
func main() {
// RSA
params := rsa.KeyParams{}
keyPair, err := rsa.GenerateKeyPair(¶ms)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("RSA key pair =", keyPair.PrivateKey)
maxLen := keyPair.PublicKey.Size()
fmt.Println("\nMax text len =", maxLen*8)
message := []byte("Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello!!!")
fmt.Println("\nText len =", len(message)*8)
cipher, err := rsa.Encrypt(message, keyPair.PublicKey)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("\ncipher =", cipher)
}
plaintext, err := rsa.Decrypt(cipher, keyPair.PrivateKey)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("\nplaintext =", string(plaintext))
}
message = []byte("Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! Hello, World! OK!!")
fmt.Println("\nText len =", len(message)*8)
cipher, _ = rsa.Encrypt(message, keyPair.PublicKey)
fmt.Println("\nRSA cipher =", cipher)
plaintext, _ = rsa.Decrypt(cipher, keyPair.PrivateKey)
fmt.Println("\nRSA plaintext =", string(plaintext))
fmt.Println("\nIs messages equal?", bytes.Equal(message, plaintext))
// AES
password := "password!"
salt := []byte("salt!")
cipher, _ = aes.Encrypt(message, password, salt)
fmt.Println("\nAES cipher =", cipher)
plaintext, _ = aes.Decrypt(cipher, password, salt)
fmt.Println("\nAES plaintext =", string(plaintext))
fmt.Println("\nIs messages equal?", bytes.Equal(message, plaintext))
}