c# - Add cleartext bytes to the beginning of a CryptoStream? -


i have interface defined so:

public interface iencryptionservice {     stream encrypt(stream cleartext);     stream decrypt(stream encrypted); } 

i implementing interface aescryptoserviceprovider, there's problem here. iv (initialization vector) not returned on interface... encrypting work fine, long have no desire decrypt ever again. decrypt() method has no chance @ of working.

what want include iv in cleartext @ beginning of stream, add cryptostream it, encrypted data "header" strip off , use decrypting stream.

so... how that? can create cryptostream easy enough, looks encrypt iv, kinda defeats purpose. load cryptostream memory, prepend iv, , stream out memorystream, inefficient, , die on large streams.

what good, secure, scalable practice this?

here had in mind. see how write iv memorystream , follow crypto? when want decrypt, pull iv off first in same way.

sorry, been long time. 1 working. should scale if don't cast ms toarray(); @ end. example write filestream go , should not need memory @ all. demo prepending iv.

    private byte[] encrypt(byte[] originalplaintextbytes)     {         using (symmetricalgorithm algorithm = aescryptoserviceprovider.create())         {             algorithm.generatekey();             algorithm.generateiv();             byte[] iv = algorithm.iv;             byte[] key = algorithm.key;             using (icryptotransform encryptor = algorithm.createencryptor(key, iv))             {                 using (memorystream ms = new memorystream())                 using (cryptostream cs = new cryptostream(ms, encryptor,cryptostreammode.write))                 {                     binarywriter bw = new binarywriter(ms);                     bw.write(iv);                     cs.write(originalplaintextbytes, 0, originalplaintextbytes.length);                     return ms.toarray();                 }             }         }     } 

ok rather edit above code, here whole program randomly creates plaintext file of 1 megabyte. encrypts ciphertext. note program not ever need 1 megabyte of memory in operate. scalable. again, before, program demonstrate concept, , better readbuffer larger 1 byte. did not want create , obscure core answer. hope helps. think kind of approach need.

using system; using system.io; using system.security.cryptography; using system.windows.forms;  namespace so_aes {     public partial class form1 : form     {         random ran = new random();         public form1()         {             initializecomponent();             using (var file = file.open("plaintext.txt", filemode.openorcreate))             {                 byte[] junkbytes = new byte[1000];                 (int = 0; < 1000; i++)                 {                     (int j = 0; j < 1000; j++)                     {                         junkbytes[j] = (byte)ran.next(0, 255);                     }                     file.write(junkbytes, 0, junkbytes.length);                 }             }              using (var plaintextfile = file.open("plaintext.txt", filemode.open))             {                 using (var cryptotextfile = file.open("crypto.txt", filemode.openorcreate))                 {                     encrypt(plaintextfile, cryptotextfile);                 }             }         }          void encrypt(stream instream, stream outstream)         {             using (symmetricalgorithm algorithm = aescryptoserviceprovider.create())             {                 algorithm.generatekey();                 algorithm.generateiv();                 byte[] iv = algorithm.iv;                 byte[] key = algorithm.key;                 using (icryptotransform encryptor = algorithm.createencryptor(key, iv))                 {                     using (cryptostream cs = new cryptostream(outstream, encryptor, cryptostreammode.write))                     {                         binarywriter bw = new binarywriter(outstream);                         bw.write(iv);                         byte[] readbuffer = new byte[1];                         binaryreader br = new binaryreader(instream);                         while (br.read(readbuffer, 0, readbuffer.length) != 0)                         {                             cs.write(readbuffer, 0, 1);                         }                     }                 }             }             instream.close();             outstream.close();         }     } } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -