Friday, 15 June 2012

java - IllegalBlockSizeException need to resolve -



java - IllegalBlockSizeException need to resolve -

i have written programme in java next tutorial http://www.java2s.com/code/android/security/aesencryption.htm getting exception i.e. "javax.crypto.illegalblocksizeexception: input length must multiple of 16 when decrypting padded cipher"

can help me?

public class utils { public static string encrypt(string message,string secretphrase){ try{ messagedigest mdig=messagedigest.getinstance("md5"); byte[] digestedbytes=mdig.digest(secretphrase.getbytes("utf-8")); secretkeyspec keyspec=new secretkeyspec(digestedbytes,"aes"); cipher cipher=cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, keyspec); byte[] encryptedbytes=cipher.dofinal(message.getbytes("utf-8")); homecoming new string(encryptedbytes,"utf-8"); }catch(exception exc){ homecoming null; } } public static string decrypt(string message,string secretphrase){ try{ messagedigest mdig=messagedigest.getinstance("md5"); byte[] digestedbytes=mdig.digest(secretphrase.getbytes("utf-8")); secretkeyspec keyspec=new secretkeyspec(digestedbytes,"aes"); cipher cipher=cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.decrypt_mode, keyspec); byte[] encryptedbytes=cipher.dofinal(message.getbytes("utf-8")); homecoming new string(encryptedbytes,"utf-8"); }catch(exception exc){ homecoming null; } }

}

well seek ,

public string encrypt(string str) { seek { // encode string bytes using utf-8 byte[] utf8 = str.getbytes("utf8"); // encrypt byte[] enc = ecipher.dofinal(utf8); // encode bytes base64 string homecoming new sun.misc.base64encoder().encode(enc); } grab (exception e) { } homecoming null; } public string decrypt(string str) { seek { // decode base64 bytes byte[] dec = new sun.misc.base64decoder().decodebuffer(str); // decrypt byte[] utf8 = dcipher.dofinal(dec); // decode using utf-8 homecoming new string(utf8, "utf8"); } grab (exception e) { } homecoming null; }

example using it

try { // generate temporary key. in practice, save key. // see encrypting des using pass phrase. secretkey key = keygenerator.getinstance("des").generatekey(); // create encrypter/decrypter class desencrypter encrypter = new desencrypter(key); // encrypt string encrypted = encrypter.encrypt("don't tell anybody!"); // decrypt string decrypted = encrypter.decrypt(encrypted); } grab (exception e) { }

java jsp java-ee security

No comments:

Post a Comment