use of java.security.spec.RSAPrivateKeySpec in project jdk8u_jdk by JetBrains.
the class KeySizeTest method main.
public static void main(String[] args) throws Exception {
int iKeyPairSize = Integer.parseInt(args[0]);
int maxLoopCnt = Integer.parseInt(args[1]);
int failCount = 0;
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
keyPairGen.initialize(iKeyPairSize);
// Generate RSA keypair
KeyPair keyPair = keyPairGen.generateKeyPair();
// Get priavte and public keys
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
try {
if (!sizeTest(keyPair)) {
failCount++;
}
} catch (Exception ex) {
ex.printStackTrace(System.err);
failCount++;
}
for (int iCnt = 0; iCnt < maxLoopCnt; iCnt++) {
// Get keysize (modulus) of keys
KeyFactory keyFact = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
// Comparing binary length.
RSAPrivateKeySpec privateKeySpec = (RSAPrivateKeySpec) keyFact.getKeySpec(privateKey, RSAPrivateKeySpec.class);
int iPrivateKeySize = privateKeySpec.getModulus().bitLength();
RSAPublicKeySpec publicKeySpec = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
int iPublicKeySize = publicKeySpec.getModulus().bitLength();
if ((iKeyPairSize != iPublicKeySize) || (iKeyPairSize != iPrivateKeySize)) {
System.err.println("iKeyPairSize : " + iKeyPairSize);
System.err.println("Generated a " + iPrivateKeySize + " bit RSA private key");
System.err.println("Generated a " + iPublicKeySize + " bit RSA public key");
failCount++;
}
}
if (failCount > 0) {
throw new RuntimeException("There are " + failCount + " tests failed.");
}
}
use of java.security.spec.RSAPrivateKeySpec in project jdk8u_jdk by JetBrains.
the class PrivateKeyEqualityTest method main.
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
// Generate the first key.
KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
KeyPair keyPair = generator.generateKeyPair();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
}
// Generate the second key.
KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(rsaPrivateKeySpec);
// Generate the third key.
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(encodedKeySpec);
// Check for equality.
if (rsaPrivateKey.equals(rsaPrivateKey2)) {
throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
}
if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
}
if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
}
if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
}
// Generate the fourth key.
RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) rsaPrivateKey;
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent(), rsaPrivateCrtKey.getPrivateExponent(), rsaPrivateCrtKey.getPrimeP(), rsaPrivateCrtKey.getPrimeQ(), rsaPrivateCrtKey.getPrimeExponentP(), rsaPrivateCrtKey.getPrimeExponentQ(), rsaPrivateCrtKey.getCrtCoefficient());
RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(rsaPrivateCrtKeySpec);
if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
}
}
use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.
the class CertificateGenerator method generateSelfSigned.
/**
* Generates a self-signed certificate. These are used to identify servers.
*
* @return A matched pair of public and private certificates.
*/
public CertificatePair generateSelfSigned() {
keyPairGenerator.initialize(KEY_SIZE);
KeyPair kp = keyPairGenerator.genKeyPair();
try {
RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
String uuid = UUID.randomUUID().toString();
signer.initSign(kp.getPrivate(), new SecureRandom());
signer.update(uuid.getBytes(Charsets.UTF_8));
signer.update(pub.getModulus().toByteArray());
signer.update(pub.getPublicExponent().toByteArray());
byte[] rawSig = signer.sign();
BigInteger signature = new BigInteger(rawSig);
PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
return new CertificatePair(publicCert, privateCert);
} catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) {
throw new RuntimeException("Unexpected exception generating certificate", e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.
the class PrivateIdentityCertificate method sign.
/**
* Produces a signature for data that can be verified as by the paired public certificate.
*
* @param dataToSign
* @return The signature
*/
public byte[] sign(byte[] dataToSign) {
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
Signature signer = null;
try {
signer = Signature.getInstance(IdentityConstants.SIGNATURE_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
PrivateKey key = keyFactory.generatePrivate(keySpec);
signer.initSign(key, new SecureRandom());
signer.update(dataToSign);
return signer.sign();
} catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException e) {
throw new RuntimeException("Unexpected exception during signing", e);
}
}
use of java.security.spec.RSAPrivateKeySpec in project Wurst-MC-1.12 by Wurst-Imperium.
the class Encryption method createRsaKeys.
private KeyPair createRsaKeys(Path publicFile, Path privateFile) {
try {
System.out.println("Generating RSA keypair.");
// generate keypair
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
KeyFactory factory = KeyFactory.getInstance("RSA");
// save public key
try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(publicFile))) {
RSAPublicKeySpec keySpec = factory.getKeySpec(pair.getPublic(), RSAPublicKeySpec.class);
out.writeObject(keySpec.getModulus());
out.writeObject(keySpec.getPublicExponent());
}
// save private key
try (ObjectOutputStream out = new ObjectOutputStream(Files.newOutputStream(privateFile))) {
RSAPrivateKeySpec keySpec = factory.getKeySpec(pair.getPrivate(), RSAPrivateKeySpec.class);
out.writeObject(keySpec.getModulus());
out.writeObject(keySpec.getPrivateExponent());
}
return pair;
} catch (GeneralSecurityException | IOException e) {
throw new ReportedException(CrashReport.makeCrashReport(e, "Creating RSA keypair"));
}
}
Aggregations