Example usage for java.security.spec RSAKeyGenParameterSpec F0

List of usage examples for java.security.spec RSAKeyGenParameterSpec F0

Introduction

In this page you can find the example usage for java.security.spec RSAKeyGenParameterSpec F0.

Prototype

BigInteger F0

To view the source code for java.security.spec RSAKeyGenParameterSpec F0.

Click Source Link

Document

The public-exponent value F0 = 3.

Usage

From source file:test.be.fedict.eid.applet.RSATest.java

@Test
public void testManualEncryption() throws Exception {
    while (true) {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA",
                BouncyCastleProvider.PROVIDER_NAME);
        SecureRandom random = new SecureRandom();
        int keySize = 128;
        keyPairGenerator.initialize(new RSAKeyGenParameterSpec(keySize, RSAKeyGenParameterSpec.F0), random);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        LOG.debug("private key modulus: " + rsaPrivateKey.getModulus());
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        LOG.debug("public key modulus: " + rsaPublicKey.getModulus());
        LOG.debug("public key exponent: " + rsaPublicKey.getPublicExponent());
        LOG.debug("modulus size: " + rsaPublicKey.getModulus().toByteArray().length);

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        int dataSize = keySize / 8 - 11;
        byte[] data1 = new byte[dataSize];
        for (int i = 0; i < data1.length; i++) {
            data1[i] = 0x00;//w ww  .j  a  va  2  s .c  om
        }
        byte[] data2 = new byte[dataSize];
        for (int i = 0; i < data2.length; i++) {
            data2[i] = 0x00;
        }
        data2[data2.length - 1] = 0x07;

        byte[] signatureValue1 = cipher.doFinal(data1);

        LOG.debug("signature size: " + signatureValue1.length);

        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] signatureValue2 = cipher.doFinal(data2);

        BigInteger sigBigInt1 = new BigInteger(signatureValue1);
        BigInteger sigBigInt2 = new BigInteger(signatureValue2);
        BigInteger msgBigInt1 = sigBigInt1.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus());
        BigInteger msgBigInt2 = sigBigInt2.modPow(rsaPublicKey.getPublicExponent(), rsaPublicKey.getModulus());
        LOG.debug("msg big int: " + msgBigInt1);
        byte[] msgBytes1 = msgBigInt1.toByteArray();
        LOG.debug("original message size: " + msgBytes1.length);
        LOG.debug("original message1: " + new String(Hex.encodeHex(msgBytes1)));
        LOG.debug("original message2: " + new String(Hex.encodeHex(msgBigInt2.toByteArray())));

        LOG.debug("msg1 prime: " + msgBigInt1.isProbablePrime(100));
        LOG.debug("msg2 prime: " + msgBigInt2.isProbablePrime(100));

        // BigInteger.pow offers a very naive implementation
        LOG.debug("calculating s1^e...");
        BigInteger s1_e = sigBigInt1.pow(rsaPublicKey.getPublicExponent().intValue());
        LOG.debug("s1^e: " + s1_e);
        LOG.debug("calculating s2^e...");
        BigInteger s2_e = sigBigInt2.pow(rsaPublicKey.getPublicExponent().intValue());
        LOG.debug("s2^e: " + s2_e);

        LOG.debug("calculating GCD...");
        LOG.debug("msg1: " + msgBigInt1);
        LOG.debug("msg2: " + msgBigInt2);
        BigInteger a = s1_e.subtract(msgBigInt1);
        BigInteger b = s2_e.subtract(msgBigInt2);
        LOG.debug("a: " + a);
        LOG.debug("b: " + b);
        BigInteger candidateModulus = a.gcd(b);
        LOG.debug("candidate modulus: " + candidateModulus);
        LOG.debug("candidate modulus size: " + candidateModulus.toByteArray().length);
        BigInteger s_e = s1_e.multiply(s2_e);
        BigInteger m = msgBigInt1.multiply(msgBigInt2);
        while (false == rsaPublicKey.getModulus().equals(candidateModulus)) {
            LOG.error("incorrect candidate modulus");
            LOG.debug("modulus | candidate modulus: "
                    + candidateModulus.remainder(rsaPublicKey.getModulus()).equals(BigInteger.ZERO));
            s_e = s_e.multiply(s1_e);
            m = m.multiply(msgBigInt1);
            BigInteger n1 = s_e.subtract(m).gcd(a);
            BigInteger n2 = s_e.subtract(m).gcd(b);
            candidateModulus = n1.gcd(n2);
            // try / 2
            LOG.debug("new modulus:       " + n1);
            LOG.debug("new modulus:       " + n2);
            LOG.debug("candidate modulus: " + candidateModulus);
            LOG.debug("actual mod:        " + rsaPublicKey.getModulus());
        }
    }
}