欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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

百科的詮釋

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

java使用公私鑰加解密的實(shí)例

僅供參考

/**
     * 數(shù)據(jù)加密 plainTextData要加密的字符串
     * @param plainTextData
     * @return
     * @throws Exception
     */
    public static Map encrypt(String plainTextData)
            throws Exception {
        HashMap result = new HashMap();
        // keySpec 生成對(duì)稱密鑰
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
        // RSA 用對(duì)方公鑰對(duì)‘對(duì)稱密鑰'進(jìn)行加密
        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));
        // 加密數(shù)據(jù)
        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;
    }
    /**
     * 數(shù)據(jù)解密 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";//使用對(duì)方的私鑰解密
        cipher.init(Cipher.UNWRAP_MODE,
                loadPrivateKeyByStr(loadKeyByFile(keyFilePathName)));
        Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
        // 解密數(shù)據(jù)
        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;
    }
    /**
     * 輸出公私鑰對(duì)
     * @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公鑰的加解密示例的詳細(xì)內(nèi)容,更多關(guān)于AES RSA公私鑰加解密的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題

    解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題

    這篇文章主要介紹了解決spring結(jié)合mybatis時(shí)一級(jí)緩存失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • Java異常處理try?catch的基本用法

    Java異常處理try?catch的基本用法

    try就像一個(gè)網(wǎng),把try{}里面的代碼所拋出的異常都網(wǎng)住,然后把異常交給catch{}里面的代碼去處理。最后執(zhí)行finally之中的代碼。無(wú)論try中代碼有沒有異常,也無(wú)論catch是否將異常捕獲到,finally中的代碼都一定會(huì)被執(zhí)行。
    2021-12-12
  • 詳解Spring如何解析占位符

    詳解Spring如何解析占位符

    Spring一直支持將屬性定義到外部的屬性的文件中,并使用占占位符的形式為使用"${}"包裝的屬性名稱,為了使用屬性占位符,我們必須配置一個(gè)PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer實(shí)例,本文將介紹如何解析占位符
    2021-06-06
  • Spring?JDBC?框架簡(jiǎn)介

    Spring?JDBC?框架簡(jiǎn)介

    Spring?JDBC?提供幾種方法和數(shù)據(jù)庫(kù)中相應(yīng)的不同的類與接口。我將給出使用JdbcTemplate類框架的經(jīng)典和最受歡迎的方法。本文給大家介紹Spring?JDBC?框架的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-12-12
  • Java多線程案例實(shí)戰(zhàn)之定時(shí)器的實(shí)現(xiàn)

    Java多線程案例實(shí)戰(zhàn)之定時(shí)器的實(shí)現(xiàn)

    在Java中可以使用多線程和定時(shí)器來(lái)實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時(shí)器實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解

    Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解

    這篇文章主要介紹了Java數(shù)據(jù)庫(kù)連接PreparedStatement的使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • java中的Object類的toSpring()方法

    java中的Object類的toSpring()方法

    這篇文章主要介紹了java中的Object類的toSpring()方法,Object是類層次結(jié)構(gòu)的根,每個(gè)類都可以將Object作為超類。所有類都直接或者間接的繼承自該類,下文相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Java內(nèi)存模型知識(shí)詳解

    Java內(nèi)存模型知識(shí)詳解

    這篇文章主要介紹了Java內(nèi)存模型知識(shí)詳解,文中通過(guò)對(duì)內(nèi)存訪問時(shí)的交互關(guān)系圖解介紹的十分詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例

    Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例

    這篇文章主要介紹了Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能,簡(jiǎn)單分析了計(jì)算機(jī)數(shù)值進(jìn)制轉(zhuǎn)換與通過(guò)位移進(jìn)行乘積計(jì)算的原理,并結(jié)合具體實(shí)例給出了java模擬計(jì)算機(jī)成績(jī)運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • 詳解SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳

    詳解SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳

    本篇文章主要介紹了SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳,本地的文件上傳到資源服務(wù)器上,比較好的辦法就是通過(guò)ftp上傳。這里是結(jié)合SpringMVC+ftp的形式上傳的,有興趣的可以了解一下。
    2016-12-12

最新評(píng)論