java - GNU Crypto Encrypt returns blank string -
i'm using gnu crypto library encrypt simple strings. believe have followed documentation correctly, problem returns blank string (in case 5 characters) of spaces. i'm not sure whether miss coded or if encoding issue. hope not embarrassingly simple.
import gnu.crypto.cipher.cipherfactory; import gnu.crypto.cipher.iblockcipher; import java.util.hashmap; import java.util.map; public class ftnsamain { public static void main(string[] args) throws exception { string data = "apple"; string key = "abcdefghijklmnop"; byte[] temp = encrypt(data.getbytes(), key.getbytes(), "aes"); system.out.println(new string(temp)); } public static byte[] encrypt(byte[] input, byte[] key, string algorithm) throws exception { byte[] output = new byte[input.length]; iblockcipher cipher = cipherfactory.getinstance(algorithm); map attributes = new hashmap(); attributes.put(iblockcipher.cipher_block_size, 16); attributes.put(iblockcipher.key_material, key); cipher.init(attributes); int bs = cipher.currentblocksize(); (int = 0; + bs < input.length; += bs) { cipher.encryptblock(input, i, output, i); } return output; } }
gnu crypto documentation have following void encryptblock(..)
methode:
encrypts block of bytes plaintext starting @ inoffset, storing encrypted bytes in ciphertext, starting @ outoffset. programmer ensure there @ least 1 full block in plaintext inoffset , space 1 full block in ciphertext outoffset. java.lang.illegalstateexception thrown if cipher has not been initialized.
your input:
string data = "apple";
is not full datablock aes needs data in blocks of 16 bytes. also, output buffer short.
for starters, try encrypting input ends 16 bytes like:
string data = "apple56789abcdef";
Comments
Post a Comment