3002N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Implementation of the ARCFOUR cipher, an algorithm apparently compatible 0N/A * with RSA Security's RC4(tm) cipher. The description of this algorithm was 0N/A * taken from Bruce Schneier's book Applied Cryptography, 2nd ed., 0N/A * We support keys from 40 to 1024 bits. ARCFOUR would allow for keys shorter 0N/A * than 40 bits, but that is too insecure for us to permit. 0N/A * Note that we subclass CipherSpi directly and do not use the CipherCore 0N/A * framework. That was designed to simplify implementation of block ciphers 0N/A * and does not offer any advantages for stream ciphers such as ARCFOUR. 0N/A * @author Andreas Sterbenz 0N/A // state array S, 256 entries. The entries are 8-bit, but we use an int[] 0N/A // because int arithmetic is much faster than in Java than bytes. 0N/A private final int[] S;
0N/A // state indices i and j. Called is and js to avoid collision with 0N/A // local variables. 'is' is set to -1 after a call to doFinal() 0N/A // the bytes of the last key used (if any) 0N/A // we need this to re-initialize after a call to doFinal() 0N/A // called by the JCE framework 0N/A // core key setup code. initializes S, is, and js 0N/A // assumes key is non-null and between 40 and 1024 bit 0N/A // initialize S[i] to i 0N/A for (
int i =
0; i <
256; i++) {
0N/A // we avoid expanding key to 256 bytes and instead keep a separate 0N/A // counter ki = i mod key.length. 0N/A for (
int i =
0, j =
0,
ki =
0; i <
256; i++) {
0N/A // core crypt code. OFB style, so works for both encryption and decryption 0N/A // doFinal() was called, need to reset the cipher to initial state 0N/A // Modes do not make sense with stream ciphers, but allow ECB 0N/A // Padding does not make sense with stream ciphers, but allow NoPadding 0N/A // Return 0 to indicate stream cipher 0N/A // output length is always the same as input length 0N/A // no IV, return null 0N/A (
"Parameters not supported");
0N/A (
"Parameters not supported");
0N/A // init method. Check opmode and key, then call init(byte[]). 0N/A // return the encoding of key if key is a valid ARCFOUR key. 0N/A // otherwise, throw an InvalidKeyException 0N/A (
"Key length must be between 40 and 1024 bit");