RSA典型非对称加密算法

私钥加密–>公钥解密,反之亦然,但不安全。也可以当做数字签名。


public 
class 
RSACoder {


        
//非对称加密算法
        
public 
static 
final 
String 
KEY_ALGORITHM
= 
“RSA”
;
        
//公钥
        
private 
static 
final 
String 
PUBLIC_KEY
= 
“RSAPublicKey”
;
        
//私钥
        
private 
static 
final 
String 
PRIVATE_KEY
= 
“RSAPrivateKey”
;
        
//密钥长度,默认1024位,512~65536位,必须是64的倍数
        
private 
static 
final 
int 
KEY_SIZE
=512;
       
        
//私钥解密
        
public 
static 
byte
[] decryptByPrivateKey(
byte
[] data, 
byte
[] key) 
throws 
KeyException, Exception{
               
//取得私钥,
              PKCS8EncodedKeySpec pkcs8KeySpec= 
new 
PKCS8EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
              PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
              
               
//对数据解密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. 
DECRYPT_MODE
, privateKey);
               
return 
cipher.doFinal(data);
       }
       
        
//公钥解密
        
public 
static 
byte
[] decryptByPublicKey(
byte
[] data, 
byte
[] key) 
throws 
Exception{
              X509EncodedKeySpec x509= 
new 
X509EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
              
               
//生成公钥
              PublicKey publicKey=keyFactory.generatePublic(x509);
               
//对数据解密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. 
DECRYPT_MODE
, publicKey);
               
return 
cipher.doFinal(data);
       }
       
        
//公钥加密
        
public 
static 
byte
[] encrpytByPublicKey(
byte
[] data, 
byte
[] key) 
throws 
Exception{
               
//取得公钥
              X509EncodedKeySpec x509= 
new 
X509EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
              PublicKey pubKey=keyFactory.generatePublic(x509);
               
//对数据加密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. 
ENCRYPT_MODE
, pubKey);
               
return 
cipher.doFinal(data);
       }
       
        
//私钥加密
        
public 
static 
byte
[] encryptByPrivate(
byte
[] data, 
byte
[] key) 
throws 
Exception, GeneralSecurityException{
               
//取得私钥
              PKCS8EncodedKeySpec pkcs8= 
new 
PKCS8EncodedKeySpec(key);
              KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
               
//生成私钥
              PrivateKey privateKey=keyFactory.generatePrivate(pkcs8);
               
//对数据加密
              Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
              cipher.init(Cipher. 
ENCRYPT_MODE
, privateKey);
               
return 
cipher.doFinal(data);
       }
       
        
//取得私钥
        
public 
static 
byte
[] getPrivateKey(Map<String,Object> keyMap){
              Key key=(Key)keyMap.get( 
PRIVATE_KEY
);
               
return 
key.getEncoded();
       }
        
//取得公钥
        
public 
static 
byte
[] getPublicKey(Map<String,Object> keyMap){
              Key key=(Key)keyMap.get( 
PUBLIC_KEY
);
               
return 
key.getEncoded();
       }
       
        
//初始化密钥
        
public 
static 
Map<String,Object> initKey() 
throws 
Exception{
              KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance( 
KEY_ALGORITHM
);
              keyPairGen.initialize( 
KEY_SIZE
);
              KeyPair keyPair=keyPairGen.generateKeyPair();
              
               
//公钥
              RSAPublicKey publicKey=(RSAPublicKey)keyPair.getPublic();
               
//私钥
              RSAPrivateKey privateKey=(RSAPrivateKey)keyPair.getPrivate();
              
               
//封装密钥
              Map<String,Object> keyMap= 
new 
HashMap<String,Object>(2);
              keyMap.put( 
PRIVATE_KEY
, privateKey);
              keyMap.put( 
PUBLIC_KEY
, publicKey);
               
return 
keyMap;
       }


}


public 
class 
RSATest {


        
private 
static 
byte
[] 
privateKey
;
        
private 
static 
byte
[] 
publicKey
;
       
        
public 
static 
void 
initKey() 
throws 
Exception{
               
//初始化密钥
              Map<String,Object> keyMap=RSACoder. initKey();
               
publicKey
=RSACoder.getPublicKey(keyMap);
               
privateKey
=RSACoder.getPrivateKey(keyMap);
              
              System. 
out
.println(
“公钥:\n” 
+Base64.encodeBase64String (
publicKey 
));
              System. 
out
.println(
“私钥:\n” 
+Base64.encodeBase64String (
privateKey 
));
       }
        
public 
static 
void 
main(String[] args) 
throws 
GeneralSecurityException, Exception {
               
// 
TODO 
Auto-generated method stub
               initKey();
              String inputStr1= 
“RSA加密算法” 
;
               
byte
[] data1=inputStr1.getBytes();
              System. 
out
.println(
“原文:\n” 
+inputStr1);
              
               
//私钥加密
               
byte
[] encodeData1=RSACoder.encryptByPrivate(data1, 
privateKey
);
              System. 
err
.println(
“加密后:\n” 
+Base64.encodeBase64String (encodeData1));
              
               
//公钥解密
               
byte
[] decodeData1=RSACoder.decryptByPublicKey(encodeData1, 
publicKey
);
              String outputStr1= 
new 
String(decodeData1);
              System. 
err
.println(
“解密后:\n” 
+outputStr1);
              
               assertEquals(inputStr1,outputStr1);
              
       }


}

    原文作者:徐小鱼
    原文地址: https://www.cnblogs.com/littlefishxu/p/3969186.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞