AES encrypt C# decrypt Java? -
i'm trying decrypt c# encrypted data in java without success. i'm using 128 bit key&block size pkcs7 padding. here's c# code:
public byte[] encrypt(byte[] data, byte[] key) { using (var ms = new memorystream()) { using (var aes = rijndaelmanaged.create()) { aes.key = key; aes.iv = key; var stream = new cryptostream(ms, aes.createencryptor(aes.key, aes.iv), cryptostreammode.write); stream.write(data, 0, data.length); stream.flushfinalblock(); return ms.toarray(); } } }
and java code:
private static key generatekey() throws exception { key key = new secretkeyspec(files.readallbytes(paths.get("d:/temp/cr.key")), "aes"); return key; } public static byte[] decrypt(byte[] encrypteddata) throws exception { key key = generatekey(); cipher c = cipher.getinstance("aes/cbc/pkcs7padding", "bc"); ivparameterspec ivspec = new ivparameterspec(key.getencoded()); c.init(cipher.decrypt_mode, key, ivspec); system.out.println(c.getblocksize()); c.update(encrypteddata); byte[] decvalue = c.dofinal(); return decvalue; } public static void main(string[] args) throws exception { security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); byte[] bb = decrypt(files.readallbytes(paths.get("d:\\temp\\cr~\\oebps\\chapter001.html"))); //decompressfile(bb, new file("d:\\temp\\enc.html")); }
any idea wrong?
thanks
update
sorry, how dumb of me, forgot write actual error message. here is:
exception in thread "main" javax.crypto.badpaddingexception: pad block corrupted @ org.bouncycastle.jcajce.provider.symmetric.util.baseblockcipher.enginedofinal(unknown source) @ javax.crypto.cipher.dofinal(cipher.java:1970)
thanks
you forgetting cipher.update(byte[]): byte[]
return data whenever full block of ciphertext has been processed.
note: apparently not answer question, exception points error. bug in above code none-the-less.