Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: image and encrypted byte array

Reply
Thread Tools

Re: image and encrypted byte array

 
 
Stefan Rybacki
Guest
Posts: n/a
 
      11-14-2008
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>>&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

 
Reply With Quote
 
 
 
 
vaneric
Guest
Posts: n/a
 
      11-15-2008
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>>&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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-15-2008
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>>&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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
Re: how to extend a byte[] array with a null byte? Tom McGlynn Java 4 04-18-2008 11:49 PM
Re: how to extend a byte[] array with a null byte? Patricia Shanahan Java 0 04-17-2008 06:47 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments
 



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