从PNG图像的lsb像素中获取字符串

时间:2015-05-15 14:56:37

标签: java android image steganography

我编写了一个代码,用于修改PNG图像每个像素的最低有效位,然后将图像保存在外部存储器中。现在,当我打开PNG图像时,我不知道如何进行反向操作,这意味着我不知道如何从这些位获取字符串。有人可以解释我吗?感谢。

这是将字符串插入最不重要节拍的代码。

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {
        case FILE_SELECT_CODE:
            if(resultCode==RESULT_OK)
            {
                Uri uri2 = data.getData();
                String[] proj = {MediaStore.Images.Media.DATA};
                CursorLoader cursorLoader = new CursorLoader(this,uri2,proj,null,null,null);
                Cursor cursor = cursorLoader.loadInBackground();
                int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String uri = cursor.getString(column_index);
                Bitmap img = BitmapFactory.decodeFile(uri);
                int height = img.getHeight();
                int width = img.getWidth();
                int[] pixel = new int[height * width];
                img.getPixels(pixel, 0, width, 1, 1, width - 1, height - 1);
                String key = "prova";
                byte[] b = key.getBytes();
                int count = 0;
                for (byte current_byte : b) {
                    for (int j = 7; j >= 0; j--) {
                        int lsb = (current_byte >> j) & 1;
                        pixel[count] = (pixel[count] & 0xfffffffe) + lsb;
                        count++;
                    }
                }
                Bitmap newImg = Bitmap.createBitmap(pixel,0,width, width-1,height-1, Bitmap.Config.ARGB_8888);
                ImageView imageView = new ImageView(this);
                imageView.setImageBitmap(newImg);
                setContentView(imageView);
                String filename = "myimage.png";
                File file2 = new File(getExternalFilesDir(null), filename);
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(file2);
                    newImg.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
                catch (Exception e1)
                {
                    e1.printStackTrace();
                }
            }
            break;
    }
}

以下是从这些位读取字符串的代码的开始。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    Uri uri2 = intent.getData();
    String[] proj = {MediaStore.Images.Media.DATA};
    CursorLoader cursorLoader = new CursorLoader(this,uri2,proj,null,null,null);
    Cursor cursor = cursorLoader.loadInBackground();
    int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String uri = cursor.getString(column_index);
    Bitmap img = BitmapFactory.decodeFile(uri);
    int height = img.getHeight();
    int width = img.getWidth();
    int[] pixel = new int[height * width];
    img.getPixels(pixel, 0, width, 1, 1, width - 1, height - 1);

在这部分的最后,我从图像中获取了像素数组,我将使用它从最不重要的节拍中获取字符串。

PS:我不能使用BufferedImage。

1 个答案:

答案 0 :(得分:0)

喜欢这个吗?

int[] pixel;
byte[] b = new byte[5 * 2]; // Default Java string encoding uses 2 bytes per symbol
// Read .png, fill pixel array etc
// Also you might wish to save string length to the pixel array first

for (int count = 0; count < pixel.length / 8 && count < b.length; count++) {
    int current_byte = 0;
    for (int j = 7; j >= 0; j--) {
        int lsb = (pixel[count * 8 + j] & 1) << j;
        current_byte += lsb;
    }
    b[count] = current_byte;
}
String key = new String(b, java.nio.charset.Charset.defaultCharset ()); // You might want to consider UTF-8