Re: image and encrypted byte array

Discussion in 'Java' started by Stefan Rybacki, Nov 14, 2008.

  1. vaneric schrieb:
    > hi
    > recently i came across a page http://en.wikipedia.org/wiki/Block_cipher_modes_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
    Stefan Rybacki, Nov 14, 2008
    #1
    1. Advertising

  2. Stefan Rybacki

    vaneric Guest

    On Nov 14, 3:31 pm, Stefan Rybacki <> 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
    vaneric, Nov 15, 2008
    #2
    1. Advertising

  3. Stefan Rybacki

    Lew Guest

    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
    Lew, Nov 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

    =?Utf-8?B?RGVhc3Vu?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    897
    =?Utf-8?B?RGVhc3Vu?=
    Jul 14, 2005
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,244
    Roedy Green
    Aug 5, 2003
  3. Kirby
    Replies:
    3
    Views:
    640
    Kirby
    Oct 8, 2004
  4. Replies:
    20
    Views:
    9,767
    licebmi
    Sep 7, 2009
  5. Tom McGlynn
    Replies:
    4
    Views:
    854
    Mark Space
    Apr 19, 2008
Loading...

Share This Page