-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
50 lines (37 loc) · 951 Bytes
/
demo.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
package main
import (
"log"
"github.com/zpmep/rsautil"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
// Generate key pair
pri, pub, err := rsautil.GenerateKeyPair(512)
check(err)
// Convert public key to bytes
pubBytes, err := rsautil.PublicKeyToBytes(pub)
check(err)
// Convert private key to bytes
priBytes, err := rsautil.PrivateKeyToBytes(pri)
check(err)
log.Println(string(pubBytes))
log.Println(string(priBytes))
// Convert bytes to public key
pub2, err := rsautil.BytesToPublicKey(pubBytes)
check(err)
// Encrypt message with publickey
ciphertext, err := rsautil.Encrypt(pub2, "github.com/zpmep/rsautil")
check(err)
log.Println(string(ciphertext))
// Convert bytes to private key
pri2, err := rsautil.BytesToPrivateKey(priBytes)
check(err)
// Decrypt ciphertext with private key
message, err := rsautil.Decrypt(pri2, ciphertext)
check(err)
log.Println(string(message))
}