不废话,code:
public class RsaProvider {
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
private static final int PubKeyHeadOffset=4;
private static final int iPubKeyLen=0x87;
private static String CLASS_NAME = new Exception().getStackTrace()[0].getClassName();
private static String TAG = "<"+CLASS_NAME+">";
private static boolean isDebug=true;
public static PublicKey getPublickKey(String modulus, String publicExponent)
throws Exception {
KeySpec publicKeySpec = new RSAPublicKeySpec(
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(publicKeySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String publicExponent, String privateExponent)
throws Exception {
KeySpec privateKeySpec = new RSAPrivateKeySpec(
new BigInteger(publicExponent, 16), new BigInteger(privateExponent, 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
return privateKey;
}
public static PrivateKey getPrivateKey(String modulus, String publicExponent, String privateExponent, String primeP, String primeQ, String primeExponentP, String primeExponentQ, String crtCoefficient)
throws Exception {
KeySpec privateKeySpec = new RSAPrivateCrtKeySpec(
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16), new BigInteger(privateExponent, 16), new BigInteger(primeP, 16), new BigInteger(primeQ, 16), new BigInteger(primeExponentP, 16), new BigInteger(primeExponentQ, 16), new BigInteger(crtCoefficient, 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
return privateKey;
}
public static String RsaPubKeyEncrypt(byte[] data, String modulus, Byte mode)
{
PublicKey publicKey=null;
String strResult=null;
String publicExponent = "010001";
try {
publicKey= getPublickKey(modulus,publicExponent);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
byte[] enBytes=null;
try
{
Cipher cipher = Cipher.getInstance("RSA");
if(mode == 0)
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
else if(mode ==1)
cipher.init(Cipher.DECRYPT_MODE, publicKey);
enBytes = cipher.doFinal(data);
}
catch(Exception e)
{
e.printStackTrace();
}
strResult=PubUtils.HexTostr(enBytes);
return strResult;
}
public static String RsaPrivateKeyEncrypt(byte[] data, Byte mode, PrivateKey privateKey)
{
String strResult=null;
byte[] enBytes=null;
try
{
Cipher cipher = Cipher.getInstance("RSA");
if(mode == 0)
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
else if(mode ==1)
cipher.init(Cipher.DECRYPT_MODE, privateKey);
enBytes = cipher.doFinal(data);
}
catch(Exception e)
{
e.printStackTrace();
}
strResult=PubUtils.HexTostr(enBytes);
return strResult;
}
/**
* ȡ��˽Կ
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* ȡ�ù�Կ
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map<String, Object> keyMap)
throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
/**
* ��ʼ����Կ
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
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(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static String encryptBASE64(byte[] src)
{
String strResult=null;
return strResult;
}
}
其中使用时,公钥加解密方法为
RsaPubKeyEncrypt();参数1:待加解密数据;2:公钥;3:int 0/1 0表示加密 1表示解密
私钥加解密方法为
RsaPrivateKeyEncrypt();参数1:待加解密数据;2:int 0/1 0表示加密 1表示解密; 3:privateKey对象
privateKey对象通过getPrivateKey()方法获取;参数为生成的公钥数据和私钥的Exponent及其他字段
生成公私密钥对方式:
KeyPairGenerator keyPairGen = null;
try {
keyPairGen = KeyPairGenerator
.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.genKeyPair();
//
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) keyPair.getPrivate();
//私钥
String de = privateKey.getPublicExponent().toString(16);
String p = privateKey.getPrimeP().toString(16);
String q = privateKey.getPrimeQ().toString(16);
String dp = privateKey.getPrimeExponentP().toString(16);
String dq = privateKey.getPrimeExponentQ().toString(16);
String qinv = privateKey.getCrtCoefficient().toString(16);
//公钥
String pubmodulus = publicKey.getModulus().toString(16);
String pub_E = publicKey.getPublicExponent().toString(16);
RSA公私密钥都可加解密,具体看需求