java - Java中的DES算法
问题描述
我正在尝试创建一个程序,您可以在其中使用 Java 中的 DES 算法加密/解密文本和文件。文件解密部分不起作用,我收到此异常:“javax.crypto.IllegalBlockSizeException:使用填充密码解密时输入长度必须是 8 的倍数”。有谁能够帮我?
import javax.crypto.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
Scanner input = new Scanner(System.in);
KeyGenerator desKeyGenerator = KeyGenerator.getInstance("DES");
Cipher desCipherEncrypter = Cipher.getInstance("DES");
Cipher desCipherDecrypter = Cipher.getInstance("DES");
SecretKey key = desKeyGenerator.generateKey();
desCipherEncrypter.init(Cipher.ENCRYPT_MODE, key);
desCipherDecrypter.init(Cipher.DECRYPT_MODE, key);
System.out.print("Plaintext: ");
String message = input.nextLine();
String encrypted = encrypt(desCipherEncrypter, message);
System.out.println("Encrypted text: " + encrypted);
String decrypted = decrypt(desCipherDecrypter, encrypted);
System.out.println("Decrypted text: " + decrypted);
System.out.print("The file you want to encrypt: ");
String path = input.nextLine();
System.out.print("The file where you want to save the encrypted one: ");
String fileenc = input.nextLine();
fileencrypt(desCipherEncrypter,path,fileenc);
System.out.print("The file where you want to save the decrypted one: ");
String filedec = input.nextLine();
filedecrypt(desCipherDecrypter, fileenc,filedec );
}
catch (NoSuchAlgorithmException noSuchAlgorithmException) {
noSuchAlgorithmException.printStackTrace();
} catch (InvalidKeyException invalidKeyException) {
invalidKeyException.printStackTrace();
} catch (NoSuchPaddingException noSuchPaddingException) {
noSuchPaddingException.printStackTrace();
} catch (BadPaddingException badPaddingException) {
badPaddingException.printStackTrace();
} catch (IllegalBlockSizeException illegalBlockSizeException) {
illegalBlockSizeException.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void fileencrypt(Cipher cipher,String path,String encryptedfile) throws FileNotFoundException,Exception{
Scanner inputi=new Scanner(System.in);
Scanner finput = null;
File file = new File(path);
if (!file.exists()) throw new FileNotFoundException();
if (!file.canRead()) throw new Exception("Cannot read " + path);
if (!file.canWrite()) throw new Exception("Cannot write " + path);
finput = new Scanner(file);
FileWriter write =new FileWriter(encryptedfile);
while (finput.hasNext()) {
String str = finput.nextLine();
String encrypt = encrypt(cipher, str);
write.write(encrypt);
write.write("\n");
}
System.out.println("Encrypted text");
write.close();
}
public static void filedecrypt(Cipher cipher,String filetodecrypt,String dencryptedfile) throws FileNotFoundException,Exception{
Scanner dinput=null;
FileWriter write1 =new FileWriter(dencryptedfile);
File filefordecrypt=new File(filetodecrypt);
dinput=new Scanner(filetodecrypt);
while (dinput.hasNext()) {
String str = dinput.nextLine();
String decrypt = decrypt(cipher, str);
write1.write(decrypt + " ");
}
write1.close();
System.out.println("Decrypted text");
}
public static String encrypt(Cipher cipher, String plainStr) throws BadPaddingException,IllegalBlockSizeException
{
byte[] plainBytes;
byte[] encryptedBytes;
plainBytes=plainStr.getBytes(StandardCharsets.ISO_8859_1);
encryptedBytes=cipher.doFinal(plainBytes);
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(Cipher cipher,String encryptedStr) throws BadPaddingException,IllegalBlockSizeException
{
byte[] encryptedBytes=Base64.getDecoder().decode(encryptedStr);
byte[] decryptedBytes=cipher.doFinal(encryptedBytes);
return new String(decryptedBytes,StandardCharsets.ISO_8859_1);
}
} ```
解决方案
推荐阅读
- python - Python运算符重载不起作用
- android - 调试被播放保护阻止的应用程序
- javascript - 如何使用node.js噩梦获取没有ID的元素
- typescript - 如何让 Angular CLI 停止在我的项目中包含给定的类型文件?
- java - 如何在spring xml配置中覆盖父方法?
- tumblr-themes - Tumblr 博客上的照片集损坏了?
- sql - TSQL - 查找具有值的前一行并更新初始行
- javascript - 反应路由器:pushstate后的空白页面
- php - 在数据透视表中使用 eloquent 保存数据
- python - 从两个表中获取帖子?