Java tutorial
/** * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * * Copyright (c) 2012 - SCAPI (http://crypto.biu.ac.il/scapi) * This file is part of the SCAPI project. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * We request that any publication and/or code referring to and/or based on SCAPI contain an appropriate citation to SCAPI, including a reference to * http://crypto.biu.ac.il/SCAPI. * * SCAPI uses Crypto++, Miracl, NTL and Bouncy Castle. Please see these projects for any further licensing issues. * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * */ package edu.biu.scapi.primitives.prg.bc; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.SecretKey; import org.bouncycastle.crypto.engines.RC4Engine; import edu.biu.scapi.primitives.prg.RC4; /** * RC4 is a well known stream cipher, that is essentially a pseudorandom generator.<p> * In our implementation, we throw out the first 1024 bits since the first few bytes have been shown to have some bias. * * @author Cryptography and Computer Security Research Group Department of Computer Science Bar-Ilan University (Meital Levy) * */ public final class BcRC4 extends BcPRG implements RC4 { /** * Passes the RC4Engine of BC to the abstract super class */ public BcRC4() { super(new RC4Engine()); } public BcRC4(SecureRandom random) { super(new RC4Engine(), random); } public BcRC4(String randNumGenAlg) throws NoSuchAlgorithmException { super(new RC4Engine(), SecureRandom.getInstance(randNumGenAlg)); } public void setKey(SecretKey secretKey) { //sets the parameters super.setKey(secretKey); //RC4 has a problem in the first 1024 bits. by ignoring these bytes, we bypass this problem. byte[] out = new byte[128]; getPRGBytes(out, 0, 128); } }