Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: image and encrypted byte array (http://www.velocityreviews.com/forums/t644441-re-image-and-encrypted-byte-array.html)

Stefan Rybacki 11-14-2008 10:31 AM

Re: image and encrypted byte array
 
vaneric schrieb:
> hi
> recently i came across a page http://en.wikipedia.org/wiki/Block_c...s_of_operation
> where they illustrate the defect of ECB mode using an image created
> from an encrypted byte array.As a learning exercise i wanted to take
> an image ,get its pixels and encrypt the data using different modes
> and try to reproduce the picture.
> But in java api ,image data is obtained as int[] not byte[] .I want to
> know how i can use this for the exercise.converting from int to byte
> seems to be lossy.Is there some way i can get the bytes of the image
> and then work on that byte[]? Finally ,is there a way to create an
> image out of the byte[]?


you could just extract the r,g,b and a byte values from the int value.

int pixel=...;

byte a=pixel>>24;
byte r=(pixel>>16)&255;
byte g=(pixel>>8)&255;
byte b=pixel&255;

assuming the order of color components is argb. But this should actually be
irrelevant in your case as long as you restore it in the same order you should
be fine.

>
> any help grately appreciated
> thanks in advance
> eric


vaneric 11-15-2008 05:09 PM

Re: image and encrypted byte array
 
On Nov 14, 3:31*pm, Stefan Rybacki <noem...@noemail.foobar> wrote:

> you could just extract the r,g,b and a byte values from the int value.



hi,
thanks for the reply.
i wrote the following code
int pixel=...
byte a=(byte)(pixel>>24);
byte r=(byte)((pixel>>16)&255);
byte g=(byte)((pixel>>8)&255);
byte b=(byte)(pixel & 255);
System.out.println("a="+a+"\nr="+r+"\ng="+g+"\nb=" +b);


Also I tried the byteValue() method in class Integer.
Integer in=new Integer(pixel);
byte byteval=in.byteValue();
System.out.println("byteval="+byteval);

This is what i am getting for different values of pixel.
1) pixel=-5334383
a=-1, r=-82, g=-102, b=-111
byteval=-111

2) pixel=-10526881
a=-1, r=95, g=95, b=95
byteval=95

3) pixel=-11192268
a=-1, r=85, g=56, b=52
byteval=52

It seems that the byteVal() always returns the 'b' component .Is there
any particular reason for that?


Also,if i am to create a byte[] to represent the image should i make
byte[] of length=3*number of pixels in the image so as to put r,g,b
byte values of first pixel in the first 3 byte[] cells and so on? I
need to pass that byte[] to another application so that it can be
encrypted and later used to recreate an image.

Is there something wrong in this approach?
thanks
eric

Lew 11-15-2008 05:30 PM

Re: image and encrypted byte array
 
vaneric wrote:
> i [sic] wrote the following code
> int pixel=...
> byte a=(byte)(pixel>>24);
> byte r=(byte)((pixel>>16)&255);
> byte g=(byte)((pixel>>8)&255);
> byte b=(byte)(pixel & 255);
> System.out.println("a="+a+"\nr="+r+"\ng="+g+"\nb=" +b);

....
> Also I tried the byteValue() method in class Integer.
> Integer in=new Integer(pixel);
> byte byteval=in.byteValue();
> System.out.println("byteval="+byteval);
> ...
> It seems that the byteVal() always returns the 'b' component .Is there
> any particular reason for that?


Yes. Did you look at the Javadocs for the method? They clearly state:
> Returns the value of this Integer as a byte.


What else would you expect it to do?

> Also,if i [sic] am to create a byte[] to represent the image should i [sic] make
> byte[] of length=3*number of pixels in the image so as to put r,g,b
> byte values of first pixel in the first 3 byte[] cells and so on? I
> need to pass that byte[] to another application so that it can be
> encrypted and later used to recreate an image.


Whichever ever way you unroll the ints into bytes, you must do the exact
reciprocal to roll them back into ints. It doesn't matter much what way that
is as long as you ensure that condition.

--
Lew


All times are GMT. The time now is 08:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57