RSA非对称加密和解密方法
2007-05-11 12:32
423 查看
RSA公匙和密匙生成方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.markerking;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.ObjectInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.ObjectOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.KeyPair;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.KeyPairGenerator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.PrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.PublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public abstract class RSAKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 生成2048位的RSA公匙和私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void generator(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String privateKeyFileName) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kpg.initialize(2048);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyPair kp = kpg.generateKeyPair();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PublicKey puk = kp.getPublic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrivateKey prk = kp.getPrivate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos1 = new FileOutputStream("publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos2 = new FileOutputStream(privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos1.writeObject(puk);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos2.writeObject(prk);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos1.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos2.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fos1.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fos2.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("公匙生成成功!公匙文件为publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("私匙生成成功!私匙文件为" + privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 获取已经生成的公匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return RSAPublicKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static RSAPublicKey getPublicKey(String publicKeyFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(publicKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPublicKey rsapuk = (RSAPublicKey) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return rsapuk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 获取已经生成的私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return RSAPrivateKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static RSAPrivateKey getPrivateKey(String privateKeyFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPrivateKey psaprk = (RSAPrivateKey) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return psaprk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
调用方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.markerking;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.math.BigInteger;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/Contract<br/>d98e<br/>edBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* RSA非对称式加密与解密
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MarkerKing
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version v1.0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @time 2007-2-23 21:19
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class RSAUtil
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 加密方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 需要加密的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return String 加密后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String S2RSA(String str) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得公匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPublicKey rsapuk = RSAKey.getPublicKey("publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得公匙参数e,n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger e = rsapuk.getPublicExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger n = rsapuk.getModulus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 转换明文m
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] btext = str.getBytes("GB2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger m = new BigInteger(btext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 计算密文
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger c = m.modPow(e, n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 返回
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return c.toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 解密方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 私匙文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 加密后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return String 解密后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String RSA2S(String fileName, String rsaStr) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPrivateKey rsaprk = RSAKey.getPrivateKey(fileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取私匙参数d,n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger d = rsaprk.getPrivateExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger n = rsaprk.getModulus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取密文
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger c = new BigInteger(rsaStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 解密计算
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger m = c.modPow(d, n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 转换成String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] ctext = m.toByteArray();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new String(ctext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.markerking;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.FileOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.ObjectInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.ObjectOutputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.KeyPair;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.KeyPairGenerator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.PrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.PublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public abstract class RSAKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 生成2048位的RSA公匙和私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void generator(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String privateKeyFileName) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
kpg.initialize(2048);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
KeyPair kp = kpg.generateKeyPair();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PublicKey puk = kp.getPublic();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrivateKey prk = kp.getPrivate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos1 = new FileOutputStream("publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos2 = new FileOutputStream(privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos1.writeObject(puk);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos2.writeObject(prk);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos1.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos2.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fos1.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fos2.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("公匙生成成功!公匙文件为publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("私匙生成成功!私匙文件为" + privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 获取已经生成的公匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return RSAPublicKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static RSAPublicKey getPublicKey(String publicKeyFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(publicKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPublicKey rsapuk = (RSAPublicKey) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return rsapuk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 获取已经生成的私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return RSAPrivateKey
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected static RSAPrivateKey getPrivateKey(String privateKeyFileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(privateKeyFileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPrivateKey psaprk = (RSAPrivateKey) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return psaprk;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
调用方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package org.markerking;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.math.BigInteger;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPrivateKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.security.interfaces.RSAPublicKey;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/Contract<br/>d98e<br/>edBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* RSA非对称式加密与解密
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author MarkerKing
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @version v1.0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @time 2007-2-23 21:19
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class RSAUtil
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 加密方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 需要加密的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return String 加密后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String S2RSA(String str) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得公匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPublicKey rsapuk = RSAKey.getPublicKey("publicRSAKey.dat");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得公匙参数e,n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger e = rsapuk.getPublicExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger n = rsapuk.getModulus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 转换明文m
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] btext = str.getBytes("GB2312");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger m = new BigInteger(btext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 计算密文
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger c = m.modPow(e, n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 返回
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return c.toString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 解密方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 私匙文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 加密后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return String 解密后的字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String RSA2S(String fileName, String rsaStr) throws Exception
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取私匙
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RSAPrivateKey rsaprk = RSAKey.getPrivateKey(fileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取私匙参数d,n
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger d = rsaprk.getPrivateExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger n = rsaprk.getModulus();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获取密文
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger c = new BigInteger(rsaStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 解密计算
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BigInteger m = c.modPow(d, n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 转换成String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] ctext = m.toByteArray();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new String(ctext);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- PHP rsa加密解密使用方法
- java RSA非对称加密-解密(简洁明了)
- PHP开发接口使用RSA进行加密解密方法
- ios php RSA 非对称加密解密 der 和pem生成
- 关于解决RSA非对称加密时,将密文存入数据库取出后,再对密文解密的时候抛出javax.crypto.BadPaddingException: Data must start with zero
- RSA非对称加密解密例子
- RSA不限长度非对称加密解密C#
- 漫谈iOS RSA非对称加密与解密
- JDK自带方法实现RSA非对称加密
- 漫谈iOS RSA非对称加密与解密
- android RSA加密 解密 非对称可逆加密
- CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互
- 非对称加解密——RSA加密、解密以及数字签名
- JAVA实现RSA加密解密 非对称算法
- 对文件压缩加密,解密解压缩,非对称加密,RSA,AES算法
- PHP rsa加密解密使用方法
- RSA加密的方式和解密方式实现方法(推荐)
- Java对称与非对称加密解密,AES与RSA
- JAVA 获取RSA非对称加密,私钥加密、公钥解密
- PHP的RSA加密解密方法以及开发接口使用