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.
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