由于项目安全考虑,需要对关键数据进行加密;本文就RSA加密进行再次学习,本文就文中的工具类进行简单说明;如有不足之处,希望大家予以反馈,便于互相学习;
先说下RSA加密的优缺点:
优点:安全性好。非对称加密使用一对秘钥,一个用来加密(publicKey),一个用来解密(privateKey);而且公钥(publicKey)是公开的(前后台的公钥是一致的),私钥只在需要做解密的一端保存(一般来讲Android端只做加密,故Android无需保留privateKey),而另一端出于安全考虑,不需要保存,不需要像对称加密(AES)那样,在前后台交互之前需要对秘钥进行同步;
缺点:加解密花费时间较长,速度慢,只适合对少量数据进行加密(运用场景例如:登陆、修改密码等);秘钥长度越长,安全性能越好,但是解析时长也会随之下降;
秘钥长度一般为:512-2024,低于512的也存在安全风险;
以下是一个Android 端工具类,可拿去用;
/**
* RSA算法加密/解密工具类。
*/
public abstract class RSAUtils {
/** 算法名称 */
private static final String ALGORITHOM = "RSA";
/** 密钥大小 */
private static final int KEY_SIZE = 2048;
/** 默认的安全服务提供者 */
private static final Provider DEFAULT_PROVIDER = new BouncyCastleProvider();
private static KeyPairGenerator keyPairGen = null;
private static KeyFactory keyFactory = null;
/** 缓存的密钥对。 */
private static KeyPair oneKeyPair = null;
private static File rsaPairFile = null;
static {
try {
keyPairGen = KeyPairGenerator.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
keyFactory = KeyFactory.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
} catch (NoSuchAlgorithmException ex) {
Logger.e(ex.getMessage());
}
}
private RSAUtils() {
}
/**
* 生成并返回RSA密钥对。
*/
private static synchronized KeyPair generateKeyPair() {
try {
keyPairGen.initialize(KEY_SIZE, new SecureRandom(DateFormatUtils.format(System.currentTimeMillis(),"yyyyMMddhhmmss").getBytes()));
oneKeyPair = keyPairGen.generateKeyPair();
saveKeyPair(oneKeyPair);
return oneKeyPair;
} catch (InvalidParameterException ex) {
Logger.e("KeyPairGenerator does not support a key length of " + KEY_SIZE + ".");
} catch (NullPointerException ex) {
Logger.e("RSAUtils#KEY_PAIR_GEN is null, can not generate KeyPairGenerator instance.");
}
return null;
}
/**
* 若需要创建新的密钥对文件,则返回 {@code true},否则 {@code false}。
*/
private static boolean isCreateKeyPairFile() {
// 是否创建新的密钥对文件
boolean createNewKeyPair = false;
if (!rsaPairFile.exists() || rsaPairFile.isDirectory()) {
createNewKeyPair = true;
}
return createNewKeyPair;
}
/**
* 将指定的RSA密钥对以文件形式保存。
*
* @param keyPair 要保存的密钥对。
*/
private static void saveKeyPair(KeyPair keyPair) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = FileUtils.openOutputStream(rsaPairFile);
oos = new ObjectOutputStream(fos);
oos.writeObject(keyPair);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(oos);
IOUtils.closeQuietly(fos);
}
}
/**
* 返回RSA密钥对。
*/
private static KeyPair getKeyPair() {
// 首先判断是否需要重新生成新的密钥对文件
if (isCreateKeyPairFile()) {
// 直接强制生成密钥对文件,并存入缓存。
return generateKeyPair();
}
if (oneKeyPair != null) {
return oneKeyPair;
}
return readKeyPair();
}
// 同步读出保存的密钥对
private static KeyPair readKeyPair() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = FileUtils.openInputStream(rsaPairFile);
ois = new ObjectInputStream(fis);
oneKeyPair = (KeyPair) ois.readObject();
return oneKeyPair;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(ois);
IOUtils.closeQuietly(fis);
}
return null;
}
/**
* 根据给定的系数和专用指数构造一个RSA专用的公钥对象。
*
* @param modulus 系数。
* @param publicExponent 专用指数。
* @return RSA专用公钥对象。
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) {
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(modulus),
new BigInteger(publicExponent));
try {
return (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);
} catch (InvalidKeySpecException ex) {
Logger.e("RSAPublicKeySpec is unavailable.");
} catch (NullPointerException ex) {
Logger.e("RSAUtils#KEY_FACTORY is null, can not generate KeyFactory instance.");
}
return null;
}
/**
* 根据给定的系数和专用指数构造一个RSA专用的私钥对象。
*
* @param modulus 系数。
* @param privateExponent 专用指数。
* @return RSA专用私钥对象。
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) {
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus),
new BigInteger(privateExponent));
try {
return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
} catch (InvalidKeySpecException ex) {
Logger.e("RSAPrivateKeySpec is unavailable.");
} catch (NullPointerException ex) {
Logger.e("RSAUtils#KEY_FACTORY is null, can not generate KeyFactory instance.");
}
return null;
}
/**
* 根据给定的16进制系数和专用指数字符串构造一个RSA专用的私钥对象。
* @return RSA专用私钥对象。
*/
public static RSAPrivateKey getRSAPrivateKey(String hexModulus, String hexPrivateExponent) {
if(StringUtils.isBlank(hexModulus) || StringUtils.isBlank(hexPrivateExponent)) {
return null;
}
byte[] modulus = null;
byte[] privateExponent = null;
try {
modulus = Hex.decodeHex(hexModulus.toCharArray());
privateExponent = Hex.decodeHex(hexPrivateExponent.toCharArray());
} catch(DecoderException ex) {
Logger.e("hexModulus or hexPrivateExponent value is invalid. return null(RSAPrivateKey).");
}
if(modulus != null && privateExponent != null) {
return generateRSAPrivateKey(modulus, privateExponent);
}
return null;
}
/**
* 根据给定的16进制系数和专用指数字符串构造一个RSA专用的公钥对象。
* @return RSA专用公钥对象。
*/
public static RSAPublicKey getRSAPublidKey(String hexModulus, String hexPublicExponent) {
if(StringUtils.isBlank(hexModulus) || StringUtils.isBlank(hexPublicExponent)) {
return null;
}
byte[] modulus = null;
byte[] publicExponent = null;
try {
modulus = Hex.decodeHex(hexModulus.toCharArray());
publicExponent = Hex.decodeHex(hexPublicExponent.toCharArray());
} catch(DecoderException ex) {
Logger.e("hexModulus or hexPublicExponent value is invalid. return null(RSAPublicKey).");
}
if(modulus != null && publicExponent != null) {
return generateRSAPublicKey(modulus, publicExponent);
}
return null;
}
/**
* 使用指定的公钥加密数据。
*
* @param publicKey 给定的公钥。
* @param data 要加密的数据。
* @return 加密后的数据。
*/
private static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
ci.init(Cipher.ENCRYPT_MODE, publicKey);
return ci.doFinal(data);
}
/**
* 使用指定的私钥解密数据。
*
* @param privateKey 给定的私钥。
* @param data 要解密的数据。
* @return 原数据。
*/
private static byte[] decrypt(PrivateKey privateKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER);
ci.init(Cipher.DECRYPT_MODE, privateKey);
return ci.doFinal(data);
}
/**
* 使用给定的公钥加密给定的字符串。
* <p />
* 若 {@code publicKey} 为 {@code null},或者 {@code plaintext} 为 {@code null} 则返回 {@code
* null}。
*
* @param publicKey 给定的公钥。
* @param plaintext 字符串。
* @return 给定字符串的密文。
*/
public static String encryptString(PublicKey publicKey, String plaintext) {
if (publicKey == null || plaintext == null) {
return null;
}
byte[] data = plaintext.getBytes();
try {
byte[] en_data = encrypt(publicKey, data);
return new String(Hex.encodeHex(en_data));
} catch (Exception ex) {
Logger.e("RSA Encryption failed");
}
return null;
}
/**
* 使用默认的公钥加密给定的字符串。
* <p />
* 若{@code plaintext} 为 {@code null} 则返回 {@code null}。
*
* @param plaintext 字符串。
* @return 给定字符串的密文。
*/
public static String encryptString(String plaintext) {
if(plaintext == null) {
return null;
}
byte[] data = plaintext.getBytes();
KeyPair keyPair = getKeyPair();
try {
byte[] en_data = encrypt((RSAPublicKey)keyPair.getPublic(), data);
return new String(Hex.encodeHex(en_data));
} catch(NullPointerException ex) {
Logger.e("keyPair cannot be null.");
} catch(Exception ex) {
Logger.e("RSA Encryption failed");
}
return null;
}
/**
* 使用给定的私钥解密给定的字符串。
* <p />
* 若私钥为 {@code null},或者 {@code encrypttext} 为 {@code null}或空字符串则返回 {@code null}。
* 私钥不匹配时,返回 {@code null}。
*
* @param privateKey 给定的私钥。
* @param encrypttext 密文。
* @return 原文字符串。
*/
public static String decryptString(PrivateKey privateKey, String encrypttext) {
if (privateKey == null || StringUtils.isBlank(encrypttext)) {
return null;
}
try {
byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());
byte[] data = decrypt(privateKey, en_data);
return new String(data, StandardCharsets.UTF_8);
} catch (Exception ex) {
Logger.e("RSA Decryption failed");
}
return null;
}
/**
* 使用默认的私钥解密给定的字符串。
* <p />
* 若{@code encrypttext} 为 {@code null}或空字符串则返回 {@code null}。
* 私钥不匹配时,返回 {@code null}。
*
* @param encrypttext 密文。
* @return 原文字符串。
*/
public static String decryptString(String encrypttext) {
if(StringUtils.isBlank(encrypttext)) {
return null;
}
KeyPair keyPair = getKeyPair();
try {
byte[] en_data = Hex.decodeHex(encrypttext.toCharArray());
byte[] data = decrypt(keyPair.getPrivate(), en_data);
return new String(data);
} catch(NullPointerException ex) {
Logger.e("keyPair cannot be null.");
} catch (Exception ex) {
Logger.e("RSA Decryption failed");
}
return null;
}
/**
* 使用默认的私钥解密由JS加密(使用此类提供的公钥加密)的字符串。
*
* @param encrypttext 密文。
* @return {@code encrypttext} 的原文字符串。
*/
public static String decryptStringByJs(String encrypttext) {
String text = decryptString(encrypttext);
if(text == null) {
return null;
}
return Base64Util.decodeBase64String(StringUtils.reverse(text));
}
/** 返回已初始化的默认的公钥。*/
public static RSAPublicKey getDefaultPublicKey() {
KeyPair keyPair = getKeyPair();
if(keyPair != null) {
return (RSAPublicKey)keyPair.getPublic();
}
return null;
}
/** 返回已初始化的默认的私钥。*/
public static RSAPrivateKey getDefaultPrivateKey() {
KeyPair keyPair = getKeyPair();
if(keyPair != null) {
return (RSAPrivateKey)keyPair.getPrivate();
}
return null;
}
}
在项目中的使用:
如果要测试解密,发现解密后输出结果是乱码,需要需要注意以下几点:
1、如Utf-8不行,可以试试别的,如GBK或者gb-2312
2、填充方式是,本示例后也用RSA,前端也用RSA是,没问题的;
android系统的RSA实现是"RSA/None/NoPadding",而标准JDK实现是"RSA/None/PKCS1Padding" ,这造成了在android机上加密后无法在服务器上解密的原因,所以在实现的时候这个一定要注意。需要后台更新或者替换相关jar包;