Encryption message in java -
i project using java's bouncycastle encryption.
however, when encrypt message, throws exception me.
javax.crypto.illegalblocksizeexception: data not block size aligned
i using blowfish/ecb/nopadding, , message xml.
public static void main(string args[]){ string message = "<abc>abcdefg</abc>"; string key = "key"; byte[] b = encrypt(message.getbytes(), key.getbytes()); } public byte[] encrypt(byte encrypt[], byte en_key[]) { try { secretkeyspec key = new secretkeyspec(en_key, "blowfish"); cipher cipher = cipher.getinstance("blowfish/ecb/nopadding"); cipher.init(cipher.encrypt_mode, en_key); return cipher.dofinal(encrypt); } catch (exception e) { e.printstacktrace(); return null; } }
could can me?
thank you
you using nopadding
, size of input data must not match block size of cipher, illegalblocksizeexception
being thrown. if use nopadding need make sure input multiple of 8 bytes.
specify padding scheme. change blowfish/cbc/pkcs5padding
, should work.
padding manually null bytes:
create new array bigger size multiple of 8 , copy old array it.
public static byte[] encrypt(byte encrypt[], byte en_key[]) { if(encrypt.length % 8 != 0){ //not multiple of 8 //create new array size multiple of 8 byte[] padded = new byte[encrypt.length + 8 - (encrypt.length % 8)]; //copy old array system.arraycopy(encrypt, 0, padded, 0, encrypt.length); encrypt = padded; } try { secretkeyspec key = new secretkeyspec(en_key, "blowfish"); cipher cipher = cipher.getinstance("blowfish/ecb/nopadding"); cipher.init(cipher.encrypt_mode, key); return cipher.dofinal(encrypt); } catch (exception e) { e.printstacktrace(); return null; } }
Comments
Post a Comment