Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
10448834
Commit
10448834
authored
Jun 27, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
表结构修改
parent
c53c65c3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
11 deletions
+87
-11
RsaHelper.java
.../src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
+53
-6
TestRsa.java
yzg-util-base/src/test/java/helper/TestRsa.java
+34
-5
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
View file @
10448834
...
...
@@ -83,6 +83,20 @@ public final class RsaHelper {
* @throws Exception
*/
public
static
void
generatorKeyPair
()
{
try
{
RsaInfo
rsaInfo
=
generatorRsa
();
Log
.
info
(
RsaHelper
.
class
,
"生成的公钥:%s\n生成的私钥:%s"
,
rsaInfo
.
getPublicKey
(),
rsaInfo
.
getPrivateKey
());
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
* 生成密钥对
*
* @throws Exception
*/
public
static
RsaInfo
generatorRsa
()
{
try
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
ALGORITHM_RSA
);
keyPairGen
.
initialize
(
KEYPAIR_LEN
);
...
...
@@ -90,12 +104,10 @@ public final class RsaHelper {
RSAPublicKey
rsaPublicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
RSAPrivateKey
rsaPrivateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
byte
[]
keyBs
=
rsaPublicKey
.
getEncoded
();
String
publicKey
=
encodeBase64
(
keyBs
);
Log
.
info
(
RsaHelper
.
class
,
"生成的公钥:\t %s"
,
publicKey
);
keyBs
=
rsaPrivateKey
.
getEncoded
();
String
privateKey
=
encodeBase64
(
keyBs
);
Log
.
info
(
RsaHelper
.
class
,
"生成的私钥:\t %s"
,
privateKey
);
String
publicKey
=
encodeBase64
(
rsaPublicKey
.
getEncoded
());
String
privateKey
=
encodeBase64
(
rsaPrivateKey
.
getEncoded
());
return
new
RsaInfo
(
publicKey
,
privateKey
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
...
...
@@ -316,4 +328,39 @@ public final class RsaHelper {
throw
new
RuntimeException
(
ex
);
}
}
public
static
class
RsaInfo
{
/**
* 公钥
*/
private
String
publicKey
;
/**
* 私钥
*/
private
String
privateKey
;
public
RsaInfo
()
{
}
public
RsaInfo
(
String
publicKey
,
String
privateKey
)
{
this
.
publicKey
=
publicKey
;
this
.
privateKey
=
privateKey
;
}
public
String
getPublicKey
()
{
return
publicKey
;
}
public
void
setPublicKey
(
String
publicKey
)
{
this
.
publicKey
=
publicKey
;
}
public
String
getPrivateKey
()
{
return
privateKey
;
}
public
void
setPrivateKey
(
String
privateKey
)
{
this
.
privateKey
=
privateKey
;
}
}
}
\ No newline at end of file
yzg-util-base/src/test/java/helper/TestRsa.java
View file @
10448834
package
helper
;
import
com.yanzuoguang.util.helper.RsaHelper
;
import
org.junit.Assert
;
import
org.junit.Test
;
public
class
TestRsa
{
...
...
@@ -8,24 +9,52 @@ public class TestRsa {
@Test
public
void
test
()
throws
Exception
{
RsaHelper
.
generatorKeyPair
();
}
@Test
public
void
testRsa
()
throws
Exception
{
RsaHelper
.
RsaInfo
rsaInfo
=
RsaHelper
.
generatorRsa
();
testRsaPublicEncode
(
rsaInfo
);
testRsaPrivateEncode
(
rsaInfo
);
}
@Test
public
void
rsa
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q8vK4lQiihlZQFw89LwogtJLAExc"
;
RsaHelper
.
RsaInfo
rsaInfo
=
new
RsaHelper
.
RsaInfo
(
publicKey
,
privateKey
);
testRsaPublicEncode
(
rsaInfo
);
testRsaPrivateEncode
(
rsaInfo
);
}
private
void
testRsaPublicEncode
(
RsaHelper
.
RsaInfo
rsaInfo
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
sb
.
append
((
char
)
(
'A'
+
i
%
26
));
}
String
from
=
sb
.
toString
();
String
temp
=
RsaHelper
.
encryptionByPublicKey
(
from
,
rsaInfo
.
getPublicKey
());
String
result
=
RsaHelper
.
decryptionByPrivateKey
(
temp
,
rsaInfo
.
getPrivateKey
());
System
.
out
.
println
(
"原字段:"
+
from
);
System
.
out
.
println
(
"公钥加密后字段"
+
temp
);
System
.
out
.
println
(
"私钥解密后字段"
+
result
);
Assert
.
assertEquals
(
from
,
result
);
}
private
void
testRsaPrivateEncode
(
RsaHelper
.
RsaInfo
rsaInfo
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
sb
.
append
((
char
)
(
'A'
+
i
%
26
));
}
String
from
=
sb
.
toString
();
String
temp
=
RsaHelper
.
encryptionByPublicKey
(
from
,
publicKey
);
String
result
=
RsaHelper
.
decryptionByPrivateKey
(
temp
,
privateKey
);
System
.
out
.
println
(
from
);
System
.
out
.
println
(
result
);
System
.
out
.
println
(
temp
);
String
temp
=
RsaHelper
.
encryptionByPrivateKey
(
from
,
rsaInfo
.
getPrivateKey
());
String
result
=
RsaHelper
.
decryptionByPublicKey
(
temp
,
rsaInfo
.
getPublicKey
());
System
.
out
.
println
(
"原字段:"
+
from
);
System
.
out
.
println
(
"公钥加密后字段"
+
temp
);
System
.
out
.
println
(
"私钥解密后字段"
+
result
);
Assert
.
assertEquals
(
from
,
result
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment