【发布时间】:2011-10-13 10:44:36
【问题描述】:
我做得对还是有错误?我仍在尝试实施的测试程序正在运行,没有任何异常或错误。但它没有做它必须做的事情,我找不到问题。
这是尝试解密的 Android 代码:
private static final int IO_BUFFER_SIZE = 4 * 1024;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
AssetManager am = this.getAssets();
InputStream is = am.open("2000_1.jpg_encrypted"); // get the encrypted image from assets folder
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);
}
//START
long start = System.currentTimeMillis()/1000L; // start
//byte[] keyStart = "MARTIN_123_MARTIN_123".getBytes(); // specific key value
KeyGenerator kgen = KeyGenerator.getInstance("AES/CBC/PKCS5Padding"); //aes
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
//sr.setSeed(keyStart);
kgen.init(128, sr);
//SecretKey skey = kgen.generateKey();
//byte[] key = skey.getEncoded();
byte[] key = "MARTIN_123_MARTIN_123".getBytes("UTF-8");
byte[] iv = "1234567890123456".getBytes("UTF-8");
byte[] decryptedData = decrypt(key, iv, b);
//END
long end = System.currentTimeMillis()/1000L; // end
Log.d("TEST","Time start "+ String.valueOf(start)); //showing the strat in ms
Log.d("TEST","Time end "+ String.valueOf(end)); //showing the end in ms
Bitmap bitmap = BitmapFactory.decodeByteArray(decryptedData , 0, decryptedData .length); //decoding bytearrayoutputstream to bitmap
//String filepath = Environment.getExternalStorageDirectory()+"bitmap";
FileOutputStream fos = new FileOutputStream("sdcard/DCIM/100ANDRO");
fos.write(decryptedData);
fos.close();
is.close(); // close the inputstream
baos.close(); // close the bytearrayoutputstream
}
catch(Exception e){
e.fillInStackTrace();
}
}
//decrypt
private byte[] decrypt(byte[] raw, byte[] iv, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
这是加密的 PHP 代码:
$folder = $this->getConfiguration()->getAppRootDir() . '/temp_encryption_testing/';
$files = array(
'007FRAMESUPERIOR.jpg',
'2000_1.jpg',
'APLICACIONdescargaliga.jpg',
'APPCOMMENTS.pdf',
'AUDIOVISUALFOTO02.jpg'
);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "MARTIN_123_MARTIN_123";
foreach($files as $file)
{
$input_file = $folder . $file;
$text = file_get_contents($input_file);
//$text = "Meet me at 11 o'clock behind the monument.";
echo strlen($text) . "\n";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
echo strlen($crypttext) . "\n";
file_put_contents($input_file . '_encrypted', $crypttext);
}
任何帮助将不胜感激:)
【问题讨论】:
-
我让这个问题更容易找到。希望你没事。
标签: php android encryption cryptography mcrypt