AES加密和解密Java [英] AES Encryption and Decryption with Java

查看:105
本文介绍了AES加密和解密Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在做的,可以看起来有点笨拙,但任何帮助是赞赏的问题。阅读几乎所有相关主题,但没有找到适当的解决方案。我是新加密解密编程,需要在我的Java应用程序之一中实现。



谢谢..
这是代码看起来如何.. public static void main(String [] args)throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException {$ b $($)

$ b // TODO自动生成的方法stub
String FileName =encryptedtext.txt;
String FileName2 =decryptptedtext.txt;
字符串pad =0;

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

SecretKey SecKey = KeyGen.generateKey();

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

byte [] byteText =我的名字是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自动生成的catch块
e.printStackTrace();
}

try {
FileReader fr = new FileReader(FileName);
BufferedReader br = new BufferedReader(fr);
cipherText = br.readLine();
br.close();
} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
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自动生成的catch块
e.printStackTrace();
}






}


解决方案

这里,你必须明白的是,密文可能包含不可打印的字符。所以,当你使用readLine()时,它可能不会给你文件中的所有字节。



另外, byteCipherText.toString( )不给你你以为你会得到的。在java中, toString()方法不会给出数组的内容的字符串表示。



不需要添加填充到加密文本。它已经被填充了。

  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 =decryptptedtext.txt;

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

SecretKey SecKey = KeyGen.generateKey();

密码AesCipher = Cipher.getInstance(AES);


byte [] byteText =你的纯文本.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);
}
}


Here is what I am doing which can look a bit clumsy but any help is appreciated regarding the problem. 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();
            }






}

解决方案

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.

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

这篇关于AES加密和解密Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆