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!");
}
}
Aggregations