对Android的图像文件加密

对Android的图像文件加密

本文介绍了流问题|密码(输入输出) - 对Android的图像文件加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的图像文件进行加密在Android上使用密码加密技术。要保存加密图像我只是这样做:

I'm trying to encrypt image files on Android with password based encryption. To save the encrypted image I just do this:

FileOutputStream fos = new FileOutputStream(thumbnailFile);
CipherOutputStream cos = new CipherOutputStream(fos, encryptCipher);
Bitmap thumbnail = Bitmap.createScaledBitmap(bm2, 140, 140, true);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 80, cos);

和读它,这样的:

FileInputStream fis = new FileInputStream(f);
CipherInputStream cis = new CipherInputStream(fis, decryptCipher);
Bitmap b = BitmapFactory.decodeStream(cis);

但位图最终为空。在code工作,当我绕过加密;那就是当我使用的文件(输入|输出)流,而不是密码。(输入|输出)流

but the Bitmap ends up as null. The code works when I bypass the encryption; that is when I use the File(Input|Output)Streams rather than the Cipher(Input|Output)streams.

我的加密算法创建如下:

My Ciphers are created as follows:

public void initCiphers(char password[]) {

PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;

byte[] salt = {
   (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
   (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
int count = 20;
pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(password);
try {
    keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    encryptCipher = Cipher.getInstance("PBEWithMD5AndDES");
    decryptCipher = Cipher.getInstance("PBEWithMD5AndDES");
    encryptCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    decryptCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
} catch (Exception e) {
    Log.v("tag", e.toString());
}

我没有得到任何的异常。

I don't get any exceptions.

显然有一些问题,使用密码(输出|输入)流了Android功能编码和/或解码的图像,但由于这些功能是不透明的,也没有例外,它很难知道它是什么。我怀疑它有做填充或冲洗。任何援助将感激AP preciated。

There is obviously some problem with using Cipher(Output|Input)Streams with the android functions for encoding and/or decoding images, but since those functions are opaque and there are no exceptions, its hard to know what it is. I suspect it has to do with padding or flushing. Any assistance would be gratefully appreciated.

推荐答案

在写一个CipherOutputStream,请确保您关闭()将数据写入后的数据流(和之前,它不会关闭底层流)。截止确保正确的增加填充。 A 的flush()是不够的在这里。

When writing to a CipherOutputStream, make sure you close() the stream after writing the data (and not closing the underlying stream before it). Closing makes sure the right padding is added. A flush() alone is not enough here.

另外,我会建议不使用DES的新协议 - pferred时下$ P $是AES

Also, I would advise to not use DES for new protocols - preferred nowadays is AES.

这篇关于流问题|密码(输入输出) - 对Android的图像文件加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:13