发表日期:2019-08 文章编辑:小灯 浏览次数:848
Java中使用OpenSSL生成的RSA公私钥进行数据加解密
Java代码
java.io.BufferedReader;
java.io.IOException;
java.io.InputStream;
java.io.InputStreamReader;
java.security.InvalidKeyException;
java.security.KeyFactory;
java.security.KeyPair;
java.security.KeyPairGenerator;
java.security.NoSuchAlgorithmException;
java.security.SecureRandom;
java.security.interfaces.RSAPrivateKey;
java.security.interfaces.RSAPublicKey;
java.security.spec.InvalidKeySpecException;
java.security.spec.PKCS8EncodedKeySpec;
java.security.spec.X509EncodedKeySpec;
javax.crypto.BadPaddingException;
javax.crypto.Cipher;
javax.crypto.IllegalBlockSizeException;
javax.crypto.NoSuchPaddingException;
sun.misc.BASE64Decoder;
sun.misc.BASE64Encoder;
RSAEncrypt {
String DEFAULT_PUBLIC_KEY=
+ +
+ +
+ +
+ ;
String DEFAULT_PRIVATE_KEY=
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ ;
RSAPrivateKey privateKey;
RSAPublicKey publicKey;
[] HEX_CHAR= {, , , , , , , , , , , , , , , };
RSAPrivateKey getPrivateKey() {
privateKey;
RSAPublicKey getPublicKey() {
publicKey;
genKeyPair(){
;
{
);
(NoSuchAlgorithmException e) {
, SecureRandom());
.privateKey= (RSAPrivateKey) keyPair.getPrivate();
.publicKey= (RSAPublicKey) keyPair.getPublic();
loadPublicKey(InputStream in) Exception{
{
BufferedReader( InputStreamReader(in));
;
StringBuilder();
((readLine= br.readLine())!=){
(readLine.charAt()==){
;
{
);
(IOException e) {
Exception();
(NullPointerException e) {
Exception();
loadPublicKey(String publicKeyStr) Exception{
{
BASE64Decoder();
[] buffer= base64Decoder.decodeBuffer(publicKeyStr);
);
X509EncodedKeySpec(buffer);
.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec);
(NoSuchAlgorithmException e) {
Exception();
(InvalidKeySpecException e) {
Exception();
(IOException e) {
Exception();
(NullPointerException e) {
Exception();
loadPrivateKey(InputStream in) Exception{
{
BufferedReader( InputStreamReader(in));
;
StringBuilder();
((readLine= br.readLine())!=){
(readLine.charAt()==){
;
{
);
(IOException e) {
Exception();
(NullPointerException e) {
Exception();
loadPrivateKey(String privateKeyStr) Exception{
{
BASE64Decoder();
[] buffer= base64Decoder.decodeBuffer(privateKeyStr);
PKCS8EncodedKeySpec(buffer);
);
.privateKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
(NoSuchAlgorithmException e) {
Exception();
(InvalidKeySpecException e) {
Exception();
(IOException e) {
Exception();
(NullPointerException e) {
Exception();
[] encrypt(RSAPublicKey publicKey, [] plainTextData) Exception{
(publicKey== ){
Exception();
;
{
);
[] output= cipher.doFinal(plainTextData);
output;
(NoSuchAlgorithmException e) {
Exception();
(NoSuchPaddingException e) {
;
(InvalidKeyException e) {
Exception();
(IllegalBlockSizeException e) {
Exception();
(BadPaddingException e) {
Exception();
[] decrypt(RSAPrivateKey privateKey, [] cipherData) Exception{
(privateKey== ){
Exception();
;
{
);
[] output= cipher.doFinal(cipherData);
output;
(NoSuchAlgorithmException e) {
Exception();
(NoSuchPaddingException e) {
;
(InvalidKeyException e) {
Exception();
(IllegalBlockSizeException e) {
Exception();
(BadPaddingException e) {
Exception();
String byteArrayToString([] data){
StringBuilder();
( i=; i<data.length; i++){
)>>> ]);
)]);
(i<data.length-){
);
stringBuilder.toString();
main(String[] args){
RSAEncrypt();
{
);
(Exception e) {
);
{
);
(Exception e) {
);
;
+rsaEncrypt.getPrivateKey().toString().length());
+rsaEncrypt.getPublicKey().toString().length());
{
[] cipher = rsaEncrypt.encrypt(rsaEncrypt.getPublicKey(), encryptStr.getBytes());
[] plainText = rsaEncrypt.decrypt(rsaEncrypt.getPrivateKey(), cipher);
+ cipher.length);
+ plainText.length);
String(plainText));
(Exception e) {
日期:2019-02 浏览次数:6404
日期:2019-02 浏览次数:4164
日期:2019-02 浏览次数:3907
日期:2019-02 浏览次数:4364
日期:2019-02 浏览次数:4094
日期:2019-02 浏览次数:7221
日期:2019-02 浏览次数:3951
日期:2019-02 浏览次数:4436
日期:2019-02 浏览次数:4284
日期:2019-02 浏览次数:3970
日期:2019-02 浏览次数:4307
日期:2019-02 浏览次数:4075
日期:2019-02 浏览次数:3975
日期:2019-02 浏览次数:3898
日期:2019-02 浏览次数:4643
日期:2019-02 浏览次数:4098
日期:2019-02 浏览次数:4088
日期:2019-02 浏览次数:4149
日期:2019-02 浏览次数:4107
日期:2019-02 浏览次数:4019
日期:2019-02 浏览次数:4384
日期:2019-09 浏览次数:4406
日期:2019-09 浏览次数:4059
日期:2019-09 浏览次数:4076
日期:2019-09 浏览次数:4028
日期:2019-09 浏览次数:4007
日期:2019-09 浏览次数:4100
日期:2019-09 浏览次数:4047
日期:2019-09 浏览次数:4518
日期:2019-09 浏览次数:5032
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.