Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
JCE Problems in Java 1.5 on OS X
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

JCE Problems in Java 1.5 on OS X



Hello All,

I am trying to use the JCE on Java 1.5 on OS X. When I encrypt some text and decrypt it all at once, it works fine. However, I will encrypt some text, save it off in a hexadecimal string format. Then I will try and decrypt that same text (after converting it back from hexadecimal) and I will receive the exception.

javax.crypto.BadPaddingException: Given final block not properly padded
        at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
        at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
        at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
        at javax.crypto.Cipher.doFinal(DashoA12275)
        at test.main(test.java:62)

If I attempt this same test forcing it to use 1.4.2 on OS X for both the encryption and decryption, I do not have this problem. I assume there is a problem in the JRE that is causing this, but I would like some community input. Below is a sample of my code.

thanks,

Courtney

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class test {

 /**
  * @param args
  * @throws UnsupportedEncodingException
  * @throws NoSuchAlgorithmException
  * @throws NoSuchPaddingException
  * @throws InvalidKeyException
  * @throws BadPaddingException
  * @throws IllegalBlockSizeException
  */
 public static void main(String[] args) {

  String seed = "TestKey";
  String unencodedText = "This is a test example";

  try {
   KeyGenerator keygen = KeyGenerator.getInstance("DES");
   SecureRandom sr = new SecureRandom(seed.getBytes());
   keygen.init(56, sr);
   SecretKey desKey = keygen.generateKey();

   // Create the cipher
   Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

   // We use the generated desKey from above to initialize the Cipher
   // object for encryption:

   // Initialize the cipher for encryption
   desCipher.init(Cipher.ENCRYPT_MODE, desKey);

   // Encrypt the cleartext
   byte[] ciphertext = desCipher.doFinal(unencodedText.getBytes());
   String encodedText = hexEncode(ciphertext);
   System.out.println(encodedText);

   // Initialize the same cipher for decryption
   desCipher.init(Cipher.DECRYPT_MODE, desKey);

   // Decrypt the ciphertext just encoded in this test
   System.out.println("Encoded text decoded: "
     + new String(desCipher.doFinal(ciphertext)));

   // Decrypt the ciphertext received as an input
   if (args.length > 0) {

    byte[] inputCipherText = hexDecode(args[0]);
    System.out.println("Inputed text decoded: "
      + new String(desCipher.doFinal(inputCipherText)));
   }
  } catch (GeneralSecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

private static final char[] hexTable = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };


 public static String hexEncode(byte[] bytes) {
  StringBuffer sb = new StringBuffer(bytes.length + 1);
  for (int ix = 0; ix < bytes.length; ix++) {
   sb.append(hexTable[(bytes[ix] & 15)]);
   sb.append(hexTable[((bytes[ix] >>> 4) & 15)]);
  }
  return sb.toString();
 }

 public static byte[] hexDecode(String string) {

  // remove '-' and convert to a char array
  char[] chars = string.toCharArray();

  byte[] ret = new byte[chars.length / 2]; // encode creates 2 chars
  // from each byte
  for (int ix = 0; ix < ret.length; ix++) {
   byte low = findIndex(hexTable, chars[2 * ix]); // low order part of
   // byte
   if (low == hexTable.length)
    throw new IllegalArgumentException("Encoded string '" + string
      + "' " + "contains invalid character at index "
      + (2 * ix));
   byte high = findIndex(hexTable, chars[2 * ix + 1]); // high order
   // part of byte
   if (high == hexTable.length)
    throw new IllegalArgumentException("Encoded string '" + string
      + "' " + "contains invalid character at index "
      + (2 * ix + 1));
   ret[ix] = (byte) (((high << 4) | low) & 0xff);
  }
  return ret;
 }

 private static byte findIndex(char[] search, char toFind) {
  byte ret = 0;
  for (ret = 0; ret < search.length; ret++) {
   if (search[ret] == toFind)
    break;
  }
  return ret;
 }
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.