久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Java 進行 AES 加密和解密

AES Encryption and Decryption with Java(使用 Java 進行 AES 加密和解密)
本文介紹了使用 Java 進行 AES 加密和解密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是我正在做的事情,看起來有點笨拙,但對于這個問題的任何幫助表示贊賞.我得到一個 BadPaddingException.閱讀幾乎所有相關主題,但沒有找到合適的解決方案.我是加密解密編程的新手,需要在我的一個 Java 應用程序中實現它.

Here is what I am doing which can look a bit clumsy but any help is appreciated regarding the problem. I'm getting a BadPaddingException. Read almost all related topics but didn't find the appropriate solution. I am new to encryption decryption programming and need to implement it in one of my Java application.

謝謝你..這就是代碼的樣子......

Thank You.. this is how the code looks....

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    // TODO Auto-generated method stub
            String FileName="encryptedtext.txt";
            String FileName2="decryptedtext.txt";
            String pad="0"; 

            KeyGenerator KeyGen=KeyGenerator.getInstance("AES");
            KeyGen.init(128);

            SecretKey SecKey=KeyGen.generateKey();

            Cipher AesCipher=Cipher.getInstance("AES");
            AesCipher.init(Cipher.ENCRYPT_MODE,SecKey);

            byte[] byteText="My name is yogesh".getBytes();
            byte[] byteCipherText=AesCipher.doFinal(byteText);
            String cipherText = null;

            try {
                FileWriter fw=new FileWriter(FileName);
                BufferedWriter bw=new BufferedWriter(fw);
                bw.write(byteCipherText.toString());
                bw.close();
            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                FileReader fr=new FileReader(FileName);
                BufferedReader br=new BufferedReader(fr);
                cipherText=br.readLine();
                br.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            AesCipher.init(Cipher.DECRYPT_MODE,SecKey);
            while(((cipherText.getBytes().length)%16)!=0)
            {
                cipherText=cipherText+pad;


            }

            byte[] bytePlainText=AesCipher.doFinal(cipherText.getBytes());
            FileWriter fw1;
            try {
                fw1 = new FileWriter(FileName2);
                BufferedWriter bw1=new BufferedWriter(fw1);
                bw1.write(bytePlainText.toString());
                bw1.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }






}

推薦答案

這里你要明白的是,密文可能包含不可打印的字符.因此,當您使用 readLine() 時,它可能不會為您提供文件中的所有字節.

Here, what you have to understand is that cipher text may contain non-printable characters. So, when you use readLine(), it will likely not give you all of the bytes in the file.

另外,byteCipherText.toString() 并沒有給你你認為你會得到的東西.在java中,toString()方法不給出數組內容的字符串表示.

Also, byteCipherText.toString() does not give you what you thought you would get. In java, the toString() method does not give the string representation of the contents of the array.

無需為加密文本添加填充.它已經被填充了.

There is no need to add padding to encrypted text. It is already padded.

import java.nio.file.Files;
import java.nio.file.Paths;
import javax.crypto.*;

public class Main {

    public static void main(String[] args) throws Exception {
        String fileName = "encryptedtext.txt";
        String fileName2 = "decryptedtext.txt";

        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);

        SecretKey secKey = keyGen.generateKey();

        Cipher aesCipher = Cipher.getInstance("AES");


        byte[] byteText = "Your Plain Text Here".getBytes();

        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
        byte[] byteCipherText = aesCipher.doFinal(byteText);
        Files.write(Paths.get(fileName), byteCipherText);


        byte[] cipherText = Files.readAllBytes(Paths.get(fileName));

        aesCipher.init(Cipher.DECRYPT_MODE, secKey);
        byte[] bytePlainText = aesCipher.doFinal(cipherText);
        Files.write(Paths.get(fileName2), bytePlainText);
    }
}

這篇關于使用 Java 進行 AES 加密和解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: а天堂中文最新一区二区三区 | 久久中文一区二区 | 亚洲在线免费 | 日韩av福利在线观看 | 中国一级特黄毛片大片 | 狠狠操狠狠操 | 亚洲一区二区三区在线播放 | 99久久99| 亚洲精品一区二区三区在线观看 | 欧美一区二区 | 欧美日韩高清在线观看 | 91国产在线播放 | 一本一道久久a久久精品蜜桃 | 狠狠狠色丁香婷婷综合久久五月 | 日本一区二区影视 | 男插女下体视频 | 成人小视频在线观看 | 日韩五月天 | 亚洲精品乱码久久久久久按摩 | 亚洲一区二区精品 | 天天综合操 | 国产精品免费一区二区三区四区 | av二区三区| 日本不卡一区 | 欧美综合在线视频 | 国产精品综合色区在线观看 | 中文字幕一区二区三区在线视频 | 伊人久久免费视频 | 欧美精品video | 色爱区综合 | 久久久久久久久久久高潮一区二区 | 国产一区二区三区亚洲 | 婷婷桃色网 | 91精品免费视频 | 久久久久久亚洲精品 | 欧美在线一区二区三区 | 91精品久久久久久久久中文字幕 | 国产成人精品一区二区三区四区 | 国产激情视频 | 国产精品久久久久久久久久免费看 | 国产精品久久久久久久久久免费看 |