Sunday 15 January 2012

java - Encrypt and decrypt with hex string -



java - Encrypt and decrypt with hex string -

i searched lot haven't found solution how solve this. have app has decrypt long hex string aes 256.

in order test it, created test method encrypts long text hex , convert , decrypt it.

if run method, next error: given final block not padded. receive error in decryption method.

the test method looks so:

@test public void testencanddecrequestwithhexstring() throws unsupportedencodingexception { cryptohelper cryptohelper = new cryptohelper("aes256"); string paramstr = "abcb28bcee5947b8aece3386871ec0df&{d5ca99d2-506b-4864-8971-e87821d6b105}&7523429"; //encrypt param string byte[] parambyteenc = cryptohelper.encryptbytestobytes(paramstr.getbytes("ascii"), param_key, param_iv); //convert hex string encryptedhexstr = cryptohelper.bytearraytohexstr(parambyteenc); //convert byte array byte[] encryptedhexbytes = cryptohelper.hexstrtobytearray(encryptedhexstr); // decrypt byte[] parambytedecrypted = cryptohelper.decryptbytestobytes(encryptedhexbytes, encryptedhexbytes.length, param_key, param_iv); string decryptedstr = new string(parambytedecrypted); assertequals("abcb28bcee5947b8aece3386871ec0df&{d5ca99d2-506b-4864-8971-e87821d6b105}&7523429", decryptedstr); }

the crypthelper class has next methods:

@override public byte[] encryptbytestobytes(byte[] plaindata, byte[] key, byte[] iv) { seek { initcipher(cipher.encrypt_mode, key, iv); homecoming aescipher.dofinal(plaindata); } grab (illegalblocksizeexception | badpaddingexception e) { log.severe(e.getmessage()); } homecoming null; } @override public byte[] decryptbytestobytes(byte[] encryptedbytes, int length, byte[] key, byte[] iv) { seek { initcipher(cipher.decrypt_mode, key, iv); homecoming aescipher.dofinal(encryptedbytes, 0, length); } grab (illegalblocksizeexception | badpaddingexception e) { e.printstacktrace(); } homecoming null; } private void initcipher(int mode, byte[] keybytes, byte[] ivbytes) { seek { // create shared secret , init cipher mode secretkeyspec secretkeyspec = new secretkeyspec(keybytes, "aes"); aescipher = cipher.getinstance("aes/cbc/pkcs5padding"); aescipher.init(mode == cipher.encrypt_mode ? cipher.encrypt_mode : cipher.decrypt_mode, secretkeyspec, new ivparameterspec(ivbytes)); } grab (invalidalgorithmparameterexception | nosuchalgorithmexception | nosuchpaddingexception | invalidkeyexception e) { e.printstacktrace(); } } public string bytearraytohexstr(byte[] encrypted) { stringbuilder hex = new stringbuilder(); (byte b : encrypted) { hex.append(string.format("%02x", b)); } homecoming new string(hex.tostring()); } public byte[] hexstrtobytearray(string hex) { stringbuilder sb = new stringbuilder(); (int = 0; < hex.length() - 1; += 2) { string output = hex.substring(i, (i + 2)); int decimal = integer.parseint(output, 16); sb.append((char) decimal); } string temp = sb.tostring(); homecoming temp.getbytes(); }

i used same key , initialization vector decryption process problem not wrong key or initialization vector. sure every function here doing job correctly. if don't utilize functions hexstrtobytearray() , bytearraytohexstr() , utilize encrypted byte decrypting, works no problem. think there encoding/decoding problem have no thought how handle in java. if utilize getbytes("utf-8") , new string(byte[], "utf-8") illegalblocksizeexception.

i hope can help me finding out if on right way , did wrong.

this clear indication shouldn't write library functions if have been defined. utilize hex codec bouncy castle, guava or apache codec instead (until oracle sees lite , provides 1 in java.util package).

if implement yourself, please don't error characters bytes:

public byte[] hexstrtobytearray(string hex) { bytearrayoutputstream baos = new bytearrayoutputstream(hex.length() / 2); (int = 0; < hex.length(); += 2) { string output = hex.substring(i, + 2); int decimal = integer.parseint(output, 16); baos.write(decimal); } homecoming baos.tobytearray(); }

java encryption hex aes

No comments:

Post a Comment