最近在用python java解密 aes密文,发现个问题
相同的密钥 相同的算法,相同的padding 居然无法解密(但是android的可以解密) ,实在找不到原因,请大牛帮忙 。类似的问题是 这些程序的加密后的结果也不同 网上找过资料 什么密钥长度问题 padding问题 这些都考虑到了 但是还是找不到原因
python代码:(报错是 M2Crypto.EVP.EVPError: bad decrypt)
from M2Crypto.EVP import Cipher
from base64 import b64encode, b64decode
from M2Crypto import util
key = 'This is the super secret key 123'
iv = '\0' * 16
ciphertext = b64decode('DTrW2VXjSoFdg0e61fHxJg==')
decipher = Cipher(alg='aes_128_cbc', key=key, op=0, iv=iv)
decipher.set_padding(padding = 1)
plaintext = decipher.update(ciphertext)
plaintext += decipher.final()
print plaintext
Java代码:(提示错误 Given final block not properly padded)
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
//return null;
}
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec("0000000000000000"
.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
public static void main(String[] args) throws Exception {
/*
* 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
* 此处使用AES-128-CBC加密模式,key需要为16位。
*/
//String cKey = "1234567890123456";
String cKey = "This is the super secret key 123";
// 解密
lStart = System.currentTimeMillis();
String enString2 = "DTrW2VXjSoFdg0e61fHxJg==";
String DeString = AESwithBASE64.Decrypt(enString2, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");
}
Android代码:
package com.android.dns.exploitaes;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
TextView textViewAESPassword;
TextView textViewPlainPassword ;
Button exploitAes_buttons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String key = "This is the super secret key 123";
final String theString="DTrW2VXjSoFdg0e61fHxJg==";
// The initialization vector used by the encryption function
final byte[] ivBytes = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
textViewAESPassword = (TextView)findViewById(R.id.textViewAESPassword);
textViewPlainPassword = (TextView)findViewById(R.id.textViewPlainPassword);
textViewAESPassword.setText(theString);
final String[] plainText = new String[1];
final byte[][] cipherData = new byte[1][1];
exploitAes_buttons = (Button) findViewById(R.id.buttonExploit);
exploitAes_buttons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
byte[] keyBytes = key.getBytes("UTF-8");
cipherData[0] = MainActivity.aes256decrypt(ivBytes, keyBytes, Base64.decode(theString.getBytes("UTF-8"), Base64.DEFAULT));
plainText[0] = new String(cipherData[0], "UTF-8");
Toast.makeText(getApplicationContext(), plainText[0], Toast.LENGTH_LONG).show();
textViewPlainPassword.setText(plainText[0]);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/*
The function that handles the aes256 decryption.
ivBytes: Initialization vector used by the decryption function
keyBytes: Key used as input by the decryption function
textBytes: Ciphertext input to the decryption function
*/
public static byte[] aes256decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
throws java.io.UnsupportedEncodingException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!