Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Decrypting encrypted data with another language

843810Feb 16 2004 — edited Feb 17 2004
I have encrypted data with a 'simple' program.
I can encrypt and decrypt the data just fine with java. However If I take the cipher text, and write it out to a file (stdout redirection) I can not decrypt it using a perl TripleDES encryption. Also I am trying to decrypt the data (external app) using the "secret key" generated by the key factory and that does not work either.

Thanks

Java code :
import javax.crypto.*;
import javax.crypto.interfaces.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;

class keyprint {

   public static void main (String args[]) {
      String key = "123456789012345678901234";
      String testText = "12345678";
      byte[] input = testText.getBytes();


      try {

         DESedeKeySpec keySpec = new DESedeKeySpec(key.getBytes());
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
         SecretKey deskey = keyFactory.generateSecret(keySpec);

//Use this key for external program encrypt /decrypt??
         System.out.println("SecretKey : ");
         System.out.println(new String(deskey.getEncoded()));

//encrypt
         Cipher cipherE = Cipher.getInstance("DESede");
         cipherE.init(Cipher.ENCRYPT_MODE, deskey);
         byte[] encText = cipherE.doFinal(input);

         System.out.println("Ecrypted Text: ");
         System.out.println(new String(encText));
//         System.out.print(new String(encText));

//decrypt
         Cipher cipherD = Cipher.getInstance("DESede");
         cipherD.init(Cipher.DECRYPT_MODE, deskey);
         byte[] decText = cipherD.doFinal(encText);

         System.out.println("Decrypted Text: ");
         System.out.println(new String(decText));

      } catch (InvalidKeyException e) {
         System.out.println(e);
      } catch (InvalidKeySpecException e) {
         System.out.println(e);
      } catch (NoSuchAlgorithmException e) {
         System.out.println(e);
      } catch (NoSuchPaddingException e) {
         System.out.println(e);
      } catch (IllegalBlockSizeException e) {
         System.out.println(e);
      } catch (BadPaddingException e) {
         System.out.println(e);
      }
   }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 16 2004
Added on Feb 16 2004
4 comments
546 views