【问题标题】:Best practice on deleting/zeroing out a security sensitive image?删除/清零安全敏感图像的最佳实践?
【发布时间】:2016-01-26 15:53:46
【问题描述】:

谁能指出我在清除敏感运行时映像方面具有最佳实践的资源?

考虑一个场景,在运行时从服务器下载敏感图像,加载到 Bitmap 对象中,然后在 Fragment 中的 ImageView 中显示。

当用户离开该屏幕,或者应用程序退出/放置在后台很长时间时,我想清除该图像数据以便不容易恢复。

我想知道是否有一种可靠的方法可以在包含图像的 Fragment 被销毁后立即将位图数据归零?

这对我来说感觉很棘手,因为位图通常作为不可变对象返回,例如BitmapFactory.decodeByteArray 说:

从指定的字节数组中解码一个不可变位图。

大概我必须创建一个可变的Bitmap,然后复制它的数据?

看起来recycle() 对我没有帮助,因为这只会将数据标记为可用于垃圾收集,而不会擦除它。

【问题讨论】:

  • 请添加您用于下载此图像的代码
  • 我没有使用图像库,并且我确保不会意外地将图像缓存在 Fragment 之外的任何位置,例如在磁盘或内存中(图像库通常具有两级 LRU 缓存)。图像数据在下载过程中被加密,因此 HTTP 缓存也不应该成为问题。
  • @Morrison,是的,我已经看到了 Bitmap.copyPixelsFromBuffer 方法,但我还没有尝试过 - 在进行详细测试之前,我正在征求有关最佳实践的建议。 :-)

标签: java android security bitmap


【解决方案1】:

您可以简单地使用清除Bitmap

someBitmap.eraseColor(android.graphics.Color.TRANSPARENT);

它将用TRANSPARENT 颜色填充位图并擦除上面的所有内容。 但是,如果您没有对位图的任何引用(例如,您已将 null 设置为包含您的 BitmapImageView,就像这样

someImageView.setDrawable(null)

垃圾收集器很快就会收集它。

【讨论】:

    【解决方案2】:

    感谢@IlyaGulya 的eraseColor 建议。下面是我目前写的代码。

    创建可变位图:

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inMutable = true;
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, bitmapOptions);
    

    清除我的 Fragment 中的图像数据的代码(当 Fragment 接收到 BitmapDrawable 时,我将它保存到 myBitmapDrawable 字段中):

    @Override
    public void onDestroy() {
        myImageView.setImageDrawable(null);
    
        try {
            MyUtils.zeroOutBitmapData(myBitmapDrawable.getBitmap());
        } catch (Exception e) {
            loggingUtil.logHandledException(e);
        }
    
        super.onDestroy();
    }
    

    我的位图归零实用程序:

    public static void zeroOutBitmapData(Bitmap mutableBitmap) {
        if (mutableBitmap.isMutable()) {
            mutableBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
        } else {
            logger.error("Expected bitmap to be mutable");
        }
    }
    

    ...这是一个单元测试(嗯,ApplicationTestCase,因为我想用真正的位图进行测试):

    public void testZeroOutBitmap() throws Exception {
        Resources resources = getContext().getResources();
    
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inMutable = true;
    
        Bitmap mutableBitmap = BitmapFactory.decodeResource(resources, R.drawable.an_example_image);
    
        // Assert that some pixels start out with non zero colors
        assertEquals(-789517, mutableBitmap.getPixel(0, 0));
        assertEquals(-723724, mutableBitmap.getPixel(10, 10));
    
        MyUtils.zeroOutBitmapData(mutableBitmap);
    
        // Check that pixel data has now been cleared out
        assertEquals(android.graphics.Color.TRANSPARENT, mutableBitmap.getPixel(0, 0));
        assertEquals(android.graphics.Color.TRANSPARENT, mutableBitmap.getPixel(10, 10));
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2013-06-19
      • 2014-01-17
      • 2023-03-08
      相关资源
      最近更新 更多