Android AES加密/解密 - 输入流& bytearrayoutputstream

时间:2011-07-27 13:46:56

标签: android encryption aes inputstream outputstream

所以基本上我是从asses文件夹加密图像然后我解密它。我把Logs放在代码中的几个地方所以我可以看到InputStream的实际大小,在bytearrayoutputstream中复制输入流之后的字节,加密后和解密后的图像大小(我认为加密/解密没有问题,但不是很确定)。问题是当我运行程序时,我在LogCat中得到了这个:

07-27 13:41:08.091: VERBOSE/Size(10546): Size of inputstream 29199
07-27 13:41:17.340: WARN/ActivityManager(52): Launch timeout has expired, giving up wake lock!
07-27 13:41:17.670: WARN/ActivityManager(52): Activity idle timeout for HistoryRecord{44defa50 com.example.pbe/.PBEencryptdecryptActivity}

就是这样。我使用的代码是:

package com.example.aes;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class PBEencryptdecryptActivity extends Activity {
    private int IO_BUFFER_SIZE;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        KeyGenerator keygen;
        try {
            keygen = KeyGenerator.getInstance("AES");
            SecretKey aesKey = keygen.generateKey();
            Cipher aesCipher,aesCipherDec;

            AssetManager am = this.getAssets();
            InputStream is = am.open("007FRAMESUPERIOR.jpg"); // get the encrypted image from assets folder
            Log.v("Size","Size of inputstream "+is.available());


            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            byte[] b = new byte[IO_BUFFER_SIZE];  

            int read;  
            while ((read = is.read(b)) != -1) {  //convert inputstream to bytearrayoutputstream
                baos.write(b, 0, read);
            }
            Log.v("Size","Size of b "+b.length);

            aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");     // Create the cipher            
            aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);    // Initialize the cipher for encryption                         
            byte[] ciphertext = aesCipher.doFinal(b);   // Encrypt the cleartext
            Log.v("Size","Size of image encrypted "+ciphertext.length);


            aesCipherDec =  Cipher.getInstance("AES/ECB/PKCS5Padding");
            aesCipherDec.init(Cipher.DECRYPT_MODE, aesKey); // Initialize the same cipher for decryption                        
            byte[] cleartext1 = aesCipher.doFinal(ciphertext);  // Decrypt the ciphertext                   

            //Bitmap bitmap = BitmapFactory.decodeByteArray(cleartext1 , 0,  cleartext1.length);    //decoding bytearrayoutputstream to bitmap
            Log.v("Size","Size of image decrypted "+cleartext1.length);

        } catch (Exception e) {
            e.printStackTrace();
            Log.v("Error", "Error Occured "+e);
        }

    }
}

实际上我在想如何设置private int IO_BUFFER_SIZE;的大小,这样我就可以将所有输入流复制到输出流而不会丢失任何数据。任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:1)

我认为有一个错误:您只加密缓冲区而不是整个文件的内容。

阅读循环后你必须打电话

b = baos.tobyteArray();

之后,您可以将IO_BUFFER_SIZE减少到您想要的任何数字。通常4094是一个很好的数字。

此外,我将使用您需要的确切大小初始化byteArrayOutputStream:

ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());  
相关问题