rsa.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. > Java 解密 Demo
  2. import javax.crypto.Cipher;
  3. import java.io.ByteArrayOutputStream;
  4. import java.security.*;
  5. import java.security.interfaces.RSAPrivateKey;
  6. import java.security.interfaces.RSAPublicKey;
  7. import java.security.spec.PKCS8EncodedKeySpec;
  8. import java.security.spec.X509EncodedKeySpec;
  9. import java.util.Base64;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. public class RSADecrypt {
  13. /**
  14. * 签名算法
  15. * java.security.Signature#signatureInfo
  16. *
  17. * signatureInfo.put("sun.security.rsa.RSASignature$MD2withRSA", TRUE);
  18. * signatureInfo.put("sun.security.rsa.RSASignature$MD5withRSA", TRUE);
  19. * signatureInfo.put("sun.security.rsa.RSASignature$SHA1withRSA", TRUE);
  20. * signatureInfo.put("sun.security.rsa.RSASignature$SHA256withRSA", TRUE);
  21. * signatureInfo.put("sun.security.rsa.RSASignature$SHA384withRSA", TRUE);
  22. * signatureInfo.put("sun.security.rsa.RSASignature$SHA512withRSA", TRUE);
  23. */
  24. public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
  25. public static final String KEY_ALGORITHM = "RSA";
  26. private static final int MAX_DECRYPT_BLOCK = 256;
  27. /**
  28. * 私钥解密
  29. *
  30. * @param encryptedData 已加密数据
  31. * @param privateKey 私钥(BASE64编码)
  32. */
  33. public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
  34. byte[] keyBytes = base64Decode(privateKey);
  35. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  36. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  37. Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  38. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  39. cipher.init(Cipher.DECRYPT_MODE, privateK);
  40. return segmented(cipher, encryptedData, MAX_DECRYPT_BLOCK);
  41. }
  42. /**
  43. * 公钥解密
  44. *
  45. * @param encryptedData 已加密数据
  46. * @param publicKey 公钥(BASE64编码)
  47. */
  48. public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
  49. byte[] keyBytes = base64Decode(publicKey);
  50. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  51. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  52. Key publicK = keyFactory.generatePublic(x509KeySpec);
  53. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  54. cipher.init(Cipher.DECRYPT_MODE, publicK);
  55. return segmented(cipher, encryptedData, MAX_DECRYPT_BLOCK);
  56. }
  57. /**
  58. * 分段处理加解密避免JDK的
  59. * Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
  60. *
  61. * @param cipher 加解密的密码对象
  62. * @param data 加解密的数据
  63. * @param MAX_BLOCK 最大处理长度
  64. */
  65. private static byte[] segmented(Cipher cipher, byte[] data, final int MAX_BLOCK) throws Exception {
  66. int inputLen = data.length;
  67. byte[] cache;
  68. ByteArrayOutputStream out = new ByteArrayOutputStream();
  69. for (int i = 0, offSet = 0; inputLen - offSet > 0; i++, offSet = i * MAX_BLOCK) {
  70. if (inputLen - offSet > MAX_BLOCK) {
  71. cache = cipher.doFinal(data, offSet, MAX_BLOCK);
  72. } else {
  73. cache = cipher.doFinal(data, offSet, inputLen - offSet);
  74. }
  75. out.write(cache, 0, cache.length);
  76. }
  77. byte[] encryptedData = out.toByteArray();
  78. out.close();
  79. return encryptedData;
  80. }
  81. /**
  82. * base64转码
  83. */
  84. public static String base64ToString(byte[] target) {
  85. return Base64.getEncoder().encodeToString(target);
  86. }
  87. /**
  88. * base64解码
  89. */
  90. public static byte[] base64Decode(String target) {
  91. return Base64.getDecoder().decode(target);
  92. }
  93. public static void main (String[] args) {
  94. String dataString = new String(decryptByPrivateKey(DataCenterRSA.base64Decode(data), PRIVATE_KEY));
  95. }
  96. }