use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success.
private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success(String provider) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
/*
* You're actually decrypting with private keys, but there is no
* distinction made here. It's all keyed off of what kind of key you're
* using. ENCRYPT_MODE and DECRYPT_MODE are the same.
*/
c.init(Cipher.ENCRYPT_MODE, privKey);
byte[] encrypted = new byte[RSA_Vector1_Encrypt_Private.length];
final int encryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, encryptLen);
assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
c.init(Cipher.DECRYPT_MODE, privKey);
final int decryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, decryptLen);
assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
}
use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class CipherTest method testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure.
private void testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure(String provider) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
/*
* You're actually encrypting with public keys, but there is no
* distinction made here. It's all keyed off of what kind of key you're
* using. ENCRYPT_MODE and DECRYPT_MODE are the same.
*/
c.init(Cipher.ENCRYPT_MODE, privKey);
byte[] output = new byte[RSA_2048_Vector1.length];
c.update(RSA_Vector1_ZeroPadded_Encrypted, 0, RSA_Vector1_ZeroPadded_Encrypted.length, output);
try {
c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
fail("Should have error when block size is too big.");
} catch (IllegalBlockSizeException success) {
assertFalse(provider, "BC".equals(provider));
} catch (ArrayIndexOutOfBoundsException success) {
assertEquals("BC", provider);
}
}
use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure.
private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure(String provider) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
/*
* You're actually encrypting with public keys, but there is no
* distinction made here. It's all keyed off of what kind of key you're
* using. ENCRYPT_MODE and DECRYPT_MODE are the same.
*/
c.init(Cipher.ENCRYPT_MODE, privKey);
byte[] tooBig_Vector = new byte[RSA_Vector1_ZeroPadded_Encrypted.length * 2];
System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, 0, RSA_Vector1_ZeroPadded_Encrypted.length);
System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, RSA_Vector1_ZeroPadded_Encrypted.length, RSA_Vector1_ZeroPadded_Encrypted.length);
try {
c.doFinal(tooBig_Vector);
fail("Should have error when block size is too big.");
} catch (IllegalBlockSizeException success) {
assertFalse(provider, "BC".equals(provider));
} catch (ArrayIndexOutOfBoundsException success) {
assertEquals("BC", provider);
}
}
use of java.security.spec.RSAPrivateKeySpec 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 java.security.spec.RSAPrivateKeySpec in project j2objc by google.
the class RSAPrivateKeyTest method test_getPrivateExponent.
/**
* java.security.interfaces.RSAPrivateKey
* #getPrivateExponent()
*/
public void test_getPrivateExponent() throws Exception {
KeyFactory gen = KeyFactory.getInstance("RSA");
final BigInteger n = BigInteger.valueOf(3233);
final BigInteger d = BigInteger.valueOf(2753);
RSAPrivateKey key = (RSAPrivateKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
assertEquals("invalid private exponent", d, key.getPrivateExponent());
}
Aggregations