RsaUtil.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package org.example;
  2. import javax.crypto.Cipher;
  3. import java.io.BufferedReader;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.security.Key;
  8. import java.security.KeyFactory;
  9. import java.security.spec.PKCS8EncodedKeySpec;
  10. import java.security.spec.X509EncodedKeySpec;
  11. public class RsaUtil { /** */
  12. /**
  13. * 加密算法RSA
  14. */
  15. public static final String RSA = "RSA";
  16. /**
  17. * RSA最大加密明文大小
  18. */
  19. private static final int MAX_ENCRYPT_BLOCK = 117;
  20. /**
  21. * RSA最大解密密文大小
  22. */
  23. private static final int MAX_DECRYPT_BLOCK = 128;
  24. /**
  25. * <p>
  26. * 公钥加密
  27. * </p>
  28. *
  29. * @param data 源数据
  30. * @param publicKey 公钥(BASE64编码)
  31. * @return
  32. * @throws Exception
  33. */
  34. public static byte[] encryptByPublicKey(byte[] data, String publicKey)
  35. throws Exception {
  36. byte[] keyBytes = new BASE64Decoder().decodeBuffer(publicKey);
  37. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  38. KeyFactory keyFactory = KeyFactory.getInstance(RSA);
  39. Key publicK = keyFactory.generatePublic(x509KeySpec);
  40. // 对数据加密
  41. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  42. cipher.init(Cipher.ENCRYPT_MODE, publicK);
  43. int inputLen = data.length;
  44. ByteArrayOutputStream out = new ByteArrayOutputStream();
  45. int offSet = 0;
  46. byte[] cache;
  47. int i = 0;
  48. // 对数据分段加密
  49. while (inputLen - offSet > 0) {
  50. if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
  51. cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
  52. } else {
  53. cache = cipher.doFinal(data, offSet, inputLen - offSet);
  54. }
  55. out.write(cache, 0, cache.length);
  56. i++;
  57. offSet = i * MAX_ENCRYPT_BLOCK;
  58. }
  59. byte[] encryptedData = out.toByteArray();
  60. out.close();
  61. return encryptedData;
  62. }
  63. /**
  64. * <P>
  65. * 私钥解密
  66. * </p>
  67. *
  68. * @param encryptedData 已加密数据
  69. * @param privateKey 私钥(BASE64编码)
  70. * @return
  71. * @throws Exception
  72. */
  73. public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
  74. throws Exception {
  75. byte[] keyBytes = new BASE64Decoder().decodeBuffer(privateKey);
  76. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  77. KeyFactory keyFactory = KeyFactory.getInstance(RSA);
  78. Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  79. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  80. cipher.init(Cipher.DECRYPT_MODE, privateK);
  81. int inputLen = encryptedData.length;
  82. ByteArrayOutputStream out = new ByteArrayOutputStream();
  83. int offSet = 0;
  84. byte[] cache;
  85. int i = 0;
  86. // 对数据分段解密
  87. while (inputLen - offSet > 0) {
  88. if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
  89. cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
  90. } else {
  91. cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
  92. }
  93. out.write(cache, 0, cache.length);
  94. i++;
  95. offSet = i * MAX_DECRYPT_BLOCK;
  96. }
  97. byte[] decryptedData = out.toByteArray();
  98. out.close();
  99. return decryptedData;
  100. }
  101. public static String getKeyString(InputStream in) {
  102. try {
  103. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  104. String readLine = null;
  105. StringBuilder sb = new StringBuilder();
  106. while ((readLine = br.readLine()) != null) {
  107. if (readLine.charAt(0) == '-') {
  108. continue;
  109. } else {
  110. sb.append(readLine);
  111. sb.append('\r');
  112. }
  113. }
  114. return sb.toString();
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. return "";
  118. }
  119. }
  120. }