Android AES加密/解密-inputstream &字节数组输出流
所以基本上我是从asses文件夹中加密图像,然后解密它。我将日志放在代码中的几个位置,这样我就可以看到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;
的大小,以便我可以将所有输入流复制到输出流而不丢失任何数据。任何帮助将非常感激!
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
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为有一个错误:您仅加密缓冲区而不是整个文件的内容。
在读取循环之后,您必须调用
Afterwards,您可以将 IO_BUFFER_SIZE 减少到您想要的任何数字。通常 4094 是一个不错的数字。
此外,我将使用您需要的确切大小来初始化 byteArrayOutputStream:
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
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: