use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testClearPassword.
/**
* clearPassword() method testing. Tests that internal copy of password
* is cleared after the method call.
*/
public void testClearPassword() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
PBEKeySpec pbeks = new PBEKeySpec(password);
pbeks.clearPassword();
try {
pbeks.getPassword();
fail("An IllegalStateException should be was thrown " + "after the clearing the password.");
} catch (IllegalStateException e) {
}
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testPBEKeySpec1.
/**
* PBEKeySpec(char[] password) method testing. Tests the behavior of
* the method in the case of null input char array and tests that input
* array is copied during the object initialization.
*/
public void testPBEKeySpec1() {
try {
PBEKeySpec pbeks = new PBEKeySpec(null);
assertTrue("An empty char[] should be used in case of null " + "char array.", pbeks.getPassword().length == 0);
} catch (NullPointerException e) {
fail("Unexpected NullPointerException was thrown.");
}
char[] password = new char[] { '1', '2', '3', '4', '5' };
PBEKeySpec pbeks = new PBEKeySpec(password);
password[0]++;
assertFalse("The change of password specified in the constructor " + "should not cause the change of internal array.", password[0] == pbeks.getPassword()[0]);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PKCS12KeyStoreSpi method unwrapKey.
protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
try {
if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
PrivateKey out;
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
SecretKey k = keyFact.generateSecret(pbeSpec);
((BCPBEKey) k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm.getId(), bcProvider);
cipher.init(Cipher.UNWRAP_MODE, k, defParams);
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
} else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters());
PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters());
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg.getKeyDerivationFunc().getAlgorithm().getId(), bcProvider);
SecretKey k = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), func.getIterationCount().intValue(), SecretKeyUtil.getKeySize(alg.getEncryptionScheme().getAlgorithm())));
Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId(), bcProvider);
cipher.init(Cipher.UNWRAP_MODE, k, new IvParameterSpec(ASN1OctetString.getInstance(alg.getEncryptionScheme().getParameters()).getOctets()));
// we pass "" as the key algorithm type as it is unknown at this point
return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
}
} catch (Exception e) {
throw new IOException("exception unwrapping private key - " + e.toString());
}
throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PKCS12KeyStoreSpi method calculatePbeMac.
private static byte[] calculatePbeMac(ASN1ObjectIdentifier 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);
BCPBEKey key = (BCPBEKey) 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.PBEKeySpec in project robovm by robovm.
the class PKCS12KeyStoreSpi method cryptData.
protected byte[] cryptData(boolean forEncryption, AlgorithmIdentifier algId, char[] password, boolean wrongPKCS12Zero, byte[] data) throws IOException {
String algorithm = algId.getAlgorithm().getId();
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try {
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
BCPBEKey key = (BCPBEKey) keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
} catch (Exception e) {
throw new IOException("exception decrypting data - " + e.toString());
}
}
Aggregations