c# - How to encrypt jpeg file correctly using xor operation -


i have problem encrypting jpeg file using xor operation. here how decoding file:

        stream imagestreamsource = new filestream(filename, filemode.open,   fileaccess.read, fileshare.read);         jpegbitmapdecoder decoder = new jpegbitmapdecoder(imagestreamsource, bitmapcreateoptions.preservepixelformat, bitmapcacheoption.default);         bitmapsource bitmapsource = decoder.frames[0];          return bitmapsource;  

here how encoding , encrypting it(bs decoded bitmapsource):

        int stride = bs.pixelwidth * 3;         int size = bs.pixelheight * stride;         byte[] pixels = new byte[size];         bs.copypixels(pixels, stride, 0);          pixels = xoring(pixels, size);          int width = bs.pixelwidth;         int height = bs.pixelheight;          bitmapsource image = bitmapsource.create(             width,             height,             bs.dpix,             bs.dpiy,             bs.format,             bs.palette,             pixels,             stride);          filestream stream = new filestream(outname, filemode.create);         jpegbitmapencoder encoder = new jpegbitmapencoder();         textblock mytextblock = new textblock();         mytextblock.text = "codec author is: " + encoder.codecinfo.author.tostring();         encoder.fliphorizontal = false;         encoder.flipvertical = false;         encoder.qualitylevel = 100;         encoder.rotation = rotation.rotate0;         encoder.frames.add(bitmapframe.create(image));         encoder.save(stream); 

this xoring function:

    public byte[] xoring(byte[] data, int size)     {         const string key = "abc";         (int = 0; < size; i++)             data[i] = (byte)(data[i] ^ (byte)key[i % key.length]);         return data;     } 

i expecting image completly noise, getting this: http://i.imgur.com/ynqqw8d.png

this original file: http://i.imgur.com/pilmdgl.png

i appreciate help! seems if 1 color channel encrypted...

if use constant key there no chance decent level of security. in fact, images show of data still 'jump' out of resulting image..

the missing component way change encoding key during encrytion. obviuos way use random generator create new encoding byte each xor operation.

this way real key seed use setup random @ class level(!):

random r = new randowm(2014); 

or possibly like:

random r = new randowm(imagestreamsource.length); 

this set in way allow decode later.

you xor creating fresh key as

byte key = (byte) r.next(256); 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -