rsa加密 java
- IT科技
- 關注:3.12W次
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
java rsa加密是什麼?讓我們一起來了解一下吧!
java rsa加密是java中的一種非對稱加密算法。非對稱加密是指加密密鑰與加密祕鑰不一樣。我們經常看見的非對稱加密算法有兩種,分別是rsa和dsa。
應用rsa的時候我們一般需要公鑰與私鑰,但我們採用公鑰加密的時候,便使用私鑰解密;反之,採用私鑰加密的時候,便要使用公鑰進行解密。在java中,我們先應用genkeypair()函數產生公鑰與私鑰同時儲存在map集合裏面。接着,我們以產生的公鑰對明文進行加密。針對已加密的密文,我們再利用私鑰對其解密。
實戰演練,具體步驟如下:
package rsademo; import javax.crypto.Cipher;import java.nio.charset.StandardCharsets;import java.security.*;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.Base64; /** * @author 一隻鯊go * @title RSA_Test * @CreateTime 2021-05-13 */ public class RSA_Algorithm { private PrivateKey privateKey; private PublicKey publicKey; private static String algorithm = "RSA"; private static String signAlgorithm = "MD5withRSA"; public RSA_Algorithm() throws NoSuchAlgorithmException { //生成密鑰對對象 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); //生成密鑰對 KeyPair keyPair = keyPairGenerator.generateKeyPair(); //生成公鑰 this.publicKey = keyPair.getPublic(); //生成私鑰 this.privateKey = keyPair.getPrivate(); } /** * 公鑰字元串還原爲公鑰 * * @param publicKeyString 公鑰字元串 * @return 公鑰 * @throws Exception */ public Key publicKeyStringToKey(String publicKeyString) throws Exception { byte[] publicBytes = Base64.getDecoder().decode(publicKeyString); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicBytes)); return publicKey; } /** * 私鑰字元串還原爲私鑰 * * @param privateKeyString 私鑰字元串 * @return 私鑰 * @throws Exception */ public PrivateKey privateKeyStringToKey(String privateKeyString) throws Exception { byte[] privateBytes = Base64.getDecoder().decode(privateKeyString); KeyFactory keyFactory = KeyFactory.getInstance(algorithm); PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateBytes)); return privateKey; } /** * 返回公鑰字節數組 * * @return */ public byte[] publicKeyEncoded() { return this.publicKey.getEncoded(); } /** * 返回私鑰字節數組 * * @return */ public byte[] privateKeyEncoded() { return this.privateKey.getEncoded(); } /** * 公鑰byteToString轉碼 * * @return */ public String publicKeyToString() { return Base64.getEncoder().encodeToString(publicKeyEncoded()); } /** * 私鑰byteToString轉碼 * * @return */ public String privateKeyToString() { return Base64.getEncoder().encodeToString(privateKeyEncoded()); } /** * 公鑰加密 * * @param input 明文 * @param publicKey 公鑰 * @return 密文字元串 * @throws Exception */ public String pkEncoded(String input, String publicKey) throws Exception { byte[] bytes = input.getBytes(StandardCharsets.UTF_8); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, publicKeyStringToKey(publicKey)); byte[] cipherText = cipher.doFinal(bytes); return Base64.getEncoder().encodeToString(cipherText); } /** * 私鑰解密 * * @param cipherText 密文 * @param privateKey 私鑰 * @return 明文字元串 * @throws Exception */ public String skDecoded(String cipherText, String privateKey) throws Exception { byte[] cipherbytes = Base64.getDecoder().decode(cipherText); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, privateKeyStringToKey(privateKey)); byte[] input = cipher.doFinal(cipherbytes); return new String(input); } /** * 數字簽名:私鑰加密 * * @param signature 簽名明文字元串 * @param privateKey 私鑰字元串 * @return 簽名字元串 * @throws Exception */ public String skEncoded(String signature,String privateKey) throws Exception { Signature signature1 = Signature.getInstance(signAlgorithm); signature1.initSign(privateKeyStringToKey(privateKey)); signature1.update(signature.getBytes(StandardCharsets.UTF_8)); byte[] sign = signature1.sign(); return Base64.getEncoder().encodeToString(sign); } /** * 判斷簽名:公鑰解密 * @param input * @param signDate 簽名密文字元串 * @param publicKey 公鑰 * @return boolen * @throws Exception */ public boolean pkDecoded(String input,String signDate,String publicKey)throws Exception { Signature signature = Signature.getInstance(signAlgorithm); signature.initVerify(publicKeyStringToKey(publicKey)); signature.update(input.getBytes(StandardCharsets.UTF_8)); return signature.verify(Base64.getDecoder().decode(signDate)); } }
以上就是小編今天的分享了,希望可以幫助到大家。
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-hant/dianzi/gnmpkn.html