use of java.security.spec.RSAPrivateKeySpec in project jdk8u_jdk by JetBrains.
the class GenerationTests method getPrivateKey.
private static PrivateKey getPrivateKey(String algo, int keysize) throws Exception {
KeyFactory kf = KeyFactory.getInstance(algo);
KeySpec kspec;
if (algo.equalsIgnoreCase("DSA")) {
if (keysize == 1024) {
kspec = new DSAPrivateKeySpec(new BigInteger(DSA_X), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
} else if (keysize == 2048) {
kspec = new DSAPrivateKeySpec(new BigInteger(DSA_2048_X), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
} else
throw new RuntimeException("Unsupported keysize:" + keysize);
} else if (algo.equalsIgnoreCase("RSA")) {
if (keysize == 512) {
kspec = new RSAPrivateKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
} else {
kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_1024_PRIV));
}
} else
throw new RuntimeException("Unsupported key algorithm " + algo);
return kf.generatePrivate(kspec);
}
use of java.security.spec.RSAPrivateKeySpec in project oxAuth by GluuFederation.
the class JweDecrypterImpl method decryptEncryptionKey.
@Override
public byte[] decryptEncryptionKey(String encodedEncryptedKey) throws InvalidJweException {
if (getKeyEncryptionAlgorithm() == null) {
throw new InvalidJweException("The key encryption algorithm is null");
}
if (encodedEncryptedKey == null) {
throw new InvalidJweException("The encoded encryption key is null");
}
try {
if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA_OAEP || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.RSA1_5) {
if (rsaPrivateKey == null && privateKey == null) {
throw new InvalidJweException("The RSA private key is null");
}
//Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm(), "BC");
Cipher cipher = Cipher.getInstance(getKeyEncryptionAlgorithm().getAlgorithm());
if (rsaPrivateKey != null) {
KeyFactory keyFactory = KeyFactory.getInstance(getKeyEncryptionAlgorithm().getFamily(), "BC");
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
java.security.interfaces.RSAPrivateKey privKey = (java.security.interfaces.RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
} else {
cipher.init(Cipher.DECRYPT_MODE, privateKey);
}
byte[] decryptedKey = cipher.doFinal(Base64Util.base64urldecode(encodedEncryptedKey));
return decryptedKey;
} else if (getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A128KW || getKeyEncryptionAlgorithm() == KeyEncryptionAlgorithm.A256KW) {
if (sharedSymmetricKey == null) {
throw new InvalidJweException("The shared symmetric key is null");
}
if (sharedSymmetricKey.length != 16) {
// 128 bit
MessageDigest sha = MessageDigest.getInstance("SHA-1");
sharedSymmetricKey = sha.digest(sharedSymmetricKey);
sharedSymmetricKey = Arrays.copyOf(sharedSymmetricKey, 16);
}
byte[] encryptedKey = Base64Util.base64urldecode(encodedEncryptedKey);
SecretKeySpec keyEncryptionKey = new SecretKeySpec(sharedSymmetricKey, "AES");
AESWrapEngine aesWrapEngine = new AESWrapEngine();
CipherParameters params = new KeyParameter(keyEncryptionKey.getEncoded());
aesWrapEngine.init(false, params);
byte[] decryptedKey = aesWrapEngine.unwrap(encryptedKey, 0, encryptedKey.length);
return decryptedKey;
} else {
throw new InvalidJweException("The key encryption algorithm is not supported");
}
} catch (NoSuchPaddingException e) {
throw new InvalidJweException(e);
} catch (NoSuchAlgorithmException e) {
throw new InvalidJweException(e);
} catch (IllegalBlockSizeException e) {
throw new InvalidJweException(e);
} catch (BadPaddingException e) {
throw new InvalidJweException(e);
} catch (NoSuchProviderException e) {
throw new InvalidJweException(e);
} catch (InvalidKeyException e) {
throw new InvalidJweException(e);
} catch (InvalidKeySpecException e) {
throw new InvalidJweException(e);
} catch (InvalidCipherTextException e) {
throw new InvalidJweException(e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA384withRSA_Key_Success.
public void testSign_SHA384withRSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA384withRSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
assertTrue("Signature should match expected", Arrays.equals(signature, SHA384withRSA_Vector2Signature));
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA256withRSA_Key_Success.
public void testSign_SHA256withRSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
assertTrue("Signature should match expected", Arrays.equals(signature, SHA256withRSA_Vector2Signature));
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.spec.RSAPrivateKeySpec in project robovm by robovm.
the class SignatureTest method testVerify_SHA1withRSA_Key_InitSignThenInitVerify_Success.
public void testVerify_SHA1withRSA_Key_InitSignThenInitVerify_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
PrivateKey privKey = kf.generatePrivate(privKeySpec);
Signature sig = Signature.getInstance("SHA1withRSA");
// Start a signing operation
sig.initSign(privKey);
sig.update(Vector2Data);
// Switch to verify
sig.initVerify(pubKey);
sig.update(Vector1Data);
assertTrue("Signature must match expected signature", sig.verify(SHA1withRSA_Vector1Signature));
}
Aggregations