AES Encryption by java code and decryption by using openssl (terminal)

1,823 views
Skip to first unread message

MALKIT SINGH

unread,
Aug 6, 2014, 3:54:35 AM8/6/14
to null-...@googlegroups.com
HI All ,
I want to do AES encryption by using java code and decryption by using openssl command line.


Following is the java code which i m using to encrypt and decrypt the file  :-




{
       
        //generating the secret key (symmetric key) using AES algorithm
       
        KeyGenerator keyGenS = KeyGenerator.getInstance("AES");
            keyGenS.init(128);
            SecretKey sKey1 = keyGenS.generateKey();

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

            System.out.println("the key :- "+ aesCipher);
          
           
            byte[] ciphertext = null;
           
            //Creating a file A1.txt to store encrypted data from test.txt
           
            OutputStream outputStream = new FileOutputStream(new File(
                    "A1.txt"));
           
            //reading test.txt to encrypt the data and writing it to A1.txt
           
            FileInputStream fileInputStream1 = new FileInputStream(new File(
                    "test.txt"));
           
            //Encrypt 117 bytes at a time
           
            byte[] by1 = new byte[117];
           
            while (fileInputStream1.available()>0)
            {
                fileInputStream1.read(by1);
               
                 ciphertext = aesCipher.doFinal(by1);
                    outputStream.write(ciphertext);
                    System.out.println(fileInputStream1.available());
                    System.out.println("Encrpting !! ");
            }
           
            fileInputStream1.close();
           
            outputStream.close();
           
         
           
           
            //Reading the Encrypted file A1.txt & writing the original data to A2.txt after decrypting
           
        FileInputStream     fileInputStream = new FileInputStream(new File(
                     "A1.txt"));
           
        //decrypt 128 bytes at a time
       
        byte[] ciphertext1 = new byte[128];
         
          OutputStream outputStream1 = new FileOutputStream(new File(
                  "A2.txt"));

            aesCipher.init(Cipher.DECRYPT_MODE, sKey1);
            while(fileInputStream.available()>0){
                fileInputStream.read(ciphertext1);
           
           
            byte[] plainText = aesCipher.doFinal(ciphertext1);
           
            outputStream1.write(plainText);
            System.out
                    .println("Decrypting......");
           
    }
            outputStream1.close();
            fileInputStream.close();
           
    }

}

it works fine !!!!!!

when i use the above encrypt code for encryption i am able to do that , but when i try to decrypt the file encrypted file using openssl from command line then i m not be able to do that .

Following is the code for encrypted that is used :-
 {
       
       
         KeyGenerator keyGenS = KeyGenerator.getInstance("AES");
            keyGenS.init(128);
            SecretKey sKey1 = keyGenS.generateKey();
           
System.out.println("the secret key :"+ sKey1);

            FileOutputStream fileOutputStream=new FileOutputStream(new File("key1.txt"));
            fileOutputStream.write(sKey1.getEncoded());
            fileOutputStream.close();
           
       
       
        Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            aesCipher.init(Cipher.ENCRYPT_MODE,sKey1);

            System.out.println("the key :- "+ aesCipher);
           byte[] ciphertext = null;
            OutputStream outputStream = new FileOutputStream(new File(
                    "A1.txt"));
           
           
        System.out.println(aesCipher.doFinal());
            FileOutputStream fileOutputStreamnew=new FileOutputStream(new File("newkey1.txt"));
            fileOutputStreamnew.write(aesCipher.doFinal());
            fileOutputStreamnew.close();

           
            //Encrypting zip
           
            FileInputStream fileInputStream1 = new FileInputStream(new File(
                    "abcd.txt"));
            byte[] by1 = new byte[117];
           
            while (fileInputStream1.available()>0)
            {
                fileInputStream1.read(by1);
                // byte[] byteCipherText = aesCipher.doFinal(by1);
                 ciphertext = aesCipher.doFinal(by1);
                    outputStream.write(ciphertext);
                    System.out.println("text"+ ciphertext);
                   
                    System.out.println(fileInputStream1.available());
                    System.out.println("Encrpting !! ");
            }
           
            fileInputStream1.close();
           
            outputStream.close();
    }

}



decryption command using openssl :-


openssl enc -aes-128-cbc  -a -d   -des -in A1.txt -out output.txt -k newkey1.txt






error:- error reading input file
sometimes get error like bad magic number


please help , your help will be greatly appreciated

Thanks,
Malkit

Rahul Sasi

unread,
Aug 6, 2014, 10:29:46 AM8/6/14
to null-...@googlegroups.com
Are you providing full path to openssl and the files tht are to be encrypted /decrypted 


--
_______________________________________________________________________________
null - Spreading the right Information
null Mailing list charter: http://null.co.in/section/about/null_list_charter/
---
You received this message because you are subscribed to the Google Groups "null" group.
To unsubscribe from this group and stop receiving emails from it, send an email to null-co-in+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

MALKIT SINGH

unread,
Aug 7, 2014, 1:32:34 AM8/7/14
to null-...@googlegroups.com
Now i am able to decrypt the file using openssl (terminal) ,  by using this command
openssl enc -d -aes-128-cbc -K 31323334353637383930313233343536 -iv 30303030303030303030303030303030 -in "encrypt.aes"



but i have a question here that is it a good practice to hardcode the value or randomly generate the value and assign  to iv in java code at time of encryption

  byte[] iv = "0000000000000000".getBytes();
     // Creation of Secret key
     byte[] keyBytes = "1234567890123456".getBytes(); //AES 128bit
     System.out.println(keyBytes.length);
     System.out.println("Generate key for AES at "+keyBytes.length +" bits");
     Key key = new SecretKeySpec(keyBytes, "AES");
     // Creation of Cipher objects
     Cipher encrypt = Cipher.getInstance(xform);
     System.out.println("Get Provider Info."+encrypt.getProvider().getInfo());
     encrypt.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(iv));

If i dont give iv in java code at the time of encryption than i m not b able to decrypt the file on command line using openssl , so i have to define it .
so is it a good practice or there is any other way to do  ??




@fb1h2s

yes i m providing full path to openssl and the files that are to be enc/dec.

eQuiNoX

unread,
Aug 7, 2014, 2:39:56 AM8/7/14
to null-...@googlegroups.com
On Thu, Aug 7, 2014 at 11:02 AM, MALKIT SINGH <malkit....@gmail.com> wrote:
> Now i am able to decrypt the file using openssl (terminal) , by using this
> command
> openssl enc -d -aes-128-cbc -K 31323334353637383930313233343536 -iv
> 30303030303030303030303030303030 -in "encrypt.aes"
>
>
>
> but i have a question here that is it a good practice to hardcode the value
> or randomly generate the value and assign to iv in java code at time of
> encryption
>
> byte[] iv = "0000000000000000".getBytes();

Use SecureRandom. IV is meant to be random, else your CBC is only as
good as ECB.

-- eq

eQuiNoX

unread,
Aug 7, 2014, 2:40:06 AM8/7/14
to null-...@googlegroups.com
Not as bad as ECB now that I really think about it, but still pretty bad.

-- eq

Deepak Dhyani

unread,
Aug 7, 2014, 9:33:46 AM8/7/14
to null-...@googlegroups.com
Yeah Secure random and then store that random so that you can use it while decryption..if its same then I guess same message will get enc to same value..like ECB.. but in ECB it ll also happen with in the message as well, not so in case of CBC...so patterns will appear in ECB cipher text but not in CBC for a single message.





-- eq

--
_______________________________________________________________________________
null - Spreading the right Information
null Mailing list charter: http://null.co.in/section/about/null_list_charter/
---
You received this message because you are subscribed to the Google Groups "null" group.
To unsubscribe from this group and stop receiving emails from it, send an email to null-co-in+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far.”
 
                                        - Swami Vivekananda

eQuiNoX

unread,
Aug 7, 2014, 10:30:42 AM8/7/14
to null-...@googlegroups.com
On Thu, Aug 7, 2014 at 4:55 PM, Deepak Dhyani <deepakd...@gmail.com> wrote:
> Yeah Secure random and then store that random so that you can use it while
> decryption..if its same then I guess same message will get enc to same
> value..like ECB.. but in ECB it ll also happen with in the message as well,
> not so in case of CBC...so patterns will appear in ECB cipher text but not
> in CBC for a single message.

Also -- in ECB identical plaintext blocks => identical ciphertext
blocks. This wouldn't happen in CBC even with a constant IV.

In anycase, don't use CBC with constant IV.

-- eq
Reply all
Reply to author
Forward
0 new messages