gpt4 book ai didi

java - 从文件中读取加密数据

转载 作者:行者123 更新时间:2023-11-29 05:46:33 24 4
gpt4 key购买 nike

我正在阅读有关使用私钥加密的 IBM 教程。我写了如下代码

import java.security.*;
import javax.crypto.*;

// encrypt and decrypt using the DES private key algorithm

public class PrivateExample {

public static void main (String[] args) throws Exception {
String text=new String();
text="THIS IS AN ENCRYPTION TEST";
byte[] plainText = text.getBytes("UTF8");

// get a DES private key
System.out.println( "\nStart generating DES key" );
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
Key key = keyGen.generateKey();
System.out.println( "Finish generating DES key" );

// get a DES cipher object and print the provider
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
//
// encrypt using the key and the plaintext
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8") );

//
// decrypt the ciphertext using the same key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "Finish decryption: " );

System.out.println( new String(newPlainText, "UTF8") );
}
}

上面的代码效果很好。我能够看到结果和一切。但我想按如下方式修改它,以便我可以将密文存储在一个文件中。然后另一个程序从文件中读取加密文本并解密。以下是我到目前为止所做的,但我不明白如何进行。关于如何继续的一点提示会有所帮助。

import java.security.*;
import javax.crypto.*;

// encrypt and decrypt using the DES private key algorithm
public class PrivateExample {

public static void main (String[] args) throws Exception {
String text=new String();
text="This is an encryption test";

byte[] plainText = text.getBytes("UTF8");

// get a DES private key
System.out.println( "\nStart generating DES key" );
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
Key key = keyGen.generateKey();
System.out.println( "Finish generating DES key" );
//
// get a DES cipher object and print the provider
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
//
// encrypt using the key and the plaintext
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8") );

//Now writing to an ouput file the cipherText
try{
FileOutputStream fs=new FileOutputStream("c:/test.txt");
fs.write(cipherText);
}catch(Exception e){
e.printStackTrace();
}
//How to proceed from here

}
}

现在这个程序的工作已经完成了。它已成功将加密的字符串写入文件。新程序只需解密数据。如何从文件中读取加密字节?新程序显然不知道原始字符串是什么,但我将使用与算法中相同的 key 。请帮忙!我是加密新手

最佳答案

以下是将 key 写入文件的方式:

        //Write your key to an output file.
byte[] keyAsByte = key.getEncoded();
FileOutputStream keyfos = new FileOutputStream("key.txt");
keyfos.write(keyAsByte);
keyfos.close();

我不建议将 key 和加密文本放在同一个文件中。

以下是读取加密文本和 key 并解密的方式:

    //Read your key
FileInputStream keyFis = new FileInputStream("key.txt");
byte[] encKey = new byte[keyFis.available()];
keyFis.read(encKey);
keyFis.close();
Key keyFromFile = new SecretKeySpec(encKey, "DES");
//Read your text
FileInputStream encryptedTextFis = new FileInputStream("test.txt");
byte[] encText = new byte[encryptedTextFis.available()];
encryptedTextFis.read(encText);
encryptedTextFis.close();
//Decrypt
Cipher decrypter = Cipher.getInstance("DES/ECB/PKCS5Padding");
decrypter.init(Cipher.DECRYPT_MODE, keyFromFile);
byte[] decryptedText = decrypter.doFinal(encText);
//Print result
System.out.println("Decrypted Text: " + new String(decryptedText));

注意:我写信息的路径和你不一样。

关于java - 从文件中读取加密数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15682840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com