use of javax.crypto.SecretKeyFactory 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.SecretKeyFactory in project robovm by robovm.
the class CipherTest method test_update$BII.
/**
* javax.crypto.Cipher#update(byte[], int, int)
*/
public void test_update$BII() throws Exception {
for (int index = 1; index < 4; index++) {
Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
Key k = skf.generateSecret(keySpec);
byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
IvParameterSpec iv = new IvParameterSpec(ivMaterial);
c.init(Cipher.DECRYPT_MODE, k, iv);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] input = new byte[256];
String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".ciphertext";
File resources = Support_Resources.createTempFolder();
Support_Resources.copyFile(resources, null, resPath);
InputStream is = Support_Resources.getStream(resPath);
int bytesRead = is.read(input, 0, 256);
while (bytesRead > 0) {
byte[] output = c.update(input, 0, bytesRead);
if (output != null) {
baos.write(output);
}
bytesRead = is.read(input, 0, 256);
}
byte[] output = c.doFinal();
if (output != null) {
baos.write(output);
}
byte[] decipheredCipherText = baos.toByteArray();
is.close();
byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".plaintext");
assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(plaintextBytes), Arrays.toString(decipheredCipherText));
}
Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
try {
cipher.update(new byte[64], 0, 32);
fail();
} catch (IllegalStateException expected) {
}
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class CipherTest method test_doFinal.
/**
* javax.crypto.Cipher#doFinal()
*/
public void test_doFinal() throws Exception {
for (int index = 1; index < 4; index++) {
Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
Key k = skf.generateSecret(keySpec);
byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
IvParameterSpec iv = new IvParameterSpec(ivMaterial);
c.init(Cipher.ENCRYPT_MODE, k, iv);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] input = new byte[256];
String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".plaintext";
File resources = Support_Resources.createTempFolder();
Support_Resources.copyFile(resources, null, resPath);
InputStream is = Support_Resources.getStream(resPath);
int bytesRead = is.read(input, 0, 256);
while (bytesRead > 0) {
byte[] output = c.update(input, 0, bytesRead);
if (output != null) {
baos.write(output);
}
bytesRead = is.read(input, 0, 256);
}
byte[] output = c.doFinal();
if (output != null) {
baos.write(output);
}
byte[] encryptedPlaintext = baos.toByteArray();
is.close();
byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".ciphertext");
assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(cipherText), Arrays.toString(encryptedPlaintext));
}
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
byte[] b1 = new byte[30];
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
c.update(b, 0, 10, b1, 5);
try {
c.doFinal();
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal();
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
int len = c.doFinal(b, 0, 16, b1, 0);
assertEquals(16, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
assertTrue(Arrays.equals(c.getIV(), IV));
c.update(b1, 0, 24, b, 0);
try {
c.doFinal();
fail();
} catch (BadPaddingException expected) {
}
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class mySecretKeyFactory method testSecretKeyFactory01.
/**
* Test for <code>SecretKeyFactory</code> constructor
* Assertion: returns SecretKeyFactory object
*/
public void testSecretKeyFactory01() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
SecretKeyFactorySpi spi = new MySecretKeyFactorySpi();
SecretKeyFactory secKF = new mySecretKeyFactory(spi, defaultProvider, defaultAlgorithm);
assertEquals("Incorrect algorithm", secKF.getAlgorithm(), defaultAlgorithm);
assertEquals("Incorrect provider", secKF.getProvider(), defaultProvider);
assertNull("Incorrect result", secKF.generateSecret(null));
assertNull("Incorrect result", secKF.getKeySpec(null, null));
assertNull("Incorrect result", secKF.translateKey(null));
secKF = new mySecretKeyFactory(null, null, null);
assertNull("Algorithm must be null", secKF.getAlgorithm());
assertNull("Provider must be null", secKF.getProvider());
try {
secKF.translateKey(null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
}
use of javax.crypto.SecretKeyFactory in project robovm by robovm.
the class mySecretKeyFactory method test_translateKeyLjavax_crypto_SecretKey.
public void test_translateKeyLjavax_crypto_SecretKey() throws NoSuchAlgorithmException, InvalidKeyException {
KeyGenerator kg = null;
Key key = null;
SecretKeyFactory secKF = null;
for (int i = 0; i < validValues.length; i++) {
secKF = SecretKeyFactory.getInstance(validValues[i]);
assertNotNull(secKF.getProvider());
kg = KeyGenerator.getInstance(secKF.getAlgorithm());
kg.init(new SecureRandom());
key = kg.generateKey();
secKF.translateKey((SecretKey) key);
}
try {
secKF.translateKey(null);
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
}
Aggregations