use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.
the class JDKKeyStore 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 XobotOS by xamarin.
the class JDKPKCS12KeyStore method calculatePbeMac.
private static byte[] calculatePbeMac(DERObjectIdentifier oid, byte[] salt, int itCount, char[] password, boolean wrongPkcs12Zero, byte[] data) throws Exception {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
JCEPBEKey key = (JCEPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
Mac mac = Mac.getInstance(oid.getId(), bcProvider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
use of javax.crypto.spec.PBEParameterSpec in project XobotOS by xamarin.
the class WrapCipherSpi method engineInit.
protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
CipherParameters param;
if (key instanceof JCEPBEKey) {
JCEPBEKey k = (JCEPBEKey) key;
if (params instanceof PBEParameterSpec) {
param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
} else if (k.getParam() != null) {
param = k.getParam();
} else {
throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
}
} else {
param = new KeyParameter(key.getEncoded());
}
if (params instanceof javax.crypto.spec.IvParameterSpec) {
IvParameterSpec iv = (IvParameterSpec) params;
param = new ParametersWithIV(param, iv.getIV());
}
if (param instanceof KeyParameter && ivSize != 0) {
iv = new byte[ivSize];
random.nextBytes(iv);
param = new ParametersWithIV(param, iv);
}
switch(opmode) {
case Cipher.WRAP_MODE:
wrapEngine.init(true, param);
break;
case Cipher.UNWRAP_MODE:
wrapEngine.init(false, param);
break;
case Cipher.ENCRYPT_MODE:
case Cipher.DECRYPT_MODE:
throw new IllegalArgumentException("engine only valid for wrapping");
default:
System.out.println("eeek!");
}
}
use of javax.crypto.spec.PBEParameterSpec in project robovm by robovm.
the class PBEParameterSpecTest method testGetSalt.
/**
* getSalt() method testing. Tests that returned salt is equal
* to the salt specified in the constructor and that the change of
* returned array does not cause the change of internal array.
*/
public void testGetSalt() {
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
PBEParameterSpec pbeps = new PBEParameterSpec(salt, iterationCount);
byte[] result = pbeps.getSalt();
if (!Arrays.equals(salt, result)) {
fail("The returned salt is not equal to the specified " + "in the constructor.");
}
result[0]++;
assertFalse("The change of returned by getSalt() method salt" + "should not cause the change of internal array.", result[0] == pbeps.getSalt()[0]);
}
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]);
}
Aggregations