use of org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters in project xwiki-commons by xwiki.
the class BcPBES2Rc2CipherFactory method getInstance.
@Override
public PasswordBasedCipher getInstance(boolean forEncryption, SymmetricCipherParameters password, KeyDerivationFunction kdf) {
KeyWithIVParameters params;
if (password instanceof KeyWithIVParameters) {
KeyParameter passkey = ((KeyWithIVParameters) password).getKeyParameter();
if (passkey instanceof RC2KeyParameters) {
params = new KeyWithIVParameters(new RC2KeyParameters(kdf.derive(passkey.getKey()).getKey(), ((RC2KeyParameters) passkey).getEffectiveBits()), ((KeyWithIVParameters) password).getIV());
} else {
params = new KeyWithIVParameters(kdf.derive(((KeyWithIVParameters) password).getKey()), ((KeyWithIVParameters) password).getIV());
}
} else if (password instanceof RC2KeyParameters) {
params = kdf.derive(((KeyParameter) password).getKey(), getIVSize());
params = new KeyWithIVParameters(new RC2KeyParameters(params.getKey(), ((RC2KeyParameters) password).getEffectiveBits()), params.getIV());
} else if (password instanceof KeyParameter) {
params = kdf.derive(((KeyParameter) password).getKey(), getIVSize());
} else {
throw new IllegalArgumentException("Invalid cipher parameters for RC2 password based cipher: " + password.getClass().getName());
}
// ensure RC2Version is computable
getRC2Version(params);
return getPasswordBasedCipher(forEncryption, kdf, params);
}
use of org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters in project xwiki-commons by xwiki.
the class BcRc2CbcPaddedCipherFactoryTest method testRC2EffectiveBitsDecription.
@Test
public void testRC2EffectiveBitsDecription() throws Exception {
Cipher cipher27Enc = factory.getInstance(true, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
Cipher cipher27 = factory.getInstance(false, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
byte[] result = cipher27.doFinal(cipher27Enc.doFinal(BYTES));
assertThat(result.length, equalTo(BYTES.length));
assertThat(result, equalTo(BYTES));
result = cipher27.doFinal(cipher27Enc.doFinal(ANOTHER_BYTES));
assertThat(result.length, equalTo(ANOTHER_BYTES.length));
assertThat(result, equalTo(ANOTHER_BYTES));
}
use of org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters in project xwiki-commons by xwiki.
the class BcPBES2Rc2CipherFactory method getRC2Version.
private int getRC2Version(KeyWithIVParameters parameters) {
KeyParameter keyParams = parameters.getKeyParameter();
int keySize;
if (keyParams instanceof RC2KeyParameters) {
keySize = ((RC2KeyParameters) keyParams).getEffectiveBits();
} else {
keySize = keyParams.getKey().length * 8;
}
switch(keySize) {
case 40:
return 160;
case 64:
return 120;
case 128:
return 58;
default:
if (keySize < 256) {
throw new IllegalArgumentException("Invalid cipher key size for PBES2 RC2 password based cipher: " + keySize + " bits. Valid key size are 40, 64, 128 and 256 and more.");
}
return keySize;
}
}
use of org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters in project xwiki-commons by xwiki.
the class BcPBES2Rc2CipherFactory method getRC2CipherParameters.
private SymmetricCipherParameters getRC2CipherParameters(byte[] password, RC2CBCParameter rc2Params, KeyDerivationFunction df) {
KeyParameter keyParam;
BigInteger version = rc2Params.getRC2ParameterVersion();
if (version != null) {
int bits = getRC2EffectiveBits(version.intValue());
df.overrideKeySize((bits + 7) / 8);
keyParam = new RC2KeyParameters(df.derive(password).getKey(), bits);
} else {
df.overrideKeySize(4);
keyParam = new KeyParameter(df.derive(password).getKey());
}
return new KeyWithIVParameters(keyParam, rc2Params.getIV());
}
use of org.xwiki.crypto.params.cipher.symmetric.RC2KeyParameters in project xwiki-commons by xwiki.
the class BcRc2CbcPaddedCipherFactoryTest method testRC2EffectiveBitsEncription.
@Test
public void testRC2EffectiveBitsEncription() throws Exception {
Cipher cipher27 = factory.getInstance(true, new KeyWithIVParameters(new RC2KeyParameters(KEY32, 27), IV8));
byte[] result = cipher27.doFinal(BYTES);
assertThat(result.length, equalTo(BYTES_ENCRYPTED_SIZE));
assertThat(result, not(equalTo(getEncrypted())));
result = cipher27.doFinal(ANOTHER_BYTES);
assertThat(result.length, equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE));
assertThat(result, not(equalTo(getAnotherEncrypted())));
}
Aggregations