AES encryption error in java -
i try decrypt encrypted text , use code error me:
exception in thread "main" java.security.invalidkeyexception: illegal key size
and decryption code is:
string key = "ffce885876a617e7"; string vector = "9ee153a3df56965e7baf13a7fa1075cc"; ivparameterspec ivspec = new ivparameterspec(key.getbytes()); secretkeyspec keyspec = new secretkeyspec(vector.getbytes(), "aes"); cipher cipher = cipher.getinstance("aes/cbc/nopadding"); cipher.init(cipher.decrypt_mode, keyspec, ivspec); //error occured in line
.getbytes()
not automagically convert "hex string" matching bytes.
instead, try utility method:
private static byte[] hexstringtobytes(final string input) { final int len = input.length(); if (len % 2 != 0) throw new illegalargumentexception(); final byte[] ret = new byte[len / 2]; int offset = 0; (int = 0; < ret.length; i++) { ret[i] = (byte) integer.parseint(input.substring(offset, offset+2), 16); offset += 2; } return ret; }
then in code use hexstringtobytes(key)
etc.
Comments
Post a Comment