Search in sources :

Example 6 with CBCBlockCipher

use of com.github.zhenwei.core.crypto.modes.CBCBlockCipher in project LinLong-Java by zhenwei1108.

the class DESedeWrapEngine method init.

/**
 * Method init
 *
 * @param forWrapping true if for wrapping, false otherwise.
 * @param param       necessary parameters, may include KeyParameter, ParametersWithRandom, and
 *                    ParametersWithIV
 */
public void init(boolean forWrapping, CipherParameters param) {
    this.forWrapping = forWrapping;
    this.engine = new CBCBlockCipher(new DESedeEngine());
    SecureRandom sr;
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom pr = (ParametersWithRandom) param;
        param = pr.getParameters();
        sr = pr.getRandom();
    } else {
        sr = CryptoServicesRegistrar.getSecureRandom();
    }
    if (param instanceof KeyParameter) {
        this.param = (KeyParameter) param;
        if (this.forWrapping) {
            // Hm, we have no IV but we want to wrap ?!?
            // well, then we have to create our own IV.
            this.iv = new byte[8];
            sr.nextBytes(iv);
            this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
        }
    } else if (param instanceof ParametersWithIV) {
        this.paramPlusIV = (ParametersWithIV) param;
        this.iv = this.paramPlusIV.getIV();
        this.param = (KeyParameter) this.paramPlusIV.getParameters();
        if (this.forWrapping) {
            if ((this.iv == null) || (this.iv.length != 8)) {
                throw new IllegalArgumentException("IV is not 8 octets");
            }
        } else {
            throw new IllegalArgumentException("You should not supply an IV for unwrapping");
        }
    }
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher)

Aggregations

CBCBlockCipher (com.github.zhenwei.core.crypto.modes.CBCBlockCipher)6 PaddedBufferedBlockCipher (com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher)4 CFBBlockCipher (com.github.zhenwei.core.crypto.modes.CFBBlockCipher)3 OFBBlockCipher (com.github.zhenwei.core.crypto.modes.OFBBlockCipher)3 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)3 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)2 DESedeEngine (com.github.zhenwei.core.crypto.engines.DESedeEngine)2 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)2 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)2 BlockCipher (com.github.zhenwei.core.crypto.BlockCipher)1 AESEngine (com.github.zhenwei.core.crypto.engines.AESEngine)1 BlowfishEngine (com.github.zhenwei.core.crypto.engines.BlowfishEngine)1 DESEngine (com.github.zhenwei.core.crypto.engines.DESEngine)1 DSTU7624Engine (com.github.zhenwei.core.crypto.engines.DSTU7624Engine)1 RC2Engine (com.github.zhenwei.core.crypto.engines.RC2Engine)1 FPEFF1Engine (com.github.zhenwei.core.crypto.fpe.FPEFF1Engine)1 FPEFF3_1Engine (com.github.zhenwei.core.crypto.fpe.FPEFF3_1Engine)1 CCMBlockCipher (com.github.zhenwei.core.crypto.modes.CCMBlockCipher)1 CTSBlockCipher (com.github.zhenwei.core.crypto.modes.CTSBlockCipher)1 EAXBlockCipher (com.github.zhenwei.core.crypto.modes.EAXBlockCipher)1