当前位置:   article > 正文

java中base64解密(加密)工具类_jframe编写一个base64加密解密工具

jframe编写一个base64加密解密工具

java加密解密工具类

  1. package com.test.common;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. public class Base64Util {
  6. private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
  7. private static void decode(String s, OutputStream os) throws IOException {
  8. int i = 0;
  9. int len = s.length();
  10. while (true) {
  11. while (i < len && s.charAt(i) <= ' ')
  12. i++;
  13. if (i == len)
  14. break;
  15. int tri = (decode(s.charAt(i)) << 18)
  16. + (decode(s.charAt(i + 1)) << 12)
  17. + (decode(s.charAt(i + 2)) << 6)
  18. + (decode(s.charAt(i + 3)));
  19. os.write((tri >> 16) & 255);
  20. if (s.charAt(i + 2) == '=')
  21. break;
  22. os.write((tri >> 8) & 255);
  23. if (s.charAt(i + 3) == '=')
  24. break;
  25. os.write(tri & 255);
  26. i += 4;
  27. }
  28. }
  29. /*解密一个字符串*/
  30. public static String decode(String s) {
  31. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  32. try {
  33. decode(s, bos);
  34. } catch (IOException e) {
  35. throw new RuntimeException();
  36. }
  37. byte[] decodedBytes = bos.toByteArray();
  38. try {
  39. bos.close();
  40. bos = null;
  41. } catch (IOException ex) {
  42. System.err.println("Error while decoding BASE64: " + ex.toString());
  43. }
  44. return new String(decodedBytes);
  45. }
  46. private static int decode(char c) {
  47. if (c >= 'A' && c <= 'Z')
  48. return ((int) c) - 65;
  49. else if (c >= 'a' && c <= 'z')
  50. return ((int) c) - 97 + 26;
  51. else if (c >= '0' && c <= '9')
  52. return ((int) c) - 48 + 26 + 26;
  53. else
  54. switch (c) {
  55. case '+':
  56. return 62;
  57. case '/':
  58. return 63;
  59. case '=':
  60. return 0;
  61. default:
  62. throw new RuntimeException("unexpected code: " + c);
  63. }
  64. }
  65. /*加密一个字符串*/
  66. public static String encode(String str) {
  67. byte[] data=str.getBytes();
  68. int start = 0;
  69. int len = data.length;
  70. StringBuffer buf = new StringBuffer(data.length * 3 / 2);
  71. int end = len - 3;
  72. int i = start;
  73. int n = 0;
  74. while (i <= end) {
  75. int d = ((((int) data[i]) & 0x0ff) << 16)
  76. | ((((int) data[i + 1]) & 0x0ff) << 8)
  77. | (((int) data[i + 2]) & 0x0ff);
  78. buf.append(legalChars[(d >> 18) & 63]);
  79. buf.append(legalChars[(d >> 12) & 63]);
  80. buf.append(legalChars[(d >> 6) & 63]);
  81. buf.append(legalChars[d & 63]);
  82. i += 3;
  83. if (n++ >= 14) {
  84. n = 0;
  85. buf.append(" ");
  86. }
  87. }
  88. if (i == start + len - 2) {
  89. int d = ((((int) data[i]) & 0x0ff) << 16)
  90. | ((((int) data[i + 1]) & 255) << 8);
  91. buf.append(legalChars[(d >> 18) & 63]);
  92. buf.append(legalChars[(d >> 12) & 63]);
  93. buf.append(legalChars[(d >> 6) & 63]);
  94. buf.append("=");
  95. } else if (i == start + len - 1) {
  96. int d = (((int) data[i]) & 0x0ff) << 16;
  97. buf.append(legalChars[(d >> 18) & 63]);
  98. buf.append(legalChars[(d >> 12) & 63]);
  99. buf.append("==");
  100. }
  101. return buf.toString();
  102. }
  103. }

js加密

  1. /*base64编码加密*/
  2. function base64encode(str) {
  3. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. var base64DecodeChars = new Array(
  5. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  6. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  7. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  8. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  9. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  10. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  11. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  12. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  13. var out, i, len;
  14. var c1, c2, c3;
  15. len = str.length;
  16. i = 0;
  17. out = "";
  18. while(i < len) {
  19. c1 = str.charCodeAt(i++) & 0xff;
  20. if(i == len)
  21. {
  22. out += base64EncodeChars.charAt(c1 >> 2);
  23. out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  24. out += "==";
  25. break;
  26. }
  27. c2 = str.charCodeAt(i++);
  28. if(i == len)
  29. {
  30. out += base64EncodeChars.charAt(c1 >> 2);
  31. out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  32. out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  33. out += "=";
  34. break;
  35. }
  36. c3 = str.charCodeAt(i++);
  37. out += base64EncodeChars.charAt(c1 >> 2);
  38. out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  39. out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
  40. out += base64EncodeChars.charAt(c3 & 0x3F);
  41. }
  42. //base64编码后在按自己的算法在进行编码
  43. return out;
  44. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/202923
推荐阅读
相关标签
  

闽ICP备14008679号