/java-lessur/src/lessur/util/RC4.java

https://github.com/liamzebedee/misc · Java · 169 lines · 55 code · 33 blank · 81 comment · 11 complexity · 7da125b4415bfa2511766f3a4321a754 MD5 · raw file

  1. package lessur.util;
  2. /* Copyright (C) 2003 Clarence Ho (clarence@clarenceho.net)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use of this software for non-profit, educational,
  6. * or persoanl purposes, in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * In case of using this software for other purposes not stated above,
  19. * please conact Clarence Ho (clarence@clarenceho.net) for permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY CLARENCE HO "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  24. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  28. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /**
  35. * This is a simple implementation of the RC4 (tm) encryption algorithm. The
  36. * author implemented this class for some simple applications
  37. * that don't need/want/require the Sun's JCE framework.
  38. * <p>
  39. * But if you are looking for encryption algorithms for a
  40. * full-blown application,
  41. * it would be better to stick with Sun's JCE framework. You can find
  42. * a *free* JCE implementation with RC4 (tm) at
  43. * Cryptix (http://www.cryptix.org/).
  44. * <p>
  45. * Note that RC4 (tm) is a trademark of RSA Data Security, Inc.
  46. * Also, if you are within USA, you may need to acquire licenses from
  47. * RSA to use RC4.
  48. * Please check your local law. The author is not
  49. * responsible for any illegal use of this code.
  50. * <p>
  51. * @author Clarence Ho
  52. */
  53. public class RC4 {
  54. private byte state[] = new byte[256];
  55. private int x;
  56. private int y;
  57. /**
  58. * Initializes the class with a string key. The length
  59. * of a normal key should be between 1 and 2048 bits. But
  60. * this method doens't check the length at all.
  61. *
  62. * @param key the encryption/decryption key
  63. */
  64. public RC4(String key) throws NullPointerException {
  65. this(key.getBytes());
  66. }
  67. /**
  68. * Initializes the class with a byte array key. The length
  69. * of a normal key should be between 1 and 2048 bits. But
  70. * this method doens't check the length at all.
  71. *
  72. * @param key the encryption/decryption key
  73. */
  74. public RC4(byte[] key) throws NullPointerException {
  75. for (int i=0; i < 256; i++) {
  76. state[i] = (byte)i;
  77. }
  78. x = 0;
  79. y = 0;
  80. int index1 = 0;
  81. int index2 = 0;
  82. byte tmp;
  83. if (key == null || key.length == 0) {
  84. throw new NullPointerException();
  85. }
  86. for (int i=0; i < 256; i++) {
  87. index2 = ((key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
  88. tmp = state[i];
  89. state[i] = state[index2];
  90. state[index2] = tmp;
  91. index1 = (index1 + 1) % key.length;
  92. }
  93. }
  94. /**
  95. * RC4 encryption/decryption.
  96. *
  97. * @param data the data to be encrypted/decrypted
  98. * @return the result of the encryption/decryption
  99. */
  100. public byte[] rc4(String data) {
  101. if (data == null) {
  102. return null;
  103. }
  104. byte[] tmp = data.getBytes();
  105. this.rc4(tmp);
  106. return tmp;
  107. }
  108. /**
  109. * RC4 encryption/decryption.
  110. *
  111. * @param buf the data to be encrypted/decrypted
  112. * @return the result of the encryption/decryption
  113. */
  114. public byte[] rc4(byte[] buf) {
  115. //int lx = this.x;
  116. //int ly = this.y;
  117. int xorIndex;
  118. byte tmp;
  119. if (buf == null) {
  120. return null;
  121. }
  122. byte[] result = new byte[buf.length];
  123. for (int i=0; i < buf.length; i++) {
  124. x = (x + 1) & 0xff;
  125. y = ((state[x] & 0xff) + y) & 0xff;
  126. tmp = state[x];
  127. state[x] = state[y];
  128. state[y] = tmp;
  129. xorIndex = ((state[x] &0xff) + (state[y] & 0xff)) & 0xff;
  130. result[i] = (byte)(buf[i] ^ state[xorIndex]);
  131. }
  132. //this.x = lx;
  133. //this.y = ly;
  134. return result;
  135. }
  136. }