I was going through an IBM tutorial on encrypting using Private key. And I wrote the code as below
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") );
}
}
The above code works great. I am able to see the result and everything. But I want to modify it as follows so that i can store the cipherText in a file. Then another program reads the encrypted text from the file and decrypts it. Below is what I have done till now, but I cannot understand how to proceed. Just a little hint on how to proceed will help.
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
}
}
Now this program's job is complete. It has successfully written the encrypted string into a file. The new program only has to decrypt the data. How do I read the encrypted bytes from the file? The new program obviously has no idea regarding what the original string was, but I will be using the same key as in the algorith. Please help! I am new to encryption
Here's how you write your key to a file:
//Write your key to an output file.
byte[] keyAsByte = key.getEncoded();
FileOutputStream keyfos = new FileOutputStream("key.txt");
keyfos.write(keyAsByte);
keyfos.close();
I wouldn't recommend putting the key with the encrypted text in the same file.
Here's how you read the encrypted text and the key back and decrypt:
//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));
: I didn't use the same path as you for writing the information. :我没有使用与您相同的路径来编写信息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.