use of java.security.spec.RSAPrivateCrtKeySpec in project intellij-community by JetBrains.
the class Asn1Object method read.
private static PrivateKey read(String fileName) throws IOException {
KeyFactory factory;
try {
factory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
throw new IOException("JCE error: " + e.getMessage());
}
List<String> lines = FileUtilRt.loadLines(fileName, "UTF-8");
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (line.contains(P1_BEGIN_MARKER)) {
List<String> strings = lines.subList(i + 1, lines.size());
byte[] keyBytes = readKeyMaterial(P1_END_MARKER, strings);
RSAPrivateCrtKeySpec keySpec = getRSAKeySpec(keyBytes);
try {
return factory.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
throw new IOException("Invalid PKCS#1 PEM file: " + e.getMessage());
}
}
if (line.contains(P8_BEGIN_MARKER)) {
List<String> strings = lines.subList(i + 1, lines.size());
byte[] keyBytes = readKeyMaterial(P8_END_MARKER, strings);
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
try {
return factory.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
throw new IOException("Invalid PKCS#8 PEM file: " + e.getMessage());
}
}
}
throw new IOException("Invalid PEM file: no begin marker");
}
use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success.
public void testSign_SHA1withRSA_CrtKeyWithPublicExponent_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, RSA_2048_privateExponent, null, null, null, null, null);
// The RI fails on this key which is totally unreasonable.
final PrivateKey privKey;
try {
privKey = kf.generatePrivate(keySpec);
} catch (NullPointerException e) {
if (StandardNames.IS_RI) {
return;
} else {
fail("Private key should be created");
return;
}
}
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initSign(privKey);
sig.update(Vector1Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
assertTrue("Signature should match expected", Arrays.equals(signature, SHA1withRSA_Vector1Signature));
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector1Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure.
public void testSign_SHA1withRSA_CrtKey_NoPrivateExponent_Failure() throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(RSA_2048_modulus, RSA_2048_publicExponent, null, RSA_2048_primeP, RSA_2048_primeQ, null, null, null);
// Failing on this key early is okay.
final PrivateKey privKey;
try {
privKey = kf.generatePrivate(keySpec);
} catch (NullPointerException e) {
return;
} catch (InvalidKeySpecException e) {
return;
}
Signature sig = Signature.getInstance("SHA1withRSA");
try {
sig.initSign(privKey);
fail("Should throw error when private exponent is not available");
} catch (InvalidKeyException expected) {
}
}
use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class RSAPrivateCrtKeySpecTest method testGetPrimeExponentP.
/**
* Test for <code>getPrimeExponentP()</code> method<br>
* Assertion: returns prime exponent P
*/
public final void testGetPrimeExponentP() {
RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE);
assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeExponentP()));
}
use of java.security.spec.RSAPrivateCrtKeySpec in project robovm by robovm.
the class RSAPrivateCrtKeySpecTest method testGetCrtCoefficient.
/**
* Test for <code>getCrtCoefficient()</code> method<br>
* Assertion: returns crt coefficient
*/
public final void testGetCrtCoefficient() {
RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec(BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L));
assertTrue(BigInteger.valueOf(5L).equals(ks.getCrtCoefficient()));
}
Aggregations