기본적으로 나는 asses 폴더의 이미지를 암호화하고 암호를 해독합니다. Logs를 코드의 몇 군데에 배치하여 실제 크기의 InputStream을 볼 수 있습니다. 바이트 Bytearrayoutputstream에 입력 스트림을 복사 한 후, 암호화 및 해독 후 이미지 크기 (암호화/해독에는 문제가 없다고 생각되지만 실제로는 확실하지 않습니다.) 그리고 문제는 프로그램을 실행할 때 LogCat에서 얻는 것입니다. 내가 사용Android AES 암호화/암호 해독 - inputstream 및 bytearrayoutputstream
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);
}
}
}
사실 나는 모든 입력 스트림을 모든 data.Any 도움을 잃지 않고 outputstream에 복사 할 수 있도록 private int IO_BUFFER_SIZE;
크기를 어떻게 설정할 수 있을지 생각하고 있습니다. 정말 감사하겠습니다!
이 사실 나는 그것이 작동 생각 :
은 또한 당신이 필요로하는 정확한 크기로있는 ByteArrayOutputStream를 초기화 할 이미지의 29,200 크기 암호화 된 이미지의 B 29199 크기의 InputStream 29199 크기의 크기는 29216. 및 해독 왜 해독 된 이미지가 암호화 된 이미지보다 더 많은 바이트를 가진 이유입니까? – hardartcore
AES는 블록 암호이므로 8 바이트 블록에서 작동합니다. 따라서 출력은 8로 나눌 수 있어야합니다. 데이터가 암호화되기 전에 해독시 자동으로 제거되는 패딩 바이트가 추가됩니다. – Robert