use of javax.crypto.spec.PBEKeySpec 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.PBEKeySpec 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.PBEKeySpec in project robovm by robovm.
the class SecretKeyFactoryTest method test_PBKDF2_8BIT.
private void test_PBKDF2_8BIT(char[] password, byte[] salt, int iterations, int keyLength, byte[] expected) throws Exception {
if (!StandardNames.IS_RI) {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
KeySpec ks = new PBEKeySpec(password, salt, iterations, keyLength);
SecretKey key = factory.generateSecret(ks);
assertTrue(Arrays.equals(expected, key.getEncoded()));
}
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testGetKeyLength.
/**
* getKeyLength() method testing.
*/
public void testGetKeyLength() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
int keyLength = 10;
PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount, keyLength);
assertTrue("The returned keyLength is not equal to the value specified " + "in the constructor.", pbeks.getKeyLength() == keyLength);
pbeks = new PBEKeySpec(password);
assertTrue("The getKeyLength() method should return 0 " + "if the keyLength is not specified.", pbeks.getKeyLength() == 0);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testPBEKeySpec3.
/**
* PBEKeySpec(char[] password, byte[] salt, int iterationCount) method
* testing. Tests the behavior of the method in the case
* of inappropriate parameters and checks that array objects specified as
* a parameters are copied during the object initialization.
*/
public void testPBEKeySpec3() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
try {
PBEKeySpec pbeks = new PBEKeySpec(null, salt, iterationCount);
assertTrue("An empty char[] should be used in case of null input " + "char array.", pbeks.getPassword().length == 0);
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException was thrown.");
} catch (NullPointerException e) {
fail("Unexpected NullPointerException was thrown.");
}
try {
new PBEKeySpec(password, null, iterationCount);
fail("A NullPointerException should be was thrown " + "in the case of null salt.");
} catch (IllegalArgumentException e) {
fail("Unexpected IllegalArgumentException was thrown.");
} catch (NullPointerException e) {
}
try {
new PBEKeySpec(password, new byte[0], iterationCount);
fail("An IllegalArgumentException should be thrown " + "in the case of empty salt.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, -1);
fail("An IllegalArgumentException should be thrown " + "in the case of negative iterationCount.");
} catch (IllegalArgumentException e) {
}
try {
new PBEKeySpec(password, salt, 0);
fail("An IllegalArgumentException should be thrown " + "in the case of zero iterationCount.");
} catch (IllegalArgumentException e) {
}
PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount);
password[0]++;
assertFalse("The change of password specified in the constructor " + "should not cause the change of internal array.", password[0] == pbeks.getPassword()[0]);
salt[0]++;
assertFalse("The change of salt specified in the constructor " + " should not cause the change of internal array.", salt[0] == pbeks.getSalt()[0]);
}
Aggregations