Java Code Examples for java.security.interfaces.RSAPrivateKey#getEncoded()
The following examples show how to use
java.security.interfaces.RSAPrivateKey#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: RsaKeyTest.java From wycheproof with Apache License 2.0 | 5 votes |
@NoPresubmitTest( providers = {ProviderType.CONSCRYPT}, bugs = {"b/145113402"} ) @Test public void testEncodeDecodePrivate() throws Exception { int keySizeInBits = 2048; KeyFactory kf = KeyFactory.getInstance("RSA"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keySizeInBits); KeyPair keypair = keyGen.genKeyPair(); RSAPrivateKey priv = (RSAPrivateKey) keypair.getPrivate(); assertTrue("Expecting an RSA private key with CRT parameters", priv instanceof RSAPrivateCrtKey); byte[] encoded = priv.getEncoded(); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded); RSAPrivateKey priv2 = (RSAPrivateKey) kf.generatePrivate(spec); // Checks that priv2 equivalent to priv1. assertEquals(priv2.getModulus(), priv.getModulus()); assertEquals(priv2.getPrivateExponent(), priv.getPrivateExponent()); // Some implementations are not subclasses of RSAPrivateCtrKey. // E.g. org.conscrypt.OpenSSLRSAPrivateKey. // But there is OpenSSLRSAPrivateCrtKey if (priv instanceof RSAPrivateCrtKey) { checkPrivateCrtKey((RSAPrivateCrtKey) priv, keySizeInBits); } else { // Using a CRT key leads to 6-7 times better performance than not using the CRT. fail("Expecting an RSAPrivateCrtKey instead of " + priv.getClass().getName()); } }
Example 2
Source File: KeyResourceImpl.java From authlib-agent with MIT License | 5 votes |
@Override public byte[] getEncodedKey() { if (!allowDownloadPrivateKey) { throw new ForbiddenException("It is not allowed to download the private key"); } RSAPrivateKey key = signatureService.getKey(); return key == null ? new byte[0] : key.getEncoded(); }
Example 3
Source File: RSAEncryptCoder.java From onetwo with Apache License 2.0 | 5 votes |
public RSAEncryptCoder(int size, boolean generatedKeyPair){ this.size = size; this.encryptSize = size/8-11; this.dencryptSize = size/8; if(generatedKeyPair){ KeyPair kp = generatedKey(); RSAPublicKey pubkey = (RSAPublicKey)kp.getPublic(); publicKey = pubkey.getEncoded(); RSAPrivateKey prikey = (RSAPrivateKey)kp.getPrivate(); privateKey = prikey.getEncoded(); } }
Example 4
Source File: RSATest.java From java_security with MIT License | 5 votes |
/** * * @author timliu * 说明: 用java的jdk里面相关方法实现rsa的签名及签名验证 */ public static void jdkRSA() { try { // 1.初始化密钥 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate(); // 2.进行签名 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(src.getBytes()); byte[] result = signature.sign(); System.out.println("jdk rsa sign:" + Hex.encodeHexString(result) ); // 3.验证签名 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); signature = Signature.getInstance("MD5withRSA"); signature.initVerify(publicKey); signature.update(src.getBytes()); boolean bool = signature.verify(result); System.out.println("jdk rsa verify:" + bool); } catch (Exception e) { System.out.println(e.toString()); } }
Example 5
Source File: Main.java From keystore-decryptor with Apache License 2.0 | 5 votes |
private static void showJcaPrivateKey(PrivateKey pk) throws Exception { if (pk instanceof RSAPrivateKey) { RSAPrivateKey rsaPrivKey = (RSAPrivateKey) pk; PemObject rsaPem = new PemObject("RSA PRIVATE KEY", rsaPrivKey.getEncoded()); StringWriter sw = new StringWriter(); PemWriter pemWriter = new PemWriter(sw); try { pemWriter.writeObject(rsaPem); } finally { pemWriter.close(); } System.out.println(sw.toString()); } else if (pk instanceof java.security.interfaces.ECPrivateKey) { java.security.interfaces.ECPrivateKey ecPrivKey = (java.security.interfaces.ECPrivateKey) pk; System.out.printf("EC S: %s... (%d)\n", ecPrivKey.getS().toString(16).substring(0, 32), ecPrivKey.getS().bitLength()); if (ecPrivKey.getParams() instanceof ECNamedCurveSpec) { ECNamedCurveSpec namedCurveSpec = (ECNamedCurveSpec) ecPrivKey.getParams(); System.out.println("curve name: " + namedCurveSpec.getName()); } else { System.out.println("EC params: " + ecPrivKey.getParams()); } } else if (pk instanceof DSAPrivateKey) { DSAPrivateKey dsaPrivKey = (DSAPrivateKey) pk; System.out.printf("DSA X: %s... (%d)\n", dsaPrivKey.getX().toString(16).substring(0, 32), dsaPrivKey.getX() .bitLength()); System.out.println("DSA params: " + dsaPrivKey.getParams()); } else { System.out.println("Unknown private key type: " + pk.getClass().getName()); } }
Example 6
Source File: PrivateKeyEqualityTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 7
Source File: PrivateKeyEqualityTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 8
Source File: PrivateKeyEqualityTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 9
Source File: PrivateKeyEqualityTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 10
Source File: PrivateKeyEqualityTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 11
Source File: PrivateKeyEqualityTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 12
Source File: PrivateKeyEqualityTest.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
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"); } }
Example 13
Source File: RSATest.java From java_security with MIT License | 4 votes |
public static void jdkRSA() { try { // 1.初始化发送方密钥 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); System.out.println("Public Key:" + Base64.encodeBase64String(rsaPublicKey.getEncoded())); System.out.println("Private Key:" + Base64.encodeBase64String(rsaPrivateKey.getEncoded())); // 2.私钥加密、公钥解密 ---- 加密 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("私钥加密、公钥解密 ---- 加密:" + Base64.encodeBase64String(result)); // 3.私钥加密、公钥解密 ---- 解密 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded()); keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); result = cipher.doFinal(result); System.out.println("私钥加密、公钥解密 ---- 解密:" + new String(result)); // 4.公钥加密、私钥解密 ---- 加密 X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(rsaPublicKey.getEncoded()); KeyFactory keyFactory2 = KeyFactory.getInstance("RSA"); PublicKey publicKey2 = keyFactory2.generatePublic(x509EncodedKeySpec2); Cipher cipher2 = Cipher.getInstance("RSA"); cipher2.init(Cipher.ENCRYPT_MODE, publicKey2); byte[] result2 = cipher2.doFinal(src.getBytes()); System.out.println("公钥加密、私钥解密 ---- 加密:" + Base64.encodeBase64String(result2)); // 5.私钥解密、公钥加密 ---- 解密 PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded()); KeyFactory keyFactory5 = KeyFactory.getInstance("RSA"); PrivateKey privateKey5 = keyFactory5.generatePrivate(pkcs8EncodedKeySpec5); Cipher cipher5 = Cipher.getInstance("RSA"); cipher5.init(Cipher.DECRYPT_MODE, privateKey5); byte[] result5 = cipher5.doFinal(result2); System.out.println("公钥加密、私钥解密 ---- 解密:" + new String(result5)); } catch (Exception e) { e.printStackTrace(); } }