【发布时间】:2011-04-03 18:23:23
【问题描述】:
我编写了以下代码来使用 java 加密库加密和解密文件。
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
class Blowfish {
public static void main(String[] args) throws Exception {
String s;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Cipher encrypt = Cipher.getInstance("DES");
Cipher decrypt = Cipher.getInstance("DES");
System.out.print("Enter the key: ");
s = br.readLine();
/*
* Names of algorithms used "Blowfish" "DES" 64 bit key ie. 8 bytes
* "AES" key size has to be 16 bytes ie. 128 bits
*/
byte key[] = new byte[8];
for (int i = 0; i < s.length() && i < 8; i++)
key[i] = (byte) s.charAt(i);
encrypt.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DES"));
FileInputStream fin = new FileInputStream("test.txt");
FileOutputStream out = new FileOutputStream("encrypted.p4e");
CipherOutputStream cout = new CipherOutputStream(out, encrypt);
int input = 0;
while ((input = fin.read()) != -1) {
cout.write(input);
}
out.close();
cout.close();
System.out.println("Starting the decryption");
System.out.print("Enter the key: ");
s = br.readLine();
byte key2[] = new byte[8];
for (int i = 0; i < s.length() && i < 8; i++)
key2[i] = (byte) s.charAt(i);
decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key2, "DES"));
fin = new FileInputStream("encrypted.p4e");
out = new FileOutputStream("test2.txt");
CipherInputStream in = new CipherInputStream(fin, decrypt);
input = 0;
while ((input = in.read()) != -1) {
out.write(input);
}
out.close();
in.close();
}
}
但是,当我尝试在一个示例 .txt 文件上对其进行测试时,加密和解密运行时没有错误。但是解密后的文件与原始文件并不完全相同...结尾部分的某些部分被截断。
测试文件
用于加密的测试文件..检查 正确性
加密使用 -> pralhad
使用密钥解密后 -> pralhad
用于加密的测试文件..检查
请提出一些解决方案。
【问题讨论】:
标签: java encryption