Search in sources :

Example 1 with BCPBEKey

use of com.github.zhenwei.provider.jcajce.provider.symmetric.util.BCPBEKey in project LinLong-Java by zhenwei1108.

the class BrokenJCEBlockCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    // 
    if (key instanceof BCPBEKey) {
        param = Util.makePBEParameters((BCPBEKey) key, params, pbeType, pbeHash, cipher.getUnderlyingCipher().getAlgorithmName(), pbeKeySize, pbeIvSize);
        if (pbeIvSize != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        if (ivLength != 0) {
            param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
            ivParam = (ParametersWithIV) param;
        } else {
            param = new KeyParameter(key.getEncoded());
        }
    } else if (params instanceof RC2ParameterSpec) {
        RC2ParameterSpec rc2Param = (RC2ParameterSpec) params;
        param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec) params).getEffectiveKeyBits());
        if (rc2Param.getIV() != null && ivLength != 0) {
            param = new ParametersWithIV(param, rc2Param.getIV());
            ivParam = (ParametersWithIV) param;
        }
    } else if (params instanceof RC5ParameterSpec) {
        RC5ParameterSpec rc5Param = (RC5ParameterSpec) params;
        param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec) params).getRounds());
        if (rc5Param.getWordSize() != 32) {
            throw new IllegalArgumentException("can only accept RC5 word size 32 (at the moment...)");
        }
        if ((rc5Param.getIV() != null) && (ivLength != 0)) {
            param = new ParametersWithIV(param, rc5Param.getIV());
            ivParam = (ParametersWithIV) param;
        }
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        if (random == null) {
            random = CryptoServicesRegistrar.getSecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            random.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) RC2Parameters(com.github.zhenwei.core.crypto.params.RC2Parameters) RC5ParameterSpec(javax.crypto.spec.RC5ParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RC5Parameters(com.github.zhenwei.core.crypto.params.RC5Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BCPBEKey(com.github.zhenwei.provider.jcajce.provider.symmetric.util.BCPBEKey) IvParameterSpec(javax.crypto.spec.IvParameterSpec) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec)

Aggregations

CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)1 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)1 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)1 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)1 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)1 BCPBEKey (com.github.zhenwei.provider.jcajce.provider.symmetric.util.BCPBEKey)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 RC2ParameterSpec (javax.crypto.spec.RC2ParameterSpec)1 RC5ParameterSpec (javax.crypto.spec.RC5ParameterSpec)1