java - Can't fix "Error: Input length must be a multiple of 16 when decrypting with a pad cipher" -
ello! working chat application encrypts it's data through aes/cbc/pkcs5 padding. works through client sending encrypted message server sent , decrypted. unfortunately, whenever decrypt message, error follows: javax.crypto.illegalblocksizeexception: input length must multiple of 16 when decrypting padded cipher. encryption based of program :(http://www.scottjjohnson.com/blog/aeswithcbcexample.java) works fine , cannot see difference between code , 1 except must convert string byte array. here code code:
client (encryption):
string message = textfield.gettext(); // generate key keygenerator keygen = keygenerator.getinstance("aes"); keygen.init(128); // use 256 bit keys, need "unlimited strength" encryption policy files sun. byte[] key = keygen.generatekey().getencoded(); secretkeyspec skeyspec = new secretkeyspec(key, "aes"); // build initialization vector. example zeros, // value or generated using random number generator. byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ivparameterspec ivspec = new ivparameterspec(iv); // initialize cipher encrypt mode cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.encrypt_mode, skeyspec, ivspec); // encrypt message byte[] encrypted = cipher.dofinal(message.getbytes()); system.out.println("ciphertext: " + encrypted + "\n"); system.out.println(encrypted); out.println(encrypted); textfield.settext("");
server side:
string input = in.readline(); writer.println("message " + input);
client (decryption):
//decryption system.out.println(line); line = line.substring(8); system.out.println(line); // generate key keygenerator keygen = keygenerator.getinstance("aes"); keygen.init(128); // use 256 bit keys, need "unlimited strength" encryption policy files sun. byte[] key = keygen.generatekey().getencoded(); secretkeyspec skeyspec = new secretkeyspec(key, "aes"); // build initialization vector. example zeros, // value or generated using random number generator. byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ivparameterspec ivspec = new ivparameterspec(iv); // reinitialize cipher decryption cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.decrypt_mode, skeyspec, ivspec); // decrypt message byte[] decrypted = cipher.dofinal(line.getbytes()); system.out.println("plaintext: " + new string(decrypted) + "\n"); messagearea.append(name + ": " + decrypted + "\n"); messagearea.setcaretposition(messagearea.getdocument().getlength());
your problem has nothing cryptography. failing transfer data correctly between clients , server.
i'm sure out.println(encrypted)
not want doing, although i'm not entirely clear since don't know type of out
. nor should calling line.getbytes()
in decryption code.
you should convert ciphertext non-lossy string form, such hexadecimal or base64. so, try:
out.println(datatypeconverter.printhexbinary(encrypted));
and
byte[] decrypted = cipher.dofinal(datatypeconverter.parsehexbinary(line));
Comments
Post a Comment