importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.security.Key;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;importjava.util.HashMap;importjava.util.Map;importjavax.crypto.Cipher;importjavax.crypto.CipherInputStream;importjavax.crypto.CipherOutputStream;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;/*** RSA加解密方法。
*
*@authoryangw
*@since1.0.0*/
public classRSACode {/**加解密算法关键字*/
public static final String KEY_ALGORITHM = "RSA";/**公钥关键字*/
private static final String PUBLIC_KEY = "RSAPublicKey";/**私钥关键字*/
private static final String PRIVATE_KEY = "RSAPrivateKey";/** *//*** RSA最大加密明文大小*/
private static final int MAX_ENCRYPT_BLOCK = 117;/** *//*** RSA最大解密密文大小*/
private static final int MAX_DECRYPT_BLOCK = 128;/*** 私钥解密。
*@paramdata 对应公钥加密后的密文。
*@paramkeyBytes 私钥。
*@return明文。*/
public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes) throwsException {//取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据解密
Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);returncipher.doFinal(data);
}/*** 公钥解密。
*@paramdata 对应私钥加密后的密文。
*@paramkeyBytes 公钥。
*@return明文。*/
public static byte[] decryptByPublicKey(byte[] data, byte[] keyBytes) throwsException {//取得公钥
X509EncodedKeySpec x509KeySpec = newX509EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey=keyFactory.generatePublic(x509KeySpec);//对数据解密
Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);returncipher.doFinal(data);
}/*** 公钥加密。
*@paramdata 明文。
*@paramkeyBytes 公钥。
*@return密文。*/
public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes) throwsException {//取得公钥
X509EncodedKeySpec x509KeySpec = newX509EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey=keyFactory.generatePublic(x509KeySpec);//对数据加密
Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);returncipher.doFinal(data);
}/*** 私钥加密。
*@paramdata 明文。
*@paramkeyBytes 私钥。
*@return密文。*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] keyBytes) throwsException {//取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据加密
Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);returncipher.doFinal(data);
}/*** 取得私钥。*/
public static byte[] getPrivateKey(Map keyMap) throwsException {
Key key=(Key) keyMap.get(PRIVATE_KEY);returnkey.getEncoded();
}/*** 取得公钥。*/
public static byte[] getPublicKey(MapkeyMap)throwsException {
Key key=(Key) keyMap.get(PUBLIC_KEY);returnkey.getEncoded();
}/*** 初始化密钥。*/
public static Map initKey() throwsNoSuchAlgorithmException {
KeyPairGenerator keyPairGen=KeyPairGenerator .getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair=keyPairGen.generateKeyPair();
RSAPublicKey publicKey= (RSAPublicKey) keyPair.getPublic(); //公钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //私钥
Map keyMap = new HashMap(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);returnkeyMap;
}/*** 将密钥用BASE64加密成字符形式。*/
public static String encryptBASE64(byte[] encoded){if(encoded==null){return null;
}returnBase64Code.encodeToString(encoded);
}/*** 将以BASE64加密的密钥还原为字节数组。*/
public static byte[] decryptBASE64(String key) throwsIOException{if(key==null){return null;
}returnBase64Code.decodeFromString(key);
}/*** 使用私钥进行分段加密
*@paramdataStr 要加密的数据
*@return公钥base64字符串
*@throwsException*/
public static byte[] encryptByPublicKeyToFile(byte[] filebyte,byte[] key)throwsException {//要加密的数据
PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据加密
Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);int inputLen =filebyte.length;
ByteArrayOutputStream out= newByteArrayOutputStream();int offSet = 0;byte[] cache;int i = 0;//对数据分段加密
while (inputLen - offSet > 0) {if (inputLen - offSet >MAX_ENCRYPT_BLOCK) {
cache=cipher.doFinal(filebyte, offSet, MAX_ENCRYPT_BLOCK);
}else{
cache= cipher.doFinal(filebyte, offSet, inputLen -offSet);
}
out.write(cache,0, cache.length);
i++;
offSet= i *MAX_ENCRYPT_BLOCK;
}byte[] encryptedData =out.toByteArray();
out.close();returnencryptedData;
}/*** 使用公钥进行分段解密
*@paramdataStr 使用base64处理过的密文
*@return解密后的数据
*@throwsException*/
public static byte[] decryptByPrivateKeyFile(byte[] dataStr ,byte[] pubKey)throwsException {
X509EncodedKeySpec x509KeySpec= newX509EncodedKeySpec(pubKey);
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey=keyFactory.generatePublic(x509KeySpec);
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);int inputLen =dataStr.length;
ByteArrayOutputStream out= newByteArrayOutputStream();int offSet = 0;byte[] cache;int i = 0;//对数据分段解密
while (inputLen - offSet > 0) {if (inputLen - offSet >MAX_DECRYPT_BLOCK) {
cache=cipher.doFinal(dataStr, offSet, MAX_DECRYPT_BLOCK);
}else{
cache= cipher.doFinal(dataStr, offSet, inputLen -offSet);
}
out.write(cache,0, cache.length);
i++;
offSet= i *MAX_DECRYPT_BLOCK;
}byte[] decryptedData =out.toByteArray();
out.close();returndecryptedData;
}
}