How to generate a secret key in Java once and use this key in two different programs

My goal is to write a Java program to encrypt a text file ( cipher text ) using the AES algorithm . Then write another program to decrypt this encrypted file ( cipher text ) to return plain text. I want to use the same key (the same key, generate it once, save it somewhere and use it both in the encryption program and for decryption) for encryption and decryption. If I generate a key and perform encryption and decryption line by line in the same program, then it works fine. Here is a working code snippet for this:

  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 for encryption and decryption, I need to have two different programs (java files). So, I have to somehow generate the key and save it somewhere. Then use the same key for the encryption and decryption program. How can i do this?

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 value of the encoded key using the above program. But my question is how to generate SecretKey using this value in my decryption program?

+4
java cryptography aes secret-key
Nov 27 '13 at 4:14
source share
2 answers

Forgive me if I misunderstood your question, but I believe that you want to restore the SecretKey object from an existing key encoded in an array of bytes.

This can be done simply with the javax.crypto.spec.SecretKeySpec constructor as such:

 byte[] encoded = //Key data SecretKey secretKey = new SecretKeySpec(encoded, "AES"); 

Since SecretKeySpec is a subclass of SecretKey , casting is not required. If your encryption / defragmentation algorithm changes, be sure to change the string literal used in the AES constructor to any algorithm that you decide to use in the future.

+11
Dec 15 '13 at 5:34 on
source share

Here is one way to print the values ​​in a byte[] array in hexadecimal format:

 byte[] a = {-120, 17, 42,121}; for (byte b : a) { System.out.printf("%2X",b); } System.out.println(); 
+1
Nov 27 '13 at 4:39
source share



All Articles