当前位置:   article > 正文

RSA前后台公钥私钥加密解密以及公钥私钥存储_rsa.decryptbyprivatekey对应的加密方法

rsa.decryptbyprivatekey对应的加密方法

不多说了,直接看代码吧

首先需要俩个工具类

  1. package com.example.aasd;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. /**
  6. * Created by pw on 2019/8/14 11:04
  7. * E-Mail Address: pw163.com
  8. */
  9. public class Base64 {
  10. private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  11. .toCharArray();
  12. public static String encode(byte[] data) {
  13. int start = 0;
  14. int len = data.length;
  15. StringBuffer buf = new StringBuffer(data.length * 3 / 2);
  16. int end = len - 3;
  17. int i = start;
  18. int n = 0;
  19. while (i <= end) {
  20. int d = ((((int) data[i]) & 0x0ff) << 16)
  21. | ((((int) data[i + 1]) & 0x0ff) << 8)
  22. | (((int) data[i + 2]) & 0x0ff);
  23. buf.append(legalChars[(d >> 18) & 63]);
  24. buf.append(legalChars[(d >> 12) & 63]);
  25. buf.append(legalChars[(d >> 6) & 63]);
  26. buf.append(legalChars[d & 63]);
  27. i += 3;
  28. if (n++ >= 14) {
  29. n = 0;
  30. buf.append(" ");
  31. }
  32. }
  33. if (i == start + len - 2) {
  34. int d = ((((int) data[i]) & 0x0ff) << 16)
  35. | ((((int) data[i + 1]) & 255) << 8);
  36. buf.append(legalChars[(d >> 18) & 63]);
  37. buf.append(legalChars[(d >> 12) & 63]);
  38. buf.append(legalChars[(d >> 6) & 63]);
  39. buf.append("=");
  40. } else if (i == start + len - 1) {
  41. int d = (((int) data[i]) & 0x0ff) << 16;
  42. buf.append(legalChars[(d >> 18) & 63]);
  43. buf.append(legalChars[(d >> 12) & 63]);
  44. buf.append("==");
  45. }
  46. return buf.toString();
  47. }
  48. private static int decode(char c) {
  49. if (c >= 'A' && c <= 'Z')
  50. return ((int) c) - 65;
  51. else if (c >= 'a' && c <= 'z')
  52. return ((int) c) - 97 + 26;
  53. else if (c >= '0' && c <= '9')
  54. return ((int) c) - 48 + 26 + 26;
  55. else
  56. switch (c) {
  57. case '+':
  58. return 62;
  59. case '/':
  60. return 63;
  61. case '=':
  62. return 0;
  63. default:
  64. throw new RuntimeException("unexpected code: " + c);
  65. }
  66. }
  67. /**
  68. * Decodes the given Base64 encoded String to a new byte array. The byte
  69. * array holding the decoded data is returned.
  70. */
  71. public static byte[] decode(String s) {
  72. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  73. try {
  74. decode(s, bos);
  75. } catch (IOException e) {
  76. throw new RuntimeException();
  77. }
  78. byte[] decodedBytes = bos.toByteArray();
  79. try {
  80. bos.close();
  81. bos = null;
  82. } catch (IOException ex) {
  83. System.err.println("Error while decoding BASE64: " + ex.toString());
  84. }
  85. return decodedBytes;
  86. }
  87. private static void decode(String s, OutputStream os) throws IOException {
  88. int i = 0;
  89. int len = s.length();
  90. while (true) {
  91. while (i < len && s.charAt(i) <= ' ')
  92. i++;
  93. if (i == len)
  94. break;
  95. int tri = (decode(s.charAt(i)) << 18)
  96. + (decode(s.charAt(i + 1)) << 12)
  97. + (decode(s.charAt(i + 2)) << 6)
  98. + (decode(s.charAt(i + 3)));
  99. os.write((tri >> 16) & 255);
  100. if (s.charAt(i + 2) == '=')
  101. break;
  102. os.write((tri >> 8) & 255);
  103. if (s.charAt(i + 3) == '=')
  104. break;
  105. os.write(tri & 255);
  106. i += 4;
  107. }
  108. }
  109. }
  1. package com.example.aasd;
  2. import java.security.KeyFactory;
  3. import java.security.KeyPair;
  4. import java.security.Key
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/550969
推荐阅读
相关标签
  

闽ICP备14008679号