仅加密图像文件的内容而不加密整个文件

我正在创建一个APP,只需要记录图像的内容。 我需要在转换后文件仍然是图像,但显示的图像不显示为原始图像。

例如,我将加密的图像发送给其他用户,这个图像将能够显示和图像(但不是原始图像),但原始图像在该文件中被加密。

使用以下algorythm我加密了整个文件,由于标头也被加密,因此无法将其作为图像打开。

我正在使用这个algorythm,但我不知道如何只加密数据或如何在java / android中添加/修改图像的标题:

public byte[] encrypt_image(Bitmap bm, String password_) { byte[] encryptedData = null; try{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); byte[] keyStart = password_.getBytes(); KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(keyStart); kgen.init(128, sr); SecretKey skey = kgen.generateKey(); byte[] key = skey.getEncoded(); // Encrypt encryptedData = Security.encrypt(key,b); }catch (Exception e) { Log.e("encrpyt_image()", e.getMessage()); } return encryptedData; } 

任何人都知道如何编写这个,我一直在互联网上搜索没有成功。

我想get / setPixels方法可能是最简单的方法。

 int[] pixels = new int[bm.getWidth() * bm.getHeight()]; bm.getPixels(pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight()); encryptIntArray(pixels); bm.setPixels(pixels, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight()); 

现在你只需要编写encryptIntArray方法。

编辑:您也可以尝试使用ByteBuffer,然后您不必进行转换。

 ByteBuffer bb = ByteBuffer.allocate(bm.getByteCount()); bm.copyPixelsToBuffer(bb); byte[] b = bb.array(); bm.copyPixelsFromBuffer(ByteBuffer.wrap(Security.encrypt(key,b))); 

我没有测试过那段代码。