-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_test.go
150 lines (125 loc) · 4.94 KB
/
rsa_test.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gocrypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"testing"
)
var pubPEMPKIX = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDR5d33b53ronRpKaY0anNBcrk3
HjgR0iDV9WxiU3lV1gIuTSlmQ4freqxNyqvaPzyq/OEV4pAvWvnZ7XWfhHOvCmqD
x7cOBILyLancCghQt1IZBLUcFNwQtb53QURHTolJsFjzZ6YuRuIEzUhjGxs4cOpK
996tJA02eia15DL6SwIDAQAB
-----END PUBLIC KEY-----`
var privPEMPKCS1 = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDR5d33b53ronRpKaY0anNBcrk3HjgR0iDV9WxiU3lV1gIuTSlm
Q4freqxNyqvaPzyq/OEV4pAvWvnZ7XWfhHOvCmqDx7cOBILyLancCghQt1IZBLUc
FNwQtb53QURHTolJsFjzZ6YuRuIEzUhjGxs4cOpK996tJA02eia15DL6SwIDAQAB
AoGAI9knjNACX7EyQpe7bC5WGvJ2EaTWnKyPsRsmdLCfA3r2imPhUzbrattbvDmG
GlM8kFN6OdulFoFqNL8qnKDphMduMm12TxCZey7+XXAYFKcp0MfGB3YsNwuRAwKJ
KMvP34PtvniFMzlpIotx1vH4D73P9ZSB6FpR1Zd3yTBbayECQQD0kbMRR8gPcs9Y
VHSZ2gRr7QNAgPSU3WEaau3+zTYME0aqpMCXpwJavArotIIuZpDRkGnAgyqIRwr8
AlnrnOBRAkEA27VScn1Zmx67pkBry9CZeuNcCXCe+crsREyXyxhfQHqORKPhy7oy
ePuBQXMkAsyhczdP4WU71s0OZLNKbOWF2wJBAKJdnEkro5kF+rvEjgiaIfdYyaDo
O/gcpNu6A7j2mhwYLAEcEbRcrwZ38rIdLJQLibdnLZ4eNn101MkL8pCmfJECQEVK
mVcMW39FoQ7c0GJ2RoFwRS2g/DJxojQkJ9dgt2HBhS72tJapAZQQVgKLSrcrTFvm
/OVsJQQopbpypS1U8fMCQGnl+XW7ZlqME0maiGAVA6ueXhgWIKikkBtPPjKWZI7C
1Ab3ezfWzrrYYtx8ytFo9/vTc0tKjkzjHsbt7lvDas8=
-----END RSA PRIVATE KEY-----`
func TestRSAEncryptPKCS1v15(t *testing.T) {
rsa, err := NewRSA(WithPublicKey([]byte(pubPEMPKIX)), WithPrivateKey([]byte(privPEMPKCS1)))
if err != nil {
t.Error(err)
}
cipherBytes, err := rsa.EncryptPKCS1v15Base64([]byte("hhliotwapntjcqlt"))
if err != nil {
t.Error(err)
}
fmt.Println(string(cipherBytes))
plainBytes, err := rsa.DecryptPKCS1v15Base64(cipherBytes)
if err != nil {
t.Error(err)
}
fmt.Println(string(plainBytes))
}
func TestRSAEncryptOAEP(t *testing.T) {
rsa, err := NewRSA(WithPublicKey([]byte(pubPEMPKIX)), WithPrivateKey([]byte(privPEMPKCS1)))
if err != nil {
t.Error(err)
}
cipherBytes, err := rsa.EncryptOAEPBase64(sha256.New(), []byte("hhliotwapntjcqlt"), nil)
if err != nil {
t.Error(err)
}
fmt.Println(string(cipherBytes))
plainBytes, err := rsa.DecryptOAEPBase64(sha256.New(), cipherBytes, nil)
if err != nil {
t.Error(err)
}
fmt.Println(string(plainBytes))
}
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AQABo1AwTjAdBgNVHQ4EFgQUy0/0W8nwQfz2tO6AZ2jPkEiTzvUwHwYD
VR0jBBgwFoAUy0/0W8nwQfz2tO6AZ2jPkEiTzvUwDAYDVR0TBAUwAwEB/zANBgkq
hkiG9w0BAQsFAAOCAgEAvEVnUYsIOt87rggmLPqEueynkuQ+562M8EDHSQl82zbe
xDCxeg3DvPgKb+RvaUdt1362z/szK10SoeMgx6+EQLoV9LiVqXwNqeYfixrhrdw3
ppAhYYhymdkbUQCEMHypmXP1vPhAz4o8Bs+eES1M+zO6ErBiD7SqkmBElT+GixJC
6epC9ZQFs+dw3lPlbiZSsGE85sqc3VAs0/JgpL/pb1/Eg4s0FUhZD2C2uWdSyZGc
g0/v3aXJCp4j/9VoNhI1WXz3M45nysZIL5OQgXymLqJElQa1pZ3Wa4i/nidvT4AT
Xlxc/qijM8set/nOqp7hVd5J0uG6qdwLRILUddZ6OpXd7ZNi1EXg+Bpc7ehzGsDt
3UFGzYXDjxYnK2frQfjLS8stOQIqSrGthW6x0fdkVx0y8BByvd5J6+JmZl4UZfzA
m99VxXSt4B9x6BvnY7ktzcFDOjtuLc4B/7yg9fv1eQuStA4cHGGAttsCg1X/Kx8W
PvkkeH0UWDZ9vhH9K36703z89da6MWF+bz92B0+4HoOmlVaXRkvblsNaynJnL0LC
Ayry7QBxuh5cMnDdRwJB3AVJIiJ1GVpb7aGvBOnx+s2lwRv9HWtghb+cbwwktx1M
JHyBf3GZNSWTpKY7cD8V+NnBv3UuioOVVo+XAU4LF/bYUjdRpxWADJizNtZrtFo=
-----END CERTIFICATE-----
`)
func TestParseRSAPublicKeyFromCert(t *testing.T) {
pub, err := ParseRSAPublicKeyFromCert(certPEM)
fmt.Println(pub.Size(), err)
}
func RSAEncrypt2(plainText, certPEM []byte) ([]byte, error) {
block, _ := pem.Decode(certPEM)
if block == nil {
return nil, errors.New("public key error")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.New("failed to parse certificate: " + err.Error())
}
pub := cert.PublicKey.(*rsa.PublicKey)
return rsa.EncryptPKCS1v15(rand.Reader, pub, plainText)
}
func TestNewRSAKeyGenerator1024(t *testing.T) {
g := NewRSAKeyGenerator1024()
pkixKey, err := g.PKIXPublicKeyPEM()
if err != nil {
t.Error(err)
}
fmt.Println(string(pkixKey))
pkcs1Key, err := g.PKCS1PrivateKeyPEM()
if err != nil {
t.Error(err)
}
fmt.Println(string(pkcs1Key))
}