Unsupported library containing some previously-useful java methods
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ArcFourEncrypter.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2009 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.util.crypto;
  23. import java.util.Arrays;
  24. /**
  25. * Implements the ARC4 algorithm.
  26. *
  27. * @author chris
  28. */
  29. public class ArcFourEncrypter {
  30. /** The key to use for encryption. */
  31. final String key;
  32. /** The output of the key scheduling algorithm. */
  33. final byte[] ostate = new byte[256];
  34. /**
  35. * Creates a new encrypter that will use the specified key.
  36. *
  37. * @param key The key to use for encrypting
  38. */
  39. public ArcFourEncrypter(final String key) {
  40. this.key = key;
  41. byte[] bytes = key.getBytes();
  42. for (int i = 0; i < 256; i++) {
  43. ostate[i] = (byte) i;
  44. }
  45. int keyindex = 0;
  46. int stateindex = 0;
  47. for (int i = 0; i < 256; i++) {
  48. stateindex = (stateindex + bytes[keyindex] + ostate[i]) & 0xff;
  49. final byte u = ostate[stateindex];
  50. ostate[stateindex] = (byte) (ostate[i] & 0xff);
  51. ostate[i] = (byte) (u & 0xff);
  52. keyindex = (keyindex + 1) % bytes.length;
  53. }
  54. }
  55. /**
  56. * Encrypts the specified bytes.
  57. *
  58. * @param bytes The bytes to be encrypted
  59. * @return The encrypted bytes
  60. */
  61. public byte[] encrypt(final byte[] bytes) {
  62. final byte[] state = Arrays.copyOf(ostate, ostate.length);
  63. final byte[] dest = new byte[bytes.length];
  64. int x = 0, y = 0;
  65. for (int i = 0; i < bytes.length; i++) {
  66. x = (x + 1) & 0xff;
  67. byte sx = state[x];
  68. y = (sx + y) & 0xff;
  69. byte sy = state[y];
  70. state[y] = (byte) (sx & 0xff);
  71. state[x] = (byte) (sy & 0xff);
  72. dest[i] = (byte) (((int) bytes[i] ^ state[(sx + sy) & 0xff]) & 0xff);
  73. }
  74. return dest;
  75. }
  76. }