Skip to Main Content

Java Programming

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!

Port PHP RIJNDAEL_128 Encode/Decode code to JAVA 1.4

807588May 18 2009 — edited May 18 2009
Hi all, I have a PHP modume that encrypts / decrypts strings using RIJNDAEL_128 (or 192 or 256 but I choose 128 because I read somewhere that jdk 1.4 does not support above 128). The code is bellow
function encryptData($value){
   $key = "@sp1s0n3ByD1@k0g1@nn1$";
   $text = $value;
   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);
   return base64_encode($crypttext);
}

function decryptData($value){
   $key = "@sp1s0n3ByD1@k0g1@nn1$";
   $crypttext = base64_decode($value);
   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
   return trim($decrypttext);
}
When I encrypt the String "alexis" it gives me as a result "tMCjyMgfUM0JYzJ0fFqy6A==" and the decryption works fine.

The problem is that I need to perform similar encryption/decryption in java, because php will feed encrypted data on a java Servlet.

After many hours of searching I have coded
/*
 * Created on 18 Μαϊ 2009
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.aspis.AspisAML.general;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author A_T_DIAKOG
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AspisEncoder {

    /**
     * Turns array of bytes into string
     *
     * @param buf    Array of bytes to convert to hex string
     * @return    Generated hex string
     */
     public static String asHex (byte buf[]) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)
        strbuf.append("0");

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
      }

      return strbuf.toString();
     }


     public static void main(String[] args) throws Exception {

           //String message="This is just an example";

           // Get the KeyGenerator

           KeyGenerator kgen = KeyGenerator.getInstance("AES");
           String keyString ="@sp1s0n3ByD1@k0g1@nn1$";
           byte[] keyB = new byte[16]; 


           for (int i = 0; i < keyString.length() && i < keyB.length; i++) {
               keyB[i] = (byte) keyString.charAt(i);
               }

//               Make the Key
               SecretKey skey = new SecretKeySpec(keyB, "AES");

           // Generate the secret key specs.
           //SecretKey skey = kgen.generateKey();
           byte[] raw = skey.getEncoded();


           SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");


           // Instantiate the cipher

           Cipher cipher = Cipher.getInstance("AES");

           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

           byte[] encrypted = cipher.doFinal(Base64Coder.encodeString("alexis").getBytes());
           System.out.println("encrypted string: " + encrypted);

           cipher.init(Cipher.DECRYPT_MODE, skeySpec);

           byte[] original = cipher.doFinal(Base64Coder.decode("tMCjyMgfUM0JYzJ0fFqy6A=="));
           String originalString = new String(original);
           System.out.println("Original string: " + originalString + " " + asHex(original));
         }

}
When I try to run this I get as an encrypted string "[B@256ea620" and when I try to do the decryption I am getting a Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded

Can someone please help me on this because I am lost!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 15 2009
Added on May 18 2009
15 comments
664 views