When you point 100 to your call to fIn.Read(...) , you say "Read up to 100 bytes" - note that the actual number of bytes may be different; You must use the return value to determine how many of them have actually been read.
And in the call to fOut.Write you assume that the output of cc.Crypt(bin,key,iv,true) will be exactly 100 bytes, which is not a valid assumption. Also note that you always encrypt all 100 bytes of bin , even if you only read 1 from your file. If you read less than 100, you will encrypt everything that was "left" in bin (possibly 0s if not previously used).
Fix these length problems and you should be on the right track. Sort of:
while (rdlen < totlen) { len = fIn.Read(bin, 0, 100); // note that we are encrypting all of "bin" here, may want to only // encrypt "len" bytes.. byte[] encrypted = cc.Crypt(bin, key, iv, true); fOut.Write(encrypted, 0, encrypted.Length); rdlen = rdlen + len; }
source share