You are being redirected. ITeye Java编程 Spring框架 Ajax技术 agile敏捷软件开发 ruby on rails实践 - ITeye做最棒的软件开发交流社区 - 乐虎国际|授权平台
论坛首页 编程语言技术论坛

Java PKCS8EncodedKeySpec & X509EncodedKeySpec

浏览 2610 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2019-01-15  
  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("encryption: " + new String(decryption) + ", base64:" + Base64.byteArrayToBase64(decryption)); }          
   发表时间:2019-01-16  
字符串 长了 你这就不好用了  
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics