Search in sources :

Example 56 with RSAPrivateKeySpec

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.");
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) KeyPairGenerator(java.security.KeyPairGenerator) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 57 with RSAPrivateKeySpec

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");
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Example 58 with RSAPrivateKeySpec

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);
    }
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 59 with RSAPrivateKeySpec

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);
    }
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Signature(java.security.Signature) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) KeyFactory(java.security.KeyFactory)

Example 60 with RSAPrivateKeySpec

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"));
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyPairGenerator(java.security.KeyPairGenerator) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) KeyFactory(java.security.KeyFactory) ReportedException(net.minecraft.util.ReportedException)

Aggregations

RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)78 KeyFactory (java.security.KeyFactory)49 PrivateKey (java.security.PrivateKey)41 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)30 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)27 BigInteger (java.math.BigInteger)25 PublicKey (java.security.PublicKey)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 InvalidKeyException (java.security.InvalidKeyException)14 Cipher (javax.crypto.Cipher)14 RSAPublicKey (java.security.interfaces.RSAPublicKey)13 Signature (java.security.Signature)12 KeyPair (java.security.KeyPair)10 KeySpec (java.security.spec.KeySpec)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)9 IOException (java.io.IOException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 SignatureException (java.security.SignatureException)8 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 SecureRandom (java.security.SecureRandom)6