-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsa_test.go
107 lines (102 loc) · 4.12 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
package utils
import (
"encoding/base64"
"testing"
)
//
//var privateKey = []byte(`
//-----BEGIN RSA PRIVATE KEY-----
//MIICXQIBAAKBgQDZsfv1qscqYdy4vY+P4e3cAtmvppXQcRvrF1cB4drkv0haU24Y
//7m5qYtT52Kr539RdbKKdLAM6s20lWy7+5C0DgacdwYWd/7PeCELyEipZJL07Vro7
//Ate8Bfjya+wltGK9+XNUIHiumUKULW4KDx21+1NLAUeJ6PeW+DAkmJWF6QIDAQAB
//AoGBAJlNxenTQj6OfCl9FMR2jlMJjtMrtQT9InQEE7m3m7bLHeC+MCJOhmNVBjaM
//ZpthDORdxIZ6oCuOf6Z2+Dl35lntGFh5J7S34UP2BWzF1IyyQfySCNexGNHKT1G1
//XKQtHmtc2gWWthEg+S6ciIyw2IGrrP2Rke81vYHExPrexf0hAkEA9Izb0MiYsMCB
///jemLJB0Lb3Y/B8xjGjQFFBQT7bmwBVjvZWZVpnMnXi9sWGdgUpxsCuAIROXjZ40
//IRZ2C9EouwJBAOPjPvV8Sgw4vaseOqlJvSq/C/pIFx6RVznDGlc8bRg7SgTPpjHG
//4G+M3mVgpCX1a/EU1mB+fhiJ2LAZ/pTtY6sCQGaW9NwIWu3DRIVGCSMm0mYh/3X9
//DAcwLSJoctiODQ1Fq9rreDE5QfpJnaJdJfsIJNtX1F+L3YceeBXtW0Ynz2MCQBI8
//9KP274Is5FkWkUFNKnuKUK4WKOuEXEO+LpR+vIhs7k6WQ8nGDd4/mujoJBr5mkrw
//DPwqA3N5TMNDQVGv8gMCQQCaKGJgWYgvo3/milFfImbp+m7/Y3vCptarldXrYQWO
//AQjxwc71ZGBFDITYvdgJM1MTqc8xQek1FXn1vfpy2c6O
//-----END RSA PRIVATE KEY-----
//`)
//
//var publicKey = []byte(`
//-----BEGIN PUBLIC KEY-----
//MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZsfv1qscqYdy4vY+P4e3cAtmv
//ppXQcRvrF1cB4drkv0haU24Y7m5qYtT52Kr539RdbKKdLAM6s20lWy7+5C0Dgacd
//wYWd/7PeCELyEipZJL07Vro7Ate8Bfjya+wltGK9+XNUIHiumUKULW4KDx21+1NL
//AUeJ6PeW+DAkmJWF6QIDAQAB
//-----END PUBLIC KEY-----
//`)
var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAu9n07e4eE7Biacx9R0LEiQ3rZgcwz9LJjyKol7EdhlVHJ98J
qhYXfotJdeXGLQvxGADmsbDY5ldr0Wk1UsjNwxgTCXIXSabXKc7rOBlX1PaiPiAF
3qjc5rv660s0K9oWckZ+08i4NNmQFsZ6a0HgsyfSLG2hfegWh77Orgcz2BcnRoBL
9MJCEuFENqf+xdueVTfZz2rCM7dugMcZTyoWFcP74c76yw4vHDQAPjpbmrh0WuMI
M5cJ4W+u/856kEaD60mxyPEL+1W2NJQaJ9usSNDYvXKr3Vonu0I0BQt0LfuYHzWK
3iBwq3SzcamR/60heziJgZ0bJBofQyHj+Cn9lwIDAQABAoIBACqS4izOa6igsB00
SCxPWIWLTw9nj8t6BU5YV4dRj9RzHVZO+TzAFwEKBlMfCUQKUdDT23ToFLBXncrv
IjOp6OBPY3kfj2GU22zRRYQIUlykrO6RiWMGOFJexiZve9p4ad/qVDIhaoYnzL0s
rHAElS1lV//TtOb5I6oON38/iKNvcEKJlhxELvqzLDOnGnAKzM3l93J5dmrkpmOZ
8rZVyeXNtrHisrrJ0AoDmj7MXr4mmmi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AOCAQ8AMIIBCgKCAQEAu9n07e4eE7Biacx9R0LE
iQ3rZgcwz9LJjyKol7EdhlVHJ98JqhYXfotJdeXGLQvxGADmsbDY5ldr0Wk1UsjN
wxgTCXIXSabXKc7rOBlX1PaiPiAF3qjc5rv660s0K9oWckZ+08i4NNmQFsZ6a0Hg
syfSLG2hfegWh77Orgcz2BcnRoBL9MJCEuFENqf+xdueVTfZz2rCM7dugMcZTyoW
FcP74c76yw4vHDQAPjpbmrh0WuMIM5cJ4W+u/856kEaD60mxyPEL+1W2NJQaJ9us
SNDYvXKr3Vonu0I0BQt0LfuYHzWK3iBwq3SzcamR/60heziJgZ0bJBofQyHj+Cn9
lwIDAQAB
-----END PUBLIC KEY-----
`)
func TestRsa(t *testing.T) {
data, err := RsaEncrypt(S2B("2#95588#13683515842#陈贞#123456#"), publicKey)
if err != nil {
panic(err)
}
//t.Log(B2S(data))
tt:= base64.StdEncoding.EncodeToString(data)
t.Log(tt)
tt2,err:=base64.StdEncoding.DecodeString(tt)
origData, err := RsaDecrypt(tt2, privateKey)
if err != nil {
panic(err)
}
t.Log(B2S(origData))
// 1#95588#13683515838#陈贞#123456#411303199108182816
}
func BenchmarkRas(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
data, err := RsaEncrypt(S2B("test123"), publicKey)
if err != nil {
panic(err)
}
origData, err := RsaDecrypt(data, privateKey)
if err != nil {
panic(err)
}
b.Log(B2S(origData))
}
}