AES Encryption and Decryption with Java(使用 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 加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Java 进行 AES 加密和解密


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01