EncodedKeySpec
java.security.spec.EncodedKeySpec
PKCS8EncodedKeySpec
私钥的ASN.1编码(规范),编码按照PKCS#8标准:
PrivateKeyInfo ::= SEQUENCE { version Version, privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, privateKey PrivateKey, attributes [0] IMPLICIT Attributes OPTIONAL } Version ::= INTEGER PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier PrivateKey ::= OCTET STRING Attributes ::= SET OF Attribute
X509EncodedKeySpec
公钥的ASN.1编码(规范),编码按照X.509标准:
SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEApTHMs+zR16SE0v3npoVbMGo70xX5tJ7HF23SvM2rARkJNtRs4xZRQpWQ1JBZnGna9HcVQsrRAORqYhhTrZ9rMQIDAQABAkAFNrlYrasZErJGQEEiIWP9lwHCvZchLTB4j+TahIV+2iLTiLa21QOqQFmpBqw/uqmHsJGtqtHIXdtgCrGtoLVhAiEA4zpbZ52vkCskvZ2eJ34n6dTsiybZLIMYIdp21kd6mpECIQC6HJ2f0R6BwL/ORYhF6tA1YeXZEKyAuTgDkwgmGN/WoQIgPsXrZHeafbB9iOiXPX/LlPyekF6eFn7s1sVcmRvMEhECIBnJDS2vU4K2qdxyVccaGW7L+YRxgvTytIgKPv7IQ3sBAiEAh3XrxyuR3nJhFD5pPcRLmnst9Ag6WQuthc/SgkKJlXk="; String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUxzLPs0dekhNL956aFWzBqO9MV+bSexxdt0rzNqwEZCTbUbOMWUUKVkNSQWZxp2vR3FULK0QDkamIYU62fazECAwEAAQ=="; byte[] privateKeyEncoded = Base64.base64ToByteArray(privateKey); byte[] publicKeyEncoded = Base64.base64ToByteArray(publicKey);
// 加密 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyEncoded); KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } PrivateKey privateKey = null; try { privateKey = keyFactory.generatePrivate(pkcs8KeySpec); } catch (InvalidKeySpecException e) { Assert.fail("invalid key spec: " + e.getMessage()); } System.out.println("private key: " + Base64.byteArrayToBase64(((Key) privateKey).getEncoded()));
// 解密 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyEncoded); try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } PublicKey publicKey = null; try { publicKey = keyFactory.generatePublic(x509KeySpec); } catch (InvalidKeySpecException e) { Assert.fail("invalid key spec: " + e.getMessage()); } System.out.println("public key: " + Base64.byteArrayToBase64(((Key) publicKey).getEncoded()));
完整代码
/** * private key: MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEApTHMs+zR16SE0v3npoVbMGo70xX5tJ7HF23SvM2rARkJNtRs4xZRQpWQ1JBZnGna9HcVQsrRAORqYhhTrZ9rMQIDAQABAkAFNrlYrasZErJGQEEiIWP9lwHCvZchLTB4j+TahIV+2iLTiLa21QOqQFmpBqw/uqmHsJGtqtHIXdtgCrGtoLVhAiEA4zpbZ52vkCskvZ2eJ34n6dTsiybZLIMYIdp21kd6mpECIQC6HJ2f0R6BwL/ORYhF6tA1YeXZEKyAuTgDkwgmGN/WoQIgPsXrZHeafbB9iOiXPX/LlPyekF6eFn7s1sVcmRvMEhECIBnJDS2vU4K2qdxyVccaGW7L+YRxgvTytIgKPv7IQ3sBAiEAh3XrxyuR3nJhFD5pPcRLmnst9Ag6WQuthc/SgkKJlXk= * public key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUxzLPs0dekhNL956aFWzBqO9MV+bSexxdt0rzNqwEZCTbUbOMWUUKVkNSQWZxp2vR3FULK0QDkamIYU62fazECAwEAAQ== */ @Test public void test11() { String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEApTHMs+zR16SE0v3npoVbMGo70xX5tJ7HF23SvM2rARkJNtRs4xZRQpWQ1JBZnGna9HcVQsrRAORqYhhTrZ9rMQIDAQABAkAFNrlYrasZErJGQEEiIWP9lwHCvZchLTB4j+TahIV+2iLTiLa21QOqQFmpBqw/uqmHsJGtqtHIXdtgCrGtoLVhAiEA4zpbZ52vkCskvZ2eJ34n6dTsiybZLIMYIdp21kd6mpECIQC6HJ2f0R6BwL/ORYhF6tA1YeXZEKyAuTgDkwgmGN/WoQIgPsXrZHeafbB9iOiXPX/LlPyekF6eFn7s1sVcmRvMEhECIBnJDS2vU4K2qdxyVccaGW7L+YRxgvTytIgKPv7IQ3sBAiEAh3XrxyuR3nJhFD5pPcRLmnst9Ag6WQuthc/SgkKJlXk="; String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKUxzLPs0dekhNL956aFWzBqO9MV+bSexxdt0rzNqwEZCTbUbOMWUUKVkNSQWZxp2vR3FULK0QDkamIYU62fazECAwEAAQ=="; byte[] privateKeyEncoded = Base64.base64ToByteArray(privateKey); byte[] publicKeyEncoded = Base64.base64ToByteArray(publicKey); String message = "13120983870"; System.out.println(message); encryptAndDecrypt(message, privateKeyEncoded, publicKeyEncoded); }
private void encryptAndDecrypt(String message, byte[] privateKeyEncoded, byte[] publicKeyEncoded) { // 加密 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKeyEncoded); KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } PrivateKey privateKey = null; try { privateKey = keyFactory.generatePrivate(pkcs8KeySpec); } catch (InvalidKeySpecException e) { Assert.fail("invalid key spec: " + e.getMessage()); } System.out.println("private key: " + Base64.byteArrayToBase64(((Key) privateKey).getEncoded())); Cipher cipher = null; try { cipher = Cipher.getInstance(keyFactory.getAlgorithm()); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } catch (NoSuchPaddingException e) { Assert.fail("no such padding: " + e.getMessage()); } try { cipher.init(Cipher.ENCRYPT_MODE, privateKey); } catch (InvalidKeyException e) { Assert.fail("invalid key: " + e.getMessage()); } byte[] encryption = null; try { encryption = cipher.doFinal(message.getBytes()); } catch (IllegalBlockSizeException e) { Assert.fail("illegal block size: " + e.getMessage()); } catch (BadPaddingException e) { Assert.fail("bad padding: " + e.getMessage()); } System.out.println("encryption: " + Base64.byteArrayToBase64(encryption)); // 解密 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyEncoded); try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } PublicKey publicKey = null; try { publicKey = keyFactory.generatePublic(x509KeySpec); } catch (InvalidKeySpecException e) { Assert.fail("invalid key spec: " + e.getMessage()); } System.out.println("public key: " + Base64.byteArrayToBase64(((Key) publicKey).getEncoded())); try { cipher = Cipher.getInstance(keyFactory.getAlgorithm()); } catch (NoSuchAlgorithmException e) { Assert.fail("no such algorithm: " + e.getMessage()); } catch (NoSuchPaddingException e) { Assert.fail("no such padding: " + e.getMessage()); } try { cipher.init(Cipher.DECRYPT_MODE, publicKey); } catch (InvalidKeyException e) { Assert.fail("invalid key: " + e.getMessage()); } byte[] decryption = null; try { decryption = cipher.doFinal(encryption); } catch (IllegalBlockSizeException e) { Assert.fail("illegal block size: " + e.getMessage()); } catch (BadPaddingException e) { Assert.fail("bad padding: " + e.getMessage()); } System.out.println("decryption: " + new String(decryption) + ", base64:" + Base64.byteArrayToBase64(decryption)); }