最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

密碼系統(tǒng)AES私鑰RSA公鑰的加解密示例

 更新時間:2022年03月07日 16:54:26   作者:kl  
這篇文章主要為大家詮釋并介紹了AES私鑰RSA公鑰的加解密系統(tǒng)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步

前言

密鑰是成對存在的,加密和解密是采用不同的密鑰(公開密鑰),也就是非對稱密鑰密碼系統(tǒng),每個通信方均需要兩個密鑰,即公鑰和私鑰,使用公鑰進行加密操作,使用私鑰進行解密操作。公鑰是公開的,不需要保密,而私鑰是由個人自己持有,并且必須妥善保管和注意保密。密碼學里面博大精深,下面的實例僅供參考

百科的詮釋

公鑰(Public Key)與私鑰(Private Key)是通過一種算法得到的一個密鑰對(即一個公鑰和一個私鑰),公鑰是密鑰對中公開的部分,私鑰則是非公開的部分。公鑰通常用于加密會話密鑰、驗證數字簽名,或加密可以用相應的私鑰解密的數據。通過這種算法得到的密鑰對能保證在世界范圍內是唯一的。使用這個密鑰對的時候,如果用其中一個密鑰加密一段數據,必須用另一個密鑰解密。比如用公鑰加密數據就必須用私鑰解密,如果用私鑰加密也必須用公鑰解密,否則解密將不會成功。

java使用公私鑰加解密的實例

僅供參考

/**
     * 數據加密 plainTextData要加密的字符串
     * @param plainTextData
     * @return
     * @throws Exception
     */
    public static Map encrypt(String plainTextData)
            throws Exception {
        HashMap result = new HashMap();
        // keySpec 生成對稱密鑰
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
        // RSA 用對方公鑰對‘對稱密鑰'進行加密
        Cipher cipher = Cipher.getInstance("RSA");
        String keyFilePathName = pertery.getProperty("bsbank_Key_path")+"PublicKey.keystore";
        cipher.init(Cipher.WRAP_MODE,
                loadPublicKeyByStr(loadKeyByFile(keyFilePathName)));
        byte[] wrappedKey = cipher.wrap(keySpec);
        result.put("wrappedKey", Base64.encodeBase64String(wrappedKey));
        // 加密數據
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encryptedData = cipher.doFinal(plainTextData.getBytes("UTF-8"));
        result.put("encryptedData", Base64.encodeBase64String(encryptedData));
        return result;
    }
    /**
     * 數據解密 encryptedData
     * @param encryptedData
     * @return
     * @throws Exception
     */
    public static Map decrypt(Map encryptedData)
            throws Exception {
        // 獲取密鑰
        byte[] wrappedKey = Base64.decodeBase64(encryptedData.get("wrappedKey")
                .toString());
        HashMap result = new HashMap();
        // RSA解密密鑰
        Cipher cipher = Cipher.getInstance("RSA");
        String keyFilePathName = pertery.getProperty("bsbank_Key_path")+"privateKey.keystore";//使用對方的私鑰解密
        cipher.init(Cipher.UNWRAP_MODE,
                loadPrivateKeyByStr(loadKeyByFile(keyFilePathName)));
        Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
        // 解密數據
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(encryptedData
                .get("encryptedData").toString()));
        result.put("decryptedData", new String(decryptedData, "UTF-8"));
        result.put("wrappedKey", Base64.encodeBase64String(wrappedKey));
        return result;
    }
    private static String loadKeyByFile(String filePathName) throws Exception {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            br = new BufferedReader(new FileReader(filePathName));
            String readLine = null;
            while ((readLine = br.readLine()) != null) {
                sb.append(readLine);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != br) {
                br.close();
            }
        }
        return sb.toString();
    }
    private static RSAPublicKey loadPublicKeyByStr(String publicKeyStr)
            throws Exception {
        RSAPublicKey publicKey = null;
        try {
            byte[] buffer = Base64.decodeBase64(publicKeyStr);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            logger.error("failed to load pubKey", e);
            throw e;
        }
        return publicKey;
    }
    private static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr)
            throws Exception {
        RSAPrivateKey privateKey = null;
        try {
            byte[] buffer = Base64.decodeBase64(privateKeyStr);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
            privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            logger.error("failed to loadPrivateKeyByStr", e);
            throw e;
        }
        return privateKey;
    }
    /**
     * 輸出公私鑰對
     * @param filePath
     * @throws Exception
     */
    private static void genKeyPair(String filePath) throws Exception {
        KeyPairGenerator keyPairGen = null;
        try {
            keyPairGen = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            logger.error("failed to do key gen", e);
            throw e;
        }
        keyPairGen.initialize(1024, new SecureRandom());
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        try {
            String publicKeyString = Base64.encodeBase64String(publicKey
                    .getEncoded());
            String privateKeyString = Base64.encodeBase64String(privateKey
                    .getEncoded());
            FileWriter pubfw = new FileWriter(filePath + "/PublicKey.keystore");
            FileWriter prifw = new FileWriter(filePath + "/PrivateKey.keystore");
            BufferedWriter pubbw = new BufferedWriter(pubfw);
            BufferedWriter pribw = new BufferedWriter(prifw);
            pubbw.write(publicKeyString);
            pribw.write(privateKeyString);
            pubbw.flush();
            pubbw.close();
            pubfw.close();
            pribw.flush();
            pribw.close();
            prifw.close();
        } catch (IOException e) {
            logger.error("failed to genKeypair", e);
        }
    }

以上就是詮釋AES私鑰RSA公鑰的加解密示例的詳細內容,更多關于AES RSA公私鑰加解密的資料請關注腳本之家其它相關文章!

相關文章

  • Java?二維數組創(chuàng)建及使用方式

    Java?二維數組創(chuàng)建及使用方式

    這篇文章主要介紹了Java?二維數組創(chuàng)建及使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • springboot如何獲取yaml/yml(或properties)配置文件信息

    springboot如何獲取yaml/yml(或properties)配置文件信息

    在SpringBoot項目中,讀取配置文件信息是常見需求,可以通過@Autowired注入Environment類,使用@Value注解直接注入配置信息,或定義工具類結合ApplicationRunner進行高級配置信息獲取,特別提到
    2024-11-11
  • JAVA位運算的知識點總結

    JAVA位運算的知識點總結

    在本篇文章里小編給大家整理的是關于JAVA有關位運算的全套梳理,需要的朋友們可以參考學習下。
    2020-03-03
  • Java中Redis存儲String類型會有亂碼的問題及解決方案

    Java中Redis存儲String類型會有亂碼的問題及解決方案

    在java中使用Redis存儲String類型的數據時,會出現亂碼,我寫了一條存儲key為name,值為虎哥的字符串,然后獲取一下這個key為name的值,打印得到的值,下面通過實例代碼介紹Java中Redis存儲String類型會有亂碼的問題及解決方案,一起看看吧
    2024-04-04
  • Java文件復制多種方法實例代碼

    Java文件復制多種方法實例代碼

    近期用到文件復制,雖然程序很簡單,因為時間久了淡忘了,所以寫一篇文章記錄一下,下面這篇文章主要給大家介紹了關于Java文件復制多種方法的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Java常用集合之Set和Map的用法詳解

    Java常用集合之Set和Map的用法詳解

    這篇文章將通過一些示例為大家詳細介紹一下Java常用集合中Set和Map的用法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-07-07
  • Springboot單元測試無法讀取配置文件的解決方案

    Springboot單元測試無法讀取配置文件的解決方案

    這篇文章主要介紹了Springboot單元測試無法讀取配置文件的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 深入學習MyBatis中的參數(推薦)

    深入學習MyBatis中的參數(推薦)

    大家日常使用MyBatis經常會遇到一些異常,想要避免參數引起的錯誤,我們需要深入了解參數。想了解參數,我們首先看MyBatis處理參數和使用參數的全部過程。下面這篇文章主要給大家介紹了MyBatis中參數的的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • jenkins配置詳細指南(附jdk多個版本配置)

    jenkins配置詳細指南(附jdk多個版本配置)

    Jenkins是一款CICD(持續(xù)集成與持續(xù)交付)工具,Jenkins可以幫你在寫完代碼后,一鍵完成開發(fā)過程中的一系列自動化部署的工作,這篇文章主要給大家介紹了關于jenkins配置的相關資料,文中還附jdk多個版本配置指南,需要的朋友可以參考下
    2024-02-02
  • JMeter參數化4種實現方式(小結)

    JMeter參數化4種實現方式(小結)

    參數化是自動化測試腳本的一種常用技巧,可將腳本中的某些輸入使用參數來代替,JMeter提供了多種參數化方式,下面就其中常用的4種展開闡述,感興趣的可以來了解一下
    2021-12-12

最新評論

南通市| 澎湖县| 潞西市| 西峡县| 东安县| 哈尔滨市| 金昌市| 高密市| 岢岚县| 津南区| 云阳县| 和平区| 蓝山县| 杂多县| 额敏县| 南宫市| 福贡县| 西宁市| 罗田县| 乌兰察布市| 同心县| 乌兰察布市| 岫岩| 特克斯县| 林州市| 井陉县| 天祝| 万全县| 秀山| 从江县| 内丘县| 合水县| 正镶白旗| 利川市| 忻城县| 通渭县| 西平县| 长兴县| 武功县| 儋州市| 高阳县|