use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class CipherTest method getEncryptKey.
private static synchronized Key getEncryptKey(String algorithm) throws Exception {
Key key = ENCRYPT_KEYS.get(algorithm);
if (key != null) {
return key;
}
if (algorithm.startsWith("RSA")) {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
key = kf.generatePrivate(keySpec);
} else if (isPBE(algorithm)) {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
} else {
KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
key = kg.generateKey();
}
ENCRYPT_KEYS.put(algorithm, key);
return key;
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class CipherPBEThread method crypt.
@Override
public void crypt() throws Exception {
byte[] output = new byte[128];
byte[] decrypted = new byte[128];
byte[] input = getData().getBytes();
byte[] salt = new byte[8];
SecureRandom sr = new SecureRandom();
PBEKeySpec keySpec = new PBEKeySpec("top sicret password".toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance(getAlgName());
SecretKey key = skf.generateSecret(keySpec);
Cipher cip = Cipher.getInstance(getAlgName() + "/" + getMode() + "/" + getPadding());
sr.nextBytes(salt);
PBEParameterSpec parSpec = new PBEParameterSpec(salt, getKeyLength());
cip.init(Cipher.ENCRYPT_MODE, key, parSpec);
cip.doFinal(input, 0, input.length, output);
int outputSize = cip.getOutputSize(input.length);
cip.init(Cipher.DECRYPT_MODE, key, parSpec);
cip.doFinal(output, 0, outputSize, decrypted);
checkEncodedData(getData().getBytes(), decrypted);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class SecretKeyFactoryThread method test.
@Override
public void test() throws Exception {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algName);
byte[] b = new byte[24];
KeySpec ks = (KeySpec) ((algName == "DES") ? new DESKeySpec(b) : (algName == "DESede") ? new DESedeKeySpec(b) : new PBEKeySpec("passw".toCharArray()));
skf.generateSecret(ks);
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest 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.
* Also it checks that the method returns null if salt is not
* specified.
*/
public void testGetSalt() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
byte[] salt = new byte[] { 1, 2, 3, 4, 5 };
int iterationCount = 10;
PBEKeySpec pbeks = new PBEKeySpec(password, salt, iterationCount);
byte[] result = pbeks.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] == pbeks.getSalt()[0]);
pbeks = new PBEKeySpec(password);
assertNull("The getSalt() method should return null if the salt " + "is not specified.", pbeks.getSalt());
}
use of javax.crypto.spec.PBEKeySpec in project robovm by robovm.
the class PBEKeySpecTest method testGetPassword.
/**
* getPassword() method testing. Tests that returned password is equal
* to the password specified in the constructor and that the change of
* returned array does not cause the change of internal array.
*/
public void testGetPassword() {
char[] password = new char[] { '1', '2', '3', '4', '5' };
PBEKeySpec pbeks = new PBEKeySpec(password);
char[] result = pbeks.getPassword();
if (!Arrays.equals(password, result)) {
fail("The returned password is not equal to the specified " + "in the constructor.");
}
result[0]++;
assertFalse("The change of returned by getPassword() method password " + "should not cause the change of internal array.", result[0] == pbeks.getPassword()[0]);
}
Aggregations