昨天晚上,恰逢夜深人靜,正是擼代碼的好時候,忽然接到小學妹的電話,小學妹跟我一頓哭,把我給愁的,趕忙一頓關心
javascript
等學妹緩過來以後,終於瞭解了前因後果,原來學妹剛入職進公司,上級就讓她對接銀行系統接口。emmm,你們都知道,銀行系統大可能是RSA加密數據交互的,小學妹這塊徹底不懂,弄了一天尚未搞定。我一聽,嘿嘿,這下該我上場表演了,說不定十四億的愛情終於輪到我了呢。java
/** * 公鑰加密 * * @param data 源數據 * @param publicKey 公鑰 * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); // 對數據加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicK); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段加密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; } catch (Exception ex) { ex.printStackTrace(); } return null; }
/** * 私鑰解密 * * @param encryptedData 已加密數據 * @param privateKey 私鑰 * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段解密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; } catch (Exception ex) { } return null; }
重點來了,咱們進行加解密的數據長度確定是超過了128的,若是咱們不作分段加解密的話,就會出現以下所示的錯誤:git
javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344) at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389) at javax.crypto.Cipher.doFinal(Cipher.java:2223) at com.lwx.utils.RSAUtils.decryptByPrivateKey(RSAUtils.java:124) at com.lwx.utils.RSAUtils.main(RSAUtils.java:294)
下載連接: https://gitee.com/luowenxing/utils/blob/master/src/main/java/com/lwx/utils/RSAUtils.java.算法
public class RSAUtils { /** * 加密算法RSA */ public static final String RSA_KEY_ALGORITHM = "RSA"; /** * 加密算法RSA */ public static final String AES_KEY_ALGORITHM = "AES"; /** * 簽名算法 */ public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA"; /** * RSA最大加密明文大小 */ private static final int MAX_ENCRYPT_BLOCK = 117; /** * RSA最大解密密文大小 */ private static final int MAX_DECRYPT_BLOCK = 128; /** * 用私鑰對信息生成數字簽名 * * @param data 加密數據 * @param privateKey 私鑰 * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return MyBase64Utils.encodeStringForString(signature.sign()); } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * 校驗數字簽名 * * @param data 加密數據 * @param publicKey 公鑰 * @param sign 數字簽名 * @return * @throws Exception */ public static boolean verify(byte[] data, String publicKey, String sign) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(MyBase64Utils.decodeStringForByte(sign)); } catch (Exception ex) { } return false; } /** * 私鑰解密 * * @param encryptedData 已加密數據 * @param privateKey 私鑰 * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段解密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; } catch (Exception ex) { } return null; } /** * 公鑰解密 * * @param encryptedData 已加密數據 * @param publicKey 公鑰 * @return * @throws Exception */ public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段解密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; } catch (Exception ex) { } return null; } /** * 公鑰加密 * * @param data 源數據 * @param publicKey 公鑰 * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); // 對數據加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicK); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段加密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; } catch (Exception ex) { ex.printStackTrace(); } return null; } /** * 私鑰加密 * * @param data 源數據 * @param privateKey 私鑰 * @return * @throws Exception */ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) { try { byte[] keyBytes = MyBase64Utils.decodeStringForByte(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateK); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段加密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; }catch (Exception e){ } return null; } //生成密鑰對 public static KeyPair genKeyPair(int keyLength) throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.generateKeyPair(); } }
到這裏呢,就搞定了。emmmmm,固然只搞定了代碼,小學妹拿着代碼就溜了,唉!你們三連安慰下我這受傷的當心臟吧。markdown