use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class PBEParameterSpecTest method testPBEParameterSpec.
/**
* PBEParameterSpec(byte[] salt, int iterationCount) method testing.
* Tests the behavior of the method in the case of null input array
* and tests that input array is copied during the object initialization.
*/
public void testPBEParameterSpec() {
byte[] salt = { 1, 2, 3, 4, 5 };
int iterationCount = 10;
try {
new PBEParameterSpec(null, iterationCount);
fail("A NullPointerException should be was thrown " + "in the case of null salt.");
} catch (NullPointerException e) {
}
PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
salt[0]++;
assertFalse("The change of salt specified in the constructor " + "should not cause the change of internal array.", salt[0] == pbeps.getSalt()[0]);
}
use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class BcKeyStoreSpi method makePBECipher.
protected Cipher makePBECipher(String algorithm, int mode, char[] password, byte[] salt, int iterationCount) throws IOException {
try {
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
Cipher cipher = Cipher.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME);
cipher.init(mode, keyFact.generateSecret(pbeSpec), defParams);
return cipher;
} catch (Exception e) {
throw new IOException("Error initialising store of key store: " + e);
}
}
use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class PKCS12KeyStoreSpi method wrapKey.
protected byte[] wrapKey(String algorithm, Key key, PKCS12PBEParams pbeParams, char[] password) throws IOException {
PBEKeySpec pbeSpec = new PBEKeySpec(password);
byte[] out;
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);
out = cipher.wrap(key);
} catch (Exception e) {
throw new IOException("exception encrypting data - " + e.toString());
}
return out;
}
use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class BaseBlockCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
//
if (params == null && baseEngine.getAlgorithmName().startsWith("RC5-64")) {
throw new InvalidAlgorithmParameterException("RC5 requires an RC5ParametersSpec to be passed in.");
}
//
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
if (params instanceof IvParameterSpec) {
IvParameterSpec iv = (IvParameterSpec) params;
param = new ParametersWithIV(param, iv.getIV());
}
} else if (params instanceof PBEParameterSpec) {
pbeSpec = (PBEParameterSpec) params;
param = PBE.Util.makePBEParameters(k, params, cipher.getUnderlyingCipher().getAlgorithmName());
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (param instanceof ParametersWithIV) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
if (ivLength != 0) {
IvParameterSpec p = (IvParameterSpec) params;
if (p.getIV().length != ivLength && !isAEADModeName(modeName)) {
throw new InvalidAlgorithmParameterException("IV must be " + ivLength + " bytes long.");
}
if (key instanceof RepeatedSecretKeySpec) {
param = new ParametersWithIV(null, p.getIV());
ivParam = (ParametersWithIV) param;
} else {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), p.getIV());
ivParam = (ParametersWithIV) param;
}
} else {
if (modeName != null && modeName.equals("ECB")) {
throw new InvalidAlgorithmParameterException("ECB mode does not use an IV");
}
param = new KeyParameter(key.getEncoded());
}
} else // BEGIN android-removed
// else if (params instanceof GOST28147ParameterSpec)
// {
// GOST28147ParameterSpec gost28147Param = (GOST28147ParameterSpec)params;
//
// param = new ParametersWithSBox(
// new KeyParameter(key.getEncoded()), ((GOST28147ParameterSpec)params).getSbox());
//
// if (gost28147Param.getIV() != null && ivLength != 0)
// {
// param = new ParametersWithIV(param, gost28147Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// 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 (baseEngine.getAlgorithmName().startsWith("RC5"))
// {
// if (baseEngine.getAlgorithmName().equals("RC5-32"))
// {
// if (rc5Param.getWordSize() != 32)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 32 not " + rc5Param.getWordSize() + ".");
// }
// }
// else if (baseEngine.getAlgorithmName().equals("RC5-64"))
// {
// if (rc5Param.getWordSize() != 64)
// {
// throw new InvalidAlgorithmParameterException("RC5 already set up for a word size of 64 not " + rc5Param.getWordSize() + ".");
// }
// }
// }
// else
// {
// throw new InvalidAlgorithmParameterException("RC5 parameters passed to a cipher that is not RC5.");
// }
// if ((rc5Param.getIV() != null) && (ivLength != 0))
// {
// param = new ParametersWithIV(param, rc5Param.getIV());
// ivParam = (ParametersWithIV)param;
// }
// }
// END android-removed
{
throw new InvalidAlgorithmParameterException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.nextBytes(iv);
param = new ParametersWithIV(param, iv);
ivParam = (ParametersWithIV) param;
} else if (cipher.getUnderlyingCipher().getAlgorithmName().indexOf("PGPCFB") < 0) {
throw new InvalidAlgorithmParameterException("no IV set when one expected");
}
}
if (random != null && padded) {
param = new ParametersWithRandom(param, random);
}
try {
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:
throw new InvalidParameterException("unknown opmode " + opmode + " passed");
}
} catch (Exception e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class BaseStreamCipher method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
this.pbeSpec = null;
this.pbeAlgorithm = null;
this.engineParams = null;
//
if (!(key instanceof SecretKey)) {
throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
}
if (key instanceof BCPBEKey) {
BCPBEKey k = (BCPBEKey) key;
if (k.getOID() != null) {
pbeAlgorithm = k.getOID().getId();
} else {
pbeAlgorithm = k.getAlgorithm();
}
if (k.getParam() != null) {
param = k.getParam();
pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
} else if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
pbeSpec = (PBEParameterSpec) params;
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
if (k.getIvSize() != 0) {
ivParam = (ParametersWithIV) param;
}
} else if (params == null) {
param = new KeyParameter(key.getEncoded());
} else if (params instanceof IvParameterSpec) {
param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
ivParam = (ParametersWithIV) param;
} else {
throw new IllegalArgumentException("unknown parameter type.");
}
if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
SecureRandom ivRandom = random;
if (ivRandom == null) {
ivRandom = new SecureRandom();
}
if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
byte[] iv = new byte[ivLength];
ivRandom.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