java - 文件解密时 JPEG 字节加密返回错误图像

标签 java android encryption byte jpeg

我正在编写一个简单的代码来加密 JPEG 图像文件的像素。该代码适用于加密,生成伪随机图像,但当我尝试解密时,它返回不等于原始图像。我正在使用简单的密码加密 (RC4)。

public class MainActivity extends Activity {

static byte[] keyValue = {'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y'};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imgView = (ImageView) findViewById(R.id.imageView1);

    Bitmap picture = getBitmapFromAsset("result.jpg");
    Bitmap resultPic = picture.copy(Bitmap.Config.ARGB_8888, true);
    picture.recycle();

    int width = resultPic.getWidth();
    int height = resultPic.getHeight();

    int[] pixels = new int[width * height];

    resultPic.getPixels(pixels, 0, width, 0, 0, width, height);

    //--------- perform encryption

    byte[] content = intArrToByteArr(pixels);

    try {

        content = decrypt(content);

    } catch (Exception e) { }

    pixels = byteArrToIntArr(content);

    //--------------------------------------------

    resultPic.setPixels(pixels, 0, width, 0, 0, width, height);

    imgView.setImageBitmap(resultPic);

    File file = new File(Environment.getExternalStorageDirectory().toString() + "/result.jpg");

    try {

        FileOutputStream fOut = new FileOutputStream(file);
        resultPic.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.close();
    }
    catch (Exception e) { }
}

public static byte[] encrypt(byte[] Data) throws Exception {

    Key key = generateKey();
    Cipher c = Cipher.getInstance("RC4");
    c.init(Cipher.ENCRYPT_MODE, key);

    byte[] encVal = c.doFinal(Data);

    return encVal;
}

public static byte[] decrypt(byte[] encryptedData) throws Exception {

    Key key = generateKey();
    Cipher c = Cipher.getInstance("RC4");
    c.init(Cipher.DECRYPT_MODE, key);

    byte[] decValue = c.doFinal(encryptedData);

    return decValue;
}

private static Key generateKey() throws Exception {

    Key key = new SecretKeySpec(keyValue, "RC4");

    return key;
}

public static byte[] intArrToByteArr(int[] input){

    ByteBuffer byteBuffer = ByteBuffer.allocate(input.length * 4);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(input);

    byte[] array = byteBuffer.array();

    return array;
}

public static int[] byteArrToIntArr(byte[] input){

    IntBuffer intBuf = ByteBuffer.wrap(input).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    int[] array = new int[intBuf.remaining()];
    intBuf.get(array);

    return array;
}

private Bitmap getBitmapFromAsset(String strName) {

    AssetManager assetManager = getAssets();
    InputStream istr = null;

    try {

        istr = assetManager.open(strName);
    } catch (IOException e) { }

    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    return bitmap;
}

}

这是正常的 jpeg 文件:http://postimg.org/image/m13xdbpi5/

这是加密的:http://postimg.org/image/dzsialsw1/

这是解密后的图像:http://postimg.org/image/pm4pv01hb/

最佳答案

您正在加密,但随后将结果保存为 JPEG。这是一种有损格式 - 再次加载后,您最终会得到一个与原始图像视觉上相似的图像,但很可能有一些不同的像素......这意味着它不会解密回到原始像素。

您应该检查是否可以加密/解密,而无需将值重新编码为图像。如果这些都有效,那么您就知道加密不是问题...但是当您将其编码为 JPEG 时,您仍然会遇到丢失信息的问题...

关于java - 文件解密时 JPEG 字节加密返回错误图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618698/

相关文章:

android - 在 Linux 中运行 Android 应用程序

php - 密码哈希无法登录 codeigniter

ruby - 创建一个安全的、基于 Web 的密码管理系统,能够在用户之间共享数据

java - 访问包含 JTable 的 JScrollpane

android - 在 android studio 中解决 gradle 依赖问题?

java - 将 JComboBox 中的项目保存到文本文件

android - 停止更新我的应用程序(禁用自动更新)

android - iPhone 和 Android 的通用加密过程

java - 使用 webpush-java CLI 工具进行未经授权的注册

java - System.getenv ("ProgramFiles") 返回 C :\Program Files (x86)