加密图像文件不完整,只有文件的内容不完整、图像文件、文件、内容

2023-09-06 22:59:07 作者:笑到最后的是老子

我建立的APP和需要encript的图像的唯一的内容。我需要该文件仍然是转换后的图像,但图像显示不显示为原始。

I am creating an APP and need to encript only the content of an image. I need that the file was still an image after conversion, but the image showed does not show as the original.

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

For example, I will send the image encrypted to other user and this one will be able to show and image (but not the original), but the original image was encrypted in that file.

使用以下algorythm予加密整个文件,并且这不能被打开,由于图象首标被加密,以及

With the following algorythm I encrypted the entire file, and this cannot be opened as image due the header is encrypted as well.

我用这algorythm,但我不知道该怎么只加密数据,或如何添加/修改图像的标题中的Java / Android的:

I am using this algorythm but I do not know how to only encrypt data or how to add/modify the headers of an image in 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;
}

任何人有关于如何编纂了一个想法,我一直在寻找在网上,但没有成功。

Anyone has an idea about how to codify that, I've been searching in internet with no success.

推荐答案

我想获取/ setPixels方法可以做到这一点最简单的方法。

I guess the get/setPixels methods may be the easiest way to do this.

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方法。

Now you just have to write the encryptIntArray method.

编辑: 你也可以尝试使用ByteBuffer的,那么你就不必做转换。

edit: You could also try to use a ByteBuffer, then you don't have to do the conversion.

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

我没有测试过code虽然。

I haven't tested that code though.

 
精彩推荐
图片推荐