Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate secret key in Java once and use that key in 2 different programs

My aim is to write a Java program to encrypt a text file (cipher text) using AES algorithm. And then, write another program to decrypt that encrypted file (cipher text) to get the plain text back. I want to use same key (same key, generate once, save it somewhere, and use it in both encryption and decryption program) for encryption and decryption process. If I generate key and do the encryption and decryption line by line in the same program then it works perfectly. Here is the working code snippet for that:

        String strDataToEncrypt = new String();
        String strCipherText = new String();
        String strDecryptedText = new String();

        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey secretKey = keyGen.generateKey();

        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);

        strDataToEncrypt = "any text input";
        byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
        byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
        strCipherText = new BASE64Encoder().encode(byteCipherText);
        System.out.println("cipher text: " +strCipherText);
        aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
        byte[] byteDecryptedText = aesCipher.doFinal(new BASE64Decoder().decodeBuffer(strCipherText));
        strDecryptedText = new String(byteDecryptedText);
        System.out.println("plain text again: " +strDecryptedText);

But, I need to have two different programs (java files) for encryption and decryption. So, I have to somehow generate a key and save that somewhere. Then use the same key for both encryption and decryption program. How can I do that?

EDIT_1

KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
byte[] encoded = secretKey.getEncoded(); 
System.out.println("key: "+encoded);// key: [B@52b2a2d8

I can get the encoded key value using the above program. But my question is how to generate the SecretKey using this value in my decryption program?

like image 406
K M Rakibul Islam Avatar asked Nov 27 '13 04:11

K M Rakibul Islam


People also ask

How do you generate a random 256 bit session key in Java?

KeyGenerator keyGen = KeyGenerator. getInstance("AES"); keyGen. init(256); // for example SecretKey secretKey = keyGen. generateKey();


2 Answers

Forgive me if I misunderstood your question but I believe you wish to reconstruct a SecretKey object from a existing key encoded in a byte array.

This can be performed simply by using the javax.crypto.spec.SecretKeySpec's constructor as such:

byte[] encoded = //Key data

SecretKey secretKey = new SecretKeySpec(encoded, "AES");

Since SecretKeySpec is a subclass of SecretKey no casting is needed. Should your encryption/decrption algorithm change please make sure to change the string literal used in the constructor AES to whatever algorithm you decided to use in the future.

like image 147
initramfs Avatar answered Sep 29 '22 08:09

initramfs


Here's one way to print out the values in a byte[] array in hex:

byte[] a = {-120, 17, 42,121};
for (byte b : a)
{
    System.out.printf("%2X",b);
}
System.out.println();
like image 38
Jim Garrison Avatar answered Sep 29 '22 09:09

Jim Garrison