当前位置:   article > 正文

ArcFour加密解密,java RC4加密解密,java 加密解密

arcfour


  1. package com.app.common.util;
  2. public class ArcFour {
  3. /**
  4. * 加密解密 参数为String 字符串(String data, String key)
  5. *
  6. * @param data
  7. * @param key
  8. * @return String
  9. * @throws UnsupportedEncodingException
  10. */
  11. public static final int vactorLen = 256;
  12. private static String encodeAndDecodeStr(String data, String key)
  13. {
  14. // 1.参数检查
  15. if (data != null && data.length() > 0 && key != null
  16. && key.length() > 0)
  17. {
  18. // 2. 初始化算法
  19. // 2.1定义矢量vactor
  20. int[] vactor = new int[vactorLen];
  21. // 2.2定义临时变量tempk
  22. byte[] tempK = new byte[vactorLen];
  23. // 2.3给vactor赋值ֵ0-255
  24. for (int i = 0; i < vactorLen; i++)
  25. vactor[i] = i;
  26. int j = 1;
  27. // 2.4循环给tempK赋值
  28. for (short i = 0; i < vactorLen; i++)
  29. {
  30. tempK[i] = (byte) key.charAt((i % key.length()));
  31. }
  32. j = 0;
  33. // 2.5 置换vactor值
  34. for (int i = 0; i < 255; i++)
  35. {
  36. j = (j + vactor[i] + tempK[i]) % vactorLen;
  37. int temp = vactor[i];
  38. vactor[i] = vactor[j];
  39. vactor[j] = temp;
  40. }
  41. int i = 0;
  42. j = 0;
  43. // 2.6把data定义成char[]数组dataChar
  44. char[] dataChar = data.toCharArray();
  45. // 2.7计算数组dataChar[]长度,用InputCharLen接收。
  46. int charLen = dataChar.length;
  47. // 2.8 定义 长度为InputCharLen的char数组iOutputChar[]
  48. char[] resultChar = new char[charLen];
  49. // 2.9 加密解密算法
  50. for (short x = 0; x < charLen; x++)
  51. {
  52. i = (i + 1) % vactorLen;
  53. j = (j + vactor[i]) % vactorLen;
  54. int temp = vactor[i];
  55. vactor[i] = vactor[j];
  56. vactor[j] = temp;
  57. char tempChar = (char) vactor[(vactor[i] + (vactor[j] % vactorLen))
  58. % vactorLen];
  59. resultChar[x] = (char) (dataChar[x] ^ tempChar);
  60. }
  61. // 3. 输出结果
  62. return new String(resultChar);
  63. }
  64. return null;
  65. }
  66. /**
  67. * 二进制转换16进制*********
  68. *
  69. * @param buf
  70. * @return
  71. */
  72. private static String parseByte2HexStr(byte bytes[])
  73. {
  74. // 参数检查
  75. if (bytes != null && bytes.length > 0)
  76. {
  77. // 定义b长度为bLenth
  78. int bLenth = bytes.length;
  79. StringBuffer stringBuffer = new StringBuffer();
  80. for (int i = 0; i < bLenth; i++)
  81. {
  82. String hex = Integer.toHexString(bytes[i] & 0xFF);
  83. if (hex.length() == 1)
  84. {
  85. hex = '0' + hex;
  86. }
  87. stringBuffer.append(hex.toUpperCase());
  88. }
  89. return stringBuffer.toString();
  90. }
  91. return null;
  92. }
  93. // 测试**************
  94. public static void main(String[] args)
  95. {
  96. // String data = "梦想还是要有的,万一实现了呢?";
  97. String data = "s.,gKsf发$@($*@¥棥梦想hai¥##@……LSLK";
  98. String key = "abcdefa";
  99. // 原文
  100. System.out.println("加密前:" + data);
  101. // 密文
  102. String str = null;
  103. if (data != null && data.length() > 0)
  104. {
  105. str = encodeAndDecodeStr(data, key);
  106. System.out.println("加密后:" + parseByte2HexStr(str.getBytes()));
  107. // 解密
  108. System.out.println("解密后:"
  109. + new String(encodeAndDecodeStr(str, key)));
  110. }
  111. }
  112. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/486732
推荐阅读
相关标签
  

闽ICP备14008679号