vb.net - Receiving 'The input is not a complete block' error, tried everything I can find on google. Rjindael Encryption in .NET -


mornin', i'm trying basic encryption working using system.security.cryptography.rjindaelmanaged. have google error , cannot find problem, or doing wrong. attempting encrypt string, , decrypt string.

following code, , appreciated.

imports system.security.cryptography imports system.text public rj new rijndaelmanaged     try         rj.padding = paddingmode.none         rj.generatekey()         rj.generateiv()         dim curprovider new aescryptoserviceprovider         dim curencryptor icryptotransform         dim memencstream new memorystream         dim cryptoencstream cryptostream         curencryptor = curprovider.createencryptor(rj.key, rj.iv)         cryptoencstream = new cryptostream(memencstream, curencryptor, cryptostreammode.write)         dim startingbytes() byte = encoding.ascii.getbytes("this test")         debug.print("before length: " & startingbytes.length)         debug.print("before text: " & encoding.ascii.getstring(startingbytes))         ecryptoencstream.write(startingbytes, 0, startingbytes.length)         cryptoencstream.flushfinalblock()         memencstream.position = 0         dim thebytes(memencstream.length) byte         memencstream.read(thebytes, 0, memencstream.length)         memencstream.flush()         memencstream.close()         cryptoencstream.close()         debug.print("how long? " & thebytes.length)         debug.print("data: " & encoding.ascii.getstring(thebytes))         dim curdecryptor icryptotransform         curdecryptor = curprovider.createdecryptor(rj.key, rj.iv)         dim memdecstream new memorystream         dim cryptodecstream cryptostream         curdecryptor = curprovider.createdecryptor(rj.key, rj.iv)         cryptodecstream = new cryptostream(memdecstream, curdecryptor, cryptostreammode.write)         dim endingbytes() byte = thebytes         debug.print("before length: " & thebytes.length)         debug.print("before text: " & encoding.ascii.getstring(thebytes))         cryptodecstream.write(thebytes, 0, thebytes.length)         cryptodecstream.flushfinalblock()         memdecstream.position = 0         dim endbytes(memdecstream.length) byte         memdecstream.read(thebytes, 0, memdecstream.length)         memdecstream.flush()         memdecstream.close()         cryptoencstream.close()         debug.print("how long? " & endbytes.length)         debug.print("data: " & encoding.ascii.getstring(endbytes))     catch ex exception         debug.print(ex.tostring)     end try 

it appears problem length of data passing decryption stream. if change declaration of thebytes this:

dim thebytes(memencstream.length) byte 

to this:

dim thebytes(memencstream.length - 1) byte 

then runs fine (at least did me). i'm not vb wizard @ all, think array declaration 1 byte longer given size (i think 0 n). byte passed decryption stream, not work.

and i'm sure see enough, printing of final decrypted text not quite right. printing thebytes instead of endbytes.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -