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
3d1b7b04
Commit
3d1b7b04
authored
Nov 18, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实体位置
parent
70f336e8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
28 deletions
+94
-28
RsaHelper.java
.../src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
+65
-28
TestRsa.java
yzg-util-base/src/test/java/helper/TestRsa.java
+29
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
View file @
3d1b7b04
...
@@ -34,6 +34,32 @@ public final class RsaHelper {
...
@@ -34,6 +34,32 @@ public final class RsaHelper {
super
();
super
();
}
}
private
interface
HandleBytes
{
byte
[]
handle
(
byte
[]
from
,
int
offset
,
int
len
)
throws
Exception
;
}
private
static
byte
[]
handle
(
byte
[]
froms
,
int
size
,
HandleBytes
handle
)
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
int
inputLen
=
froms
.
length
;
int
offSet
=
0
;
byte
[]
cache
;
int
i
=
0
;
// 对数据分段加密
while
(
inputLen
-
offSet
>
0
)
{
if
(
inputLen
-
offSet
>
size
)
{
cache
=
handle
.
handle
(
froms
,
offSet
,
size
);
}
else
{
cache
=
handle
.
handle
(
froms
,
offSet
,
inputLen
-
offSet
);
}
out
.
write
(
cache
,
0
,
cache
.
length
);
i
++;
offSet
=
i
*
size
;
}
byte
[]
to
=
out
.
toByteArray
();
out
.
close
();
return
to
;
}
/**
/**
* 生成密钥对
* 生成密钥对
*
*
...
@@ -103,29 +129,14 @@ public final class RsaHelper {
...
@@ -103,29 +129,14 @@ public final class RsaHelper {
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
byte
[]
bytesFrom
=
source
.
getBytes
(
SystemContants
.
UTF8
);
byte
[]
bytes
=
source
.
getBytes
(
SystemContants
.
UTF8
);
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
byte
[]
to
=
handle
(
bytes
,
MAX_ENCRYPT_BLOCK
,
new
HandleBytes
()
{
@Override
int
inputLen
=
bytesFrom
.
length
;
public
byte
[]
handle
(
byte
[]
from
,
int
offset
,
int
len
)
throws
Exception
{
int
offSet
=
0
;
return
cipher
.
doFinal
(
from
,
offset
,
len
);
byte
[]
cache
;
int
i
=
0
;
// 对数据分段加密
while
(
inputLen
-
offSet
>
0
)
{
if
(
inputLen
-
offSet
>
MAX_ENCRYPT_BLOCK
)
{
cache
=
cipher
.
doFinal
(
bytesFrom
,
offSet
,
MAX_ENCRYPT_BLOCK
);
}
else
{
cache
=
cipher
.
doFinal
(
bytesFrom
,
offSet
,
inputLen
-
offSet
);
}
out
.
write
(
cache
,
0
,
cache
.
length
);
i
++;
offSet
=
i
*
MAX_ENCRYPT_BLOCK
;
}
}
byte
[]
encryptedData
=
out
.
toByteArray
();
});
out
.
close
();
return
encodeBase64
(
to
);
return
encodeBase64
(
encryptedData
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
...
@@ -140,11 +151,19 @@ public final class RsaHelper {
...
@@ -140,11 +151,19 @@ public final class RsaHelper {
public
static
String
decryptionByPublicKey
(
String
target
,
String
publicKeyStr
)
{
public
static
String
decryptionByPublicKey
(
String
target
,
String
publicKeyStr
)
{
try
{
try
{
byte
[]
bytes
=
decodeBase64
(
target
);
byte
[]
bytes
=
decodeBase64
(
target
);
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
cipher
.
update
(
bytes
);
return
new
String
(
cipher
.
doFinal
(),
SystemContants
.
UTF8
);
byte
[]
to
=
handle
(
bytes
,
MAX_ENCRYPT_BLOCK
,
new
HandleBytes
()
{
@Override
public
byte
[]
handle
(
byte
[]
from
,
int
offset
,
int
len
)
throws
Exception
{
return
cipher
.
doFinal
(
from
,
offset
,
len
);
}
});
return
new
String
(
to
,
SystemContants
.
UTF8
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
}
}
...
@@ -181,11 +200,20 @@ public final class RsaHelper {
...
@@ -181,11 +200,20 @@ public final class RsaHelper {
*/
*/
public
static
String
encryptionByPrivateKey
(
String
source
,
String
privateKeyStr
)
{
public
static
String
encryptionByPrivateKey
(
String
source
,
String
privateKeyStr
)
{
try
{
try
{
byte
[]
bytes
=
source
.
getBytes
(
SystemContants
.
UTF8
);
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
cipher
.
update
(
source
.
getBytes
(
SystemContants
.
UTF8
));
String
target
=
encodeBase64
(
cipher
.
doFinal
());
byte
[]
to
=
handle
(
bytes
,
MAX_ENCRYPT_BLOCK
,
new
HandleBytes
()
{
@Override
public
byte
[]
handle
(
byte
[]
from
,
int
offset
,
int
len
)
throws
Exception
{
return
cipher
.
doFinal
(
from
,
offset
,
len
);
}
});
String
target
=
encodeBase64
(
to
);
return
target
;
return
target
;
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
throw
new
RuntimeException
(
ex
);
...
@@ -200,11 +228,20 @@ public final class RsaHelper {
...
@@ -200,11 +228,20 @@ public final class RsaHelper {
*/
*/
public
static
String
decryptionByPrivateKey
(
String
target
,
String
privateKeyStr
)
{
public
static
String
decryptionByPrivateKey
(
String
target
,
String
privateKeyStr
)
{
try
{
try
{
byte
[]
bytes
=
decodeBase64
(
target
);
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
cipher
.
update
(
decodeBase64
(
target
));
return
new
String
(
cipher
.
doFinal
(),
SystemContants
.
UTF8
);
byte
[]
to
=
handle
(
bytes
,
MAX_ENCRYPT_BLOCK
,
new
HandleBytes
()
{
@Override
public
byte
[]
handle
(
byte
[]
from
,
int
offset
,
int
len
)
throws
Exception
{
return
cipher
.
doFinal
(
from
,
offset
,
len
);
}
});
return
new
String
(
to
,
SystemContants
.
UTF8
);
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
throw
new
CodeException
(
"解密失败:"
+
ex
.
getMessage
(),
ex
);
throw
new
CodeException
(
"解密失败:"
+
ex
.
getMessage
(),
ex
);
}
}
...
...
yzg-util-base/src/test/java/helper/TestRsa.java
0 → 100644
View file @
3d1b7b04
package
helper
;
import
com.yanzuoguang.util.helper.RsaHelper
;
import
org.junit.Test
;
public
class
TestRsa
{
@Test
public
void
test
()
throws
Exception
{
RsaHelper
.
generatorKeyPair
();
}
@Test
public
void
rsa
()
{
String
publicKey
=
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3GdrIdT2lOblSalXgw4Bjnv7f2ybH+iv39QLUO4j1PeN0vacswQgmUq3KHLbxWeBqNTdsuR/G5zKv4e3raEYsAxrlHw/AJCJQOKTpYEPKLiVhNj7mqg1+I66BRwaG9AunN4bF6LfTj2wgFElVbFGbv1v8SEVDRYiUkwVsAo5JqQIDAQAB"
;
String
privateKey
=
"MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALcZ2sh1PaU5uVJqVeDDgGOe/t/bJsf6K/f1AtQ7iPU943S9pyzBCCZSrcoctvFZ4Go1N2y5H8bnMq/h7etoRiwDGuUfD8AkIlA4pOlgQ8ouJWE2PuaqDX4jroFHBob0C6c3hsXot9OPbCAUSVVsUZu/W/xIRUNFiJSTBWwCjkmpAgMBAAECgYBwJ18SsHWtEhmI+OdXgIjQ/J/j4Kn1jjCGdkZgV4NBrMH5TP3sdOSYDMa06TfJyCKlC6nCZ/al8BHlF/+S1VE9Mu8DRrbHAO4OJQyOEaQ64mePL6jYkhVbjr4q7kHTbsIWVu3sH9TJRa3n+lsEpEv90ri458+ofn++h+rlYKlLfQJBAPVWi1VanQlyAtGKmeA7GrxH+XYiqeq4J1suT1qUTsRwEM4RLUZcrsdGio2bVwbbyX3TB2vYdLvf1otdndKBr0MCQQC/Due/BN+B/hqOgMdW2RoHfycg/HVOp7CpsBikRUMegDQGPb6N+z/BIXkVBNHBADzaTcJh0DgSsoMRhlEdTWajAkEAq2V31/yDAytEbtGOqMmB5xG9ZNvYq2NWE2xqAdTkpnXIN75mS+bKL+vHNiDVDrSTsrSwVZaWDv7U6u5PKNZy8wJBAI/K/Af79uuy/vG5Yk2u37Q8sopU90TXWFKdwi4AIt/VxVHdLolVS0pjkumK0wLa1vHGDEHpoAoSaCrMWEghdNkCQQCUFZxSRZB8XiLtlNyT5s6UX89Ov3eZO1KmFWiXgXuZ3STr/8sDhyC9TN8uq8vK4lQiihlZQFw89LwogtJLAExc"
;
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
500
;
i
++)
{
sb
.
append
((
char
)
(
'A'
+
i
%
26
));
}
String
from
=
sb
.
toString
();
String
value
=
RsaHelper
.
encryptionByPublicKey
(
from
,
publicKey
);
String
result
=
RsaHelper
.
decryptionByPrivateKey
(
value
,
privateKey
);
System
.
out
.
println
(
from
);
System
.
out
.
println
(
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