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

    視頻號
    視頻號

    抖音
    抖音

    快手
    快手

    微博
    微博

    java rsa私鑰加密

    文檔

    java rsa私鑰加密

    javarsa私鑰加密是一種加密算法,RSA加密注意事項:1、首先產生公鑰與私鑰。2、 設計加密與解密的算法。3、私鑰加密的數據信息只能由公鑰可以解密。4、公鑰加密的數據信息只能由私鑰可以解密。
    推薦度:
    導讀javarsa私鑰加密是一種加密算法,RSA加密注意事項:1、首先產生公鑰與私鑰。2、 設計加密與解密的算法。3、私鑰加密的數據信息只能由公鑰可以解密。4、公鑰加密的數據信息只能由私鑰可以解密。

    java rsa私鑰加密是什么?讓我們一起來了解一下吧!

    java rsa私鑰加密是一種加密算法。私鑰加密算法是用私鑰來進行加密與解密信息。私鑰加密也被稱作對稱加密,原因是加密與解密使用的秘鑰是同一個。

    RSA加密需要注意的事項如下:

    1.?首先產生公鑰與私鑰

    2.?設計加密與解密的算法

    3.?私鑰加密的數據信息只能由公鑰可以解密

    4.?公鑰加密的數據信息只能由私鑰可以解密

    實戰演練,具體步驟如下:

    public?class?RsaCryptTools?{
    ????private?static?final?String?CHARSET?=?"utf-8";
    ????private?static?final?Base64.Decoder?decoder64?=?Base64.getDecoder();
    ????private?static?final?Base64.Encoder?encoder64?=?Base64.getEncoder();
    ?
    ????/**
    ?????*?生成公私鑰
    ?????*?@param?keySize
    ?????*?@return
    ?????*?@throws?NoSuchAlgorithmException
    ?????*/
    ????public?static?SecretKey?generateSecretKey(int?keySize)?throws?NoSuchAlgorithmException?{
    ????????//生成密鑰對
    ????????KeyPairGenerator?keyGen?=?KeyPairGenerator.getInstance("RSA");
    ????????keyGen.initialize(keySize,?new?SecureRandom());
    ????????KeyPair?pair?=?keyGen.generateKeyPair();
    ????????PrivateKey?privateKey?=?pair.getPrivate();
    ????????PublicKey?publicKey?=?pair.getPublic();
    ????????//這里可以將密鑰對保存到本地
    ????????return?new?SecretKey(encoder64.encodeToString(publicKey.getEncoded()),?encoder64.encodeToString(privateKey.getEncoded()));
    ????}
    ????/**
    ?????*?私鑰加密
    ?????*?@param?data
    ?????*?@param?privateInfoStr
    ?????*?@return
    ?????*?@throws?IOException
    ?????*?@throws?InvalidCipherTextException
    ?????*/
    ????public?static?String?encryptData(String?data,?String?privateInfoStr)?throws?IOException,?InvalidKeySpecException,?NoSuchAlgorithmException,?InvalidKeyException,?NoSuchPaddingException,?BadPaddingException,?IllegalBlockSizeException?{
    ?
    ????????Cipher?cipher?=?Cipher.getInstance("RSA/ECB/PKCS1Padding");
    ????????cipher.init(Cipher.ENCRYPT_MODE,?getPrivateKey(privateInfoStr));
    ????????return?encoder64.encodeToString(cipher.doFinal(data.getBytes(CHARSET)));
    ????}
    ?
    ????/**
    ?????*?公鑰解密
    ?????*?@param?data
    ?????*?@param?publicInfoStr
    ?????*?@return
    ?????*/
    ????public?static?String?decryptData(String?data,?String?publicInfoStr)?throws?NoSuchPaddingException,?NoSuchAlgorithmException,?InvalidKeySpecException,?InvalidKeyException,?BadPaddingException,?IllegalBlockSizeException,?UnsupportedEncodingException?{
    ????????byte[]?encryptDataBytes=decoder64.decode(data.getBytes(CHARSET));
    ????????//解密
    ????????Cipher?cipher?=?Cipher.getInstance("RSA/ECB/PKCS1Padding");
    ????????cipher.init(Cipher.DECRYPT_MODE,?getPublicKey(publicInfoStr));
    ????????return?new?String(cipher.doFinal(encryptDataBytes),?CHARSET);
    ????}
    ????private?static?PublicKey?getPublicKey(String?base64PublicKey)?throws?NoSuchAlgorithmException,?InvalidKeySpecException?{
    ????????X509EncodedKeySpec?keySpec?=?new?X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
    ????????KeyFactory?keyFactory?=?KeyFactory.getInstance("RSA");
    ????????return?keyFactory.generatePublic(keySpec);
    ????}
    ????private?static?PrivateKey?getPrivateKey(String?base64PrivateKey)?throws?NoSuchAlgorithmException,?InvalidKeySpecException?{
    ????????PrivateKey?privateKey?=?null;
    ????????PKCS8EncodedKeySpec?keySpec?=?new?PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes()));
    ????????KeyFactory?keyFactory?=?null;
    ????????keyFactory?=?KeyFactory.getInstance("RSA");
    ????????privateKey?=?keyFactory.generatePrivate(keySpec);
    ????????return?privateKey;
    ????}
    ?
    ????/**
    ?????*?密鑰實體
    ?????*?@author?hank
    ?????*?@since?2020/2/28?0028?下午?16:27
    ?????*/
    ????public?static?class?SecretKey?{
    ????????/**
    ?????????*?公鑰
    ?????????*/
    ????????private?String?publicKey;
    ????????/**
    ?????????*?私鑰
    ?????????*/
    ????????private?String?privateKey;
    ?
    ????????public?SecretKey(String?publicKey,?String?privateKey)?{
    ????????????this.publicKey?=?publicKey;
    ????????????this.privateKey?=?privateKey;
    ????????}
    ?
    ????????public?String?getPublicKey()?{
    ????????????return?publicKey;
    ????????}
    ?
    ????????public?void?setPublicKey(String?publicKey)?{
    ????????????this.publicKey?=?publicKey;
    ????????}
    ?
    ????????public?String?getPrivateKey()?{
    ????????????return?privateKey;
    ????????}
    ?
    ????????public?void?setPrivateKey(String?privateKey)?{
    ????????????this.privateKey?=?privateKey;
    ????????}
    ?
    ????????@Override
    ????????public?String?toString()?{
    ????????????return?"SecretKey{"?+
    ????????????????????"publicKey='"?+?publicKey?+?'\''?+
    ????????????????????",?privateKey='"?+?privateKey?+?'\''?+
    ????????????????????'}';
    ????????}
    ????}
    ?
    ????private?static?void?writeToFile(String?path,?byte[]?key)?throws?IOException?{
    ????????File?f?=?new?File(path);
    ????????f.getParentFile().mkdirs();
    ?
    ????????try(FileOutputStream?fos?=?new?FileOutputStream(f))?{
    ????????????fos.write(key);
    ????????????fos.flush();
    ????????}
    ????}
    ?
    ????public?static?void?main(String[]?args)?throws?NoSuchAlgorithmException,?NoSuchPaddingException,?IOException,?BadPaddingException,?IllegalBlockSizeException,?InvalidKeyException,?InvalidKeySpecException?{
    ????????SecretKey?secretKey?=?generateSecretKey(2048);
    ????????System.out.println(secretKey);
    ????????String?enStr?=?encryptData("你好測試測試",?secretKey.getPrivateKey());
    ????????System.out.println(enStr);
    ????????String?deStr?=?decryptData(enStr,?secretKey.getPublicKey());
    ????????System.out.println(deStr);
    ????????enStr?=?encryptData("你好測試測試hello",?secretKey.getPrivateKey());
    ????????System.out.println(enStr);
    ????????deStr?=?decryptData(enStr,?secretKey.getPublicKey());
    ????????System.out.println(deStr);
    ????}
    ?
    }

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

    文檔

    java rsa私鑰加密

    javarsa私鑰加密是一種加密算法,RSA加密注意事項:1、首先產生公鑰與私鑰。2、 設計加密與解密的算法。3、私鑰加密的數據信息只能由公鑰可以解密。4、公鑰加密的數據信息只能由私鑰可以解密。
    推薦度:
    為你推薦
    資訊專欄
    熱門視頻
    相關推薦
    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 serialversionuid java setlayout java platform java settimeout java plug-in java sftp java plugin java rsa公鑰加密 java pipeline java ping java phoenix java paypal java path java rsa加密 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
    Top 久久精品国产99久久无毒不卡| 国产99视频精品免费视频7| 国色精品va在线观看免费视频| 亚洲精品无码av中文字幕| 99久久国产综合精品swag| 91精品国产高清91久久久久久| 国产精品露脸国语对白河北| 国产成人精品久久二区二区 | 国产精品一级香蕉一区| 亚洲av永久无码精品秋霞电影影院| 亚洲国产精品综合久久网络| 热久久精品免费视频| 亚洲欧美精品午睡沙发| 91精品国产品国语在线不卡| 久久精品视频免费看| 日韩人妻无码精品系列| 国产成人精品久久| 午夜国产精品无套| 国产亚洲精品成人久久网站| 成人国产精品视频频| 尤物精品视频一区二区三区| 97热久久免费频精品99| 久久精品国产亚洲AV久| 精品亚洲成a人片在线观看| 久久99精品久久久久久国产 | 久久精品视频亚洲| 国产在线精品一区二区三区直播| 国产精品男男视频一区二区三区| 四虎国产精品成人| 大桥未久在线精品视频在线| 免费精品国自产拍在线播放 | 99re热这里只有精品视频中文字幕| 国产高清国内精品福利99久久| 精品一区中文字幕| 无码国模国产在线无码精品国产自在久国产 | 国产中文在线亚洲精品官网| 成人精品视频一区二区三区| 成人无码精品1区2区3区免费看| 国产午夜精品一本在线观看| 国内精品伊人久久久久777| 亚洲精品无码专区在线在线播放 |