<sup id="m40ya"></sup>
  • 
    
  • <kbd id="m40ya"></kbd>
    <samp id="m40ya"></samp>
    <ul id="m40ya"></ul>
  • 更多精彩內容,歡迎關注:

    視頻號
    視頻號

    抖音
    抖音

    快手
    快手

    微博
    微博

    java rsa加密

    文檔

    java rsa加密

    java rsa加密是java中的一種非對稱加密算法,應用rsa的時候一般需要公鑰與私鑰,但我們采用公鑰加密的時候,便使用私鑰解密;反之,采用私鑰加密的時候,便要使用公鑰進行解密。
    推薦度:
    導讀java rsa加密是java中的一種非對稱加密算法,應用rsa的時候一般需要公鑰與私鑰,但我們采用公鑰加密的時候,便使用私鑰解密;反之,采用私鑰加密的時候,便要使用公鑰進行解密。

    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));
    ????}
    ?
    }

    ?以上就是小編今天的分享了,希望可以幫助到大家。

    文檔

    java rsa加密

    java rsa加密是java中的一種非對稱加密算法,應用rsa的時候一般需要公鑰與私鑰,但我們采用公鑰加密的時候,便使用私鑰解密;反之,采用私鑰加密的時候,便要使用公鑰進行解密。
    推薦度:
    為你推薦
    資訊專欄
    熱門視頻
    相關推薦
    java path java paypal java phoenix java ping java pipeline java rsa公鑰加密 java rsa私鑰加密 java rtp java runnable java runtime.exec java rxjava java sandbox java script java selector java selenium java semaphore java separator java sequence java serializable java serializable接口 java rotate java reverse java reverse() java retrofit java resume java resultset java result java rest java requests java partial java request java parser java repeat java parse java pandas java override java render java region java require java resolve
    Top 久久996热精品xxxx| 日产精品卡一卡二卡三的概述| 国产尤物在线视精品在亚洲| 亚洲国产精品99久久久久久| 亚洲国产精品无码久久久蜜芽| av无码精品一区二区三区四区| 99视频精品全部免费观看| 国产精品毛多多水多| 国产精品三级在线| 亚洲第一精品在线视频| 中文字幕精品亚洲无线码二区| 51午夜精品免费视频| 国产精品久久久久久无毒不卡| 人妻熟妇乱又伦精品HD| 精品国际久久久久999波多野| 久久91精品国产一区二区| 亚洲精品乱码久久久久久蜜桃不卡| 国产精品gz久久久| 国产精品久久久久国产精品三级| 亚洲精品蜜夜内射| 国产精品三级视频| 国产99re在线观看只有精品 | aaaaaa精品视频在线观看| 国产看午夜精品理论片| 精品久久久无码中文字幕边打电话| 亚洲午夜成人精品无码色欲| 91麻豆精品国产自产在线| 久久久久无码精品国产| 99精品免费观看| 99久久99久久久99精品齐| 少妇伦子伦精品无码STYLES| 久久久久久久99精品国产片| 国产精品水嫩水嫩| 亚洲AV无码成人精品区蜜桃| 国产成人无码精品一区二区三区| 亚洲精品国产精品乱码不99 | 国产成人无码精品久久久性色| 国产免费伦精品一区二区三区| 国产精品免费看久久久香蕉| 亚洲精品黄色视频在线观看免费资源| www好男人精品视频在线观看|