-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (90 loc) · 3.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Package main - example usage of RSA encryption - decryption
package main
import (
"fmt"
"github.com/pilinux/crypt"
)
func main() {
text := "Hello world"
// public key in PEM format as a string
publicKeyPEM := `
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3awVyjRX9exXCyAwqhwX
YsmNWU5teeqQWpbmnVpYiOuxHhtBzhpBvlK0Xy+8oGXEfmrznCdHG+l5763116Fz
2qHRclCzEIX7fgHgG2Jx7PC6Dm1daOgThxeTrvwfKp9NpITogI4Lfeb/AQ1KOF1Y
/MCoSSKhq40Gucoksdnv8Af21lkb7WIXKYg70ZOUKV+PxSsOKyfi4RoIJl/QGEJv
CKUy/TtV6HuNflMnmGRLKeos7JuugrGIg9/OjamUsd1EY2urNiQ+RYYk4LesQjki
fn/oCrarrfRNCMaS1Wvd1w81RPg+5px+Va0ypymq/ZjeKHDxhQgBlVyIYou+CwzG
awIDAQAB
-----END PUBLIC KEY-----
`
// private key in PEM format as a string
privateKeyPEM := `
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDdrBXKNFf17FcL
IDCqHBdiyY1ZTm156pBaluadWliI67EeG0HOGkG+UrRfL7ygZcR+avOcJ0cb6Xnv
rfXXoXPaodFyULMQhft+AeAbYnHs8LoObV1o6BOHF5Ou/B8qn02khOiAjgt95v8B
DUo4XVj8wKhJIqGrjQa5yiSx2e/wB/bWWRvtYhcpiDvRk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
v5RB+DWxnBOUULj1zy+cysCy8ZQKjVfqgGj5FbBs4XFkKQTFJRM1/srVFI52dO00
b0ci8ITG5zNxs7og++pLQuYRbcRgsPEifV1wAc8V6YjwKC0VnE0M3g5z4FawHl+6
SbInS6BTvRRvtR0h/KS9lntjkrEF5oyERpexhTa7AoGATP9+QU1L7+DN0YTtZsNm
eHcwyhZv1wU8umpgBDSP0ZWxkbeOOlmWlkwtvsw4cwbZsieUvwBvHgmS4ODZmCu7
1fcMzN6SDVLYLOc0coINyBY6/CNMp/sdYJ0qxiSfuFkQAOj5kvzkfSbRw8Bn+OVD
a6bLS9Kl4jLdVtF+OR5VFiY=
-----END PRIVATE KEY-----
`
// RSA: encryption
// Encoder
encoder := crypt.NewEncoder(publicKeyPEM)
if encoder.Err != nil {
fmt.Println(encoder.Err)
return
}
encoder.HashAlg = crypt.SHA512
encryptedData, err := encoder.EncryptRSA(text)
if err != nil {
fmt.Println(err)
return
}
ciphertext := encoder.ToBase64RawStd([]byte(encryptedData))
fmt.Println("ciphertext encoded into base64:")
fmt.Println(ciphertext)
fmt.Println("")
// RSA: decryption
// Decoder
decoder := crypt.NewDecoder(privateKeyPEM)
if decoder.Err != nil {
fmt.Println(decoder.Err)
return
}
decoder.HashAlg = crypt.SHA512
decodedText, err := decoder.FromBase64RawStd(ciphertext)
if err != nil {
fmt.Println(err)
return
}
plaintext, err := decoder.DecryptRSA(decodedText)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:")
fmt.Println(plaintext)
fmt.Println("")
}