我希望从一个类加密文本文件,并且应该能够从另一个类解密它。
这里我已成功加密文本文件并存储到名为" encryptedFile"的字符串中。在班级" endecryption.java"。但我想解密相同的#34; encryptedFile"到另一个名为" decryptedFile"的文本文件中来自另一个类decryption.java,但它不再是解密,而是再次加密加密文件。
这是我的endecryption.java类
class Endecryption {
private static Cipher encrypt;
private static Cipher decrypt;
private static final byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 };
public static void main(String[] args) {
// TODO code application logic here
String normalFile = "D:/ashok/ashk.txt";
String encryptedFile="D:/ashok/aso.txt";
try {
SecretKey secret_key = KeyGenerator.getInstance("DES")
.generateKey();
AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(
initialization_vector);
// set encryption mode ...
encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
encrypt.init(Cipher.ENCRYPT_MODE, secret_key, alogrithm_specs);
// set decryption mode
// encrypt file
encrypt(new FileInputStream(normalFile), new FileOutputStream(
encryptedFile));
System.out.println("End of Encryption/Decryption procedure!");
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IOException e) {
e.printStackTrace();
}
}
private static void encrypt(InputStream input, OutputStream output)
throws IOException {
output = new CipherOutputStream(output, encrypt);
writeBytes(input, output);
}
private static void writeBytes(InputStream input, OutputStream output)
throws IOException {
byte[] writeBuffer = new byte[512];
int readBytes = 0;
while ((readBytes = input.read(writeBuffer)) >= 0) {
output.write(writeBuffer, 0, readBytes);
}
output.close();
input.close();
}
这是我的decrypt.java类
public class decryption {
private static Cipher encrypt;
private static Cipher decrypt;
private static final byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 };
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String encryptedFile = "D:/ashok/aso.txt";
String decryptedFile = "D:/ashok/amii.txt";
try {
SecretKey secret_key = KeyGenerator.getInstance("DES")
.generateKey();
AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(
initialization_vector);
// set encryption mode ...
// set decryption mode
decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
decrypt.init(Cipher.DECRYPT_MODE, secret_key, alogrithm_specs);
// encrypt file
// decrypt file
decrypt(new FileInputStream(encryptedFile), new FileOutputStream(
decryptedFile));
System.out.println("End of Encryption/Decryption procedure!");
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IOException e) {
e.printStackTrace();
}
}
private static void decrypt(InputStream input, OutputStream output)
throws IOException {
input = new CipherInputStream(input, decrypt);
writeBytes(input, output);
}
private static void writeBytes(InputStream input, OutputStream output)
throws IOException {
byte[] writeBuffer = new byte[512];
int readBytes = 0;
while ((readBytes = input.read(writeBuffer)) >= 0) {
output.write(writeBuffer, 0, readBytes);
}
output.close();
input.close();
}
}