ARCFour.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ARCFour.java --
  2. Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.prng;
  32. import gnu.java.security.Registry;
  33. import gnu.java.security.prng.BasePRNG;
  34. import gnu.java.security.prng.LimitReachedException;
  35. import java.util.Map;
  36. /**
  37. * RC4 is a stream cipher developed by Ron Rivest. Until 1994 RC4 was a trade
  38. * secret of RSA Data Security, Inc., when it was released anonymously to a
  39. * mailing list. This version is a descendent of that code, and since there is
  40. * no proof that the leaked version was in fact RC4 and because "RC4" is a
  41. * trademark, it is called "ARCFOUR", short for "Allegedly RC4".
  42. * <p>
  43. * This class only implements the <i>keystream</i> of ARCFOUR. To use this as a
  44. * stream cipher, one would say:
  45. * <pre>
  46. * out = in &circ; arcfour.nextByte();
  47. * </pre>
  48. * <p>
  49. * This operation works for encryption and decryption.
  50. * <p>
  51. * References:
  52. * <ol>
  53. * <li>Schneier, Bruce: <i>Applied Cryptography: Protocols, Algorithms, and
  54. * Source Code in C, Second Edition.</i> (1996 John Wiley and Sons), pp.
  55. * 397--398. ISBN 0-471-11709-9</li>
  56. * <li>K. Kaukonen and R. Thayer, "A Stream Cipher Encryption Algorithm
  57. * 'Arcfour'", Internet Draft (expired), <a
  58. * href="http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt">draft-kaukonen-cipher-arcfour-03.txt</a></li>
  59. * </ol>
  60. */
  61. public class ARCFour
  62. extends BasePRNG
  63. implements Cloneable
  64. {
  65. /** The attributes property name for the key bytes. */
  66. public static final String ARCFOUR_KEY_MATERIAL = "gnu.crypto.prng.arcfour.key-material";
  67. /** The size of the internal S-box. */
  68. public static final int ARCFOUR_SBOX_SIZE = 256;
  69. /** The S-box. */
  70. private byte[] s;
  71. private byte m, n;
  72. /** Default 0-arguments constructor. */
  73. public ARCFour()
  74. {
  75. super(Registry.ARCFOUR_PRNG);
  76. }
  77. public void setup(Map attributes)
  78. {
  79. byte[] kb = (byte[]) attributes.get(ARCFOUR_KEY_MATERIAL);
  80. if (kb == null)
  81. throw new IllegalArgumentException("ARCFOUR needs a key");
  82. s = new byte[ARCFOUR_SBOX_SIZE];
  83. m = n = 0;
  84. byte[] k = new byte[ARCFOUR_SBOX_SIZE];
  85. for (int i = 0; i < ARCFOUR_SBOX_SIZE; i++)
  86. s[i] = (byte) i;
  87. if (kb.length > 0)
  88. for (int i = 0, j = 0; i < ARCFOUR_SBOX_SIZE; i++)
  89. {
  90. k[i] = kb[j++];
  91. if (j >= kb.length)
  92. j = 0;
  93. }
  94. for (int i = 0, j = 0; i < ARCFOUR_SBOX_SIZE; i++)
  95. {
  96. j = j + s[i] + k[i];
  97. byte temp = s[i];
  98. s[i] = s[j & 0xff];
  99. s[j & 0xff] = temp;
  100. }
  101. buffer = new byte[ARCFOUR_SBOX_SIZE];
  102. try
  103. {
  104. fillBlock();
  105. }
  106. catch (LimitReachedException wontHappen)
  107. {
  108. }
  109. }
  110. public void fillBlock() throws LimitReachedException
  111. {
  112. for (int i = 0; i < buffer.length; i++)
  113. {
  114. m++;
  115. n = (byte)(n + s[m & 0xff]);
  116. byte temp = s[m & 0xff];
  117. s[m & 0xff] = s[n & 0xff];
  118. s[n & 0xff] = temp;
  119. temp = (byte)(s[m & 0xff] + s[n & 0xff]);
  120. buffer[i] = s[temp & 0xff];
  121. }
  122. }
  123. }