Java Code Examples for java.security.interfaces.RSAPrivateCrtKey#getEncoded()

The following examples show how to use java.security.interfaces.RSAPrivateCrtKey#getEncoded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenerateRSAPrivateCrtKey.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 2
Source File: GenerateRSAPrivateCrtKey.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 3
Source File: GenerateRSAPrivateCrtKey.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 4
Source File: GenerateRSAPrivateCrtKey.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 5
Source File: GenerateRSAPrivateCrtKey.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 6
Source File: GenerateRSAPrivateCrtKey.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 7
Source File: GenerateRSAPrivateCrtKey.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 8
Source File: GenerateRSAPrivateCrtKey.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 9
Source File: GenerateRSAPrivateCrtKey.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 10
Source File: GenerateRSAPrivateCrtKey.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 11
Source File: GenerateRSAPrivateCrtKey.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 12
Source File: GenerateRSAPrivateCrtKey.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }
 
Example 13
Source File: GenerateRSAPrivateCrtKey.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Create an RSA Private Key from the CRT information
        RSAPrivateCrtKeySpec rsaCrtSpec =
            new RSAPrivateCrtKeySpec(new BigInteger(1, modulus),
                                     new BigInteger(1, pubExpo),
                                     new BigInteger(1, priExpo),
                                     new BigInteger(1, primeP),
                                     new BigInteger(1, primeQ),
                                     new BigInteger(1, expoP),
                                     new BigInteger(1, expoQ),
                                     new BigInteger(1, coeff));

        // Create an RSA private key from the CRT specification
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunRsaSign");
        RSAPrivateCrtKey rsaPriKey =
            (RSAPrivateCrtKey) kf.generatePrivate(rsaCrtSpec);

        // test resulting key against original specification
        if (!rsaPriKey.getCrtCoefficient().equals(rsaCrtSpec.getCrtCoefficient()))
            throw new Exception("coefficients not equal");
        if (!rsaPriKey.getPrimeExponentP().equals(rsaCrtSpec.getPrimeExponentP()))
            throw new Exception("primeExponentPs not equal");
        if (!rsaPriKey.getPrimeExponentQ().equals(rsaCrtSpec.getPrimeExponentQ()))
            throw new Exception("primeExponentQs not equal");
        if (!rsaPriKey.getPrimeP().equals(rsaCrtSpec.getPrimeP()))
            throw new Exception("primePs not equal");
        if (!rsaPriKey.getPrimeQ().equals(rsaCrtSpec.getPrimeQ()))
            throw new Exception("primeQs not equal");
        if (!rsaPriKey.getPublicExponent().equals(rsaCrtSpec.getPublicExponent()))
            throw new Exception("public exponents not equal");
        if (!rsaPriKey.getPrivateExponent().equals(rsaCrtSpec.getPrivateExponent()))
            throw new Exception("private exponents not equal");
        if (!rsaPriKey.getModulus().equals(rsaCrtSpec.getModulus()))
            throw new Exception("modulus not equal");
        if (!rsaPriKey.getFormat().equals("PKCS#8") &&
            !rsaPriKey.getFormat().equals("PKCS8"))
            throw new Exception("format not PKCS#8");
        if (!rsaPriKey.getAlgorithm().equals("RSA"))
            throw new Exception("algorithm not RSA");
        if (rsaPriKey.getEncoded() == null)
            throw new Exception("encoded key is null");

        PKCS8EncodedKeySpec pkcs8Key =
            new PKCS8EncodedKeySpec(rsaPriKey.getEncoded());

        RSAPrivateCrtKey rsaPriKey2
            = (RSAPrivateCrtKey) kf.generatePrivate(pkcs8Key);
        if (!Arrays.equals(rsaPriKey.getEncoded(), rsaPriKey2.getEncoded()))
            throw new Exception("encoded keys not equal");
    }