在Android上加密图像文件 - 密码(输出|输入)流问题

Mat*_*ing 13 java android

我正在尝试使用基于密码的加密来加密Android上的图像文件.要保存加密图像,我只需这样做:

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);
Run Code Online (Sandbox Code Playgroud)

并阅读它,这:

FileInputStream fis = new FileInputStream(f);
CipherInputStream cis = new CipherInputStream(fis, decryptCipher);
Bitmap b = BitmapFactory.decodeStream(cis);
Run Code Online (Sandbox Code Playgroud)

但是Bitmap最终为null.当我绕过加密时代码有效; 那是我使用文件(输入|输出)流而不是密码(输入|输出)流.

我的密码创建如下:

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()); 
}
Run Code Online (Sandbox Code Playgroud)

我没有任何例外.

使用带有android函数的Cipher(输出|输入)Streams进行编码和/或解码图像显然存在一些问题,但由于这些函数是不透明的并且没有例外,因此很难知道它是什么.我怀疑它与填充或冲洗有关.任何帮助将不胜感激.

小智 1

您可以子类化 CipherOutputStream 或什至只是 OutputStream,并且只重写lush() 方法而不执行任何操作。