开发者

Android AES encryption/decryption - inputstream & bytearrayoutputstream

So basically I'm encrypting an image from asses folder and after that I decrypt it.I put Logs in a few places in code so I can see the real size of InputStream, bytes after the copying inputstream in bytearrayoutputstream, the size of image after encryption and after decryption (i think there is no problem with encryption/decryption,but not really sure).And the problem is when I run the program, I get this in 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}

and thats it.The code I'm using is :

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
     开发者_如何学Python       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);
        }

    }
}

Actually I'm thinking how can I set the size of private int IO_BUFFER_SIZE; so I can copy all inputstream to the outputstream without loosing any data.Any help will be really appreciate!


I think there is a mistake: you are encrypting only the buffer not the content of the whole file.

After the reading loop you have to call

b = baos.tobyteArray();

Afterwards you can reduce IO_BUFFER_SIZE to any number you want. Typically 4094 would be a good number.

Additionally I would initialize the byteArrayOutputStream with the exact size you need:

ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜