python和android环境下AES加密解密算法

python和android环境下AES加密解密算法,android,python,encryption,encoding,aes,Android,Python,Encryption,Encoding,Aes,我有用于AES加密的python和android代码。当我在android中加密文本时,它在python上成功解密,但在android端无法解密。有人有主意吗 Python代码: 导入base64 导入hashlib 从加密导入随机 从Crypto.Cipher导入AES 类别密码: def uuu init uuuu(self,key): self.bs=16 self.key=hashlib.sha256(key.encode()).digest() def加密(自我、消息): 消息=自身。

我有用于AES加密的python和android代码。当我在android中加密文本时,它在python上成功解密,但在android端无法解密。有人有主意吗

Python代码:

导入base64
导入hashlib
从加密导入随机
从Crypto.Cipher导入AES
类别密码:
def uuu init uuuu(self,key):
self.bs=16
self.key=hashlib.sha256(key.encode()).digest()
def加密(自我、消息):
消息=自身。\u pad(消息)
iv=随机.new().read(AES.块大小)
密码=AES.new(self.key,AES.MODE_CBC,iv)
返回base64.b64编码(iv+密码.加密(消息)).decode('utf-8'))
def解密(自我,加密):
enc=base64.b64解码(enc)
iv=附件[:AES.块大小]
密码=AES.new(self.key,AES.MODE_CBC,iv)
返回self.\u unpad(cipher.decrypt(enc[AES.block\u size:])).decode('utf-8')
def_垫(自身):
返回s+(self.bs-len(s)%self.bs)*chr(self.bs-len(s)%self.bs)
@静力学方法
def_unpad(s):
返回s[:-ord(s[len(s)-1:])]
Android代码:

import java.io.IOException;
导入java.security.invalidalgorithParameterException;
导入java.security.InvalidKeyException;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
导入javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.SecretKey;
导入javax.crypto.SecretKeyFactory;
导入javax.crypto.spec.IvParameterSpec;
导入javax.crypto.spec.PBEKeySpec;
导入javax.crypto.spec.SecretKeySpec;
导入java.security.SecureRandom;
导入java.security.spec.AlgorithmParameterSpec;
导入java.security.spec.InvalidKeySpecException;
导入java.security.spec.KeySpec;
导入java.util.array;
导入android.annotation.SuppressLint;
导入android.location.Criteria;
导入android.util.Base64;
导入android.util.Log;
@SuppressLint(“新API”)
公共类密码{
私有静态最终字符串标记=Crypt.class.getSimpleName();
私有静态最终字符串字符编码=“UTF-8”;
私有静态最终字符串密码转换=“AES/CBC/PKCS5Padding”;
私有静态最终字符串aesEncryptionAlgorithm=“AES”;
private static final String key=“这是我的密钥”;
私有静态字节[]ivBytes={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
私有静态字节[]密钥字节;
私有静态Crypt实例=null;
地穴()
{
SecureRandom=新的SecureRandom();
Crypt.ivBytes=新字节[16];
random.nextBytes(Crypt.ivBytes);
}
公共静态Crypt getInstance(){
if(实例==null){
instance=newcrypt();
}
返回实例;
}
public String encrypt_String(最终字符串纯文本)抛出InvalidKeyException、NoSuchAlgorithmException、NoSuchPaddingException、InvalidAlgorithmParameterException、IllegalBlockSizeException、BadPaddingException、IOException
{
返回Base64.encodeToString(加密(plain.getBytes()),Base64.DEFAULT);
}
public String decrypt_String(最终字符串普通)抛出InvalidKeyException、NoSuchAlgorithmException、NoSuchPaddingException、InvalidAlgorithmParameterException、IllegalBlockSizeException、BadPaddingException、ClassNotFoundException、IOException
{
byte[]encryptedBytes=解密(Base64.decode(普通,0));
返回Base64.encodeToString(encryptedBytes,Base64.DEFAULT);
}
公共字节[]加密(字节[]mes)
抛出NoSuchAlgorithmException,
没有这样的例外,
InvalidKeyException,
无效算法参数异常,
非法区块大小例外,
BadPaddingException,IOException{
keyBytes=key.getBytes(“UTF-8”);
Log.d(标签,“长键:”+keyBytes.length);
MessageDigest md=MessageDigest.getInstance(“SHA-256”);
md.update(keyBytes);
keyBytes=md.digest();
Log.d(标签,“长键:”+keyBytes.length);
AlgorithmParameterSpec ivSpec=新的IvParameterSpec(ivBytes);
SecretKeySpec newKey=新SecretKeySpec(keyBytes,AESEencryptionalGorithm);
密码=空;
cipher=cipher.getInstance(cipherTransformation);
SecureRandom=新的SecureRandom();
Crypt.ivBytes=新字节[16];
random.nextBytes(Crypt.ivBytes);
cipher.init(cipher.ENCRYPT_模式,newKey,random);
//cipher.init(cipher.ENCRYPT_模式,newKey,ivSpec);
字节[]目的地=新字节[ivBytes.length+mes.length];
数组复制(ivBytes,0,destination,0,ivBytes.length);
System.arraycopy(mes,0,目的地,ivBytes.length,mes.length);
返回cipher.doFinal(目的地);
}
公共字节[]解密(字节[]字节)
抛出NoSuchAlgorithmException,
没有这样的例外,
InvalidKeyException,
无效算法参数异常,
非法区块大小例外,
BadPaddingException、IOException、ClassNotFoundException{
keyBytes=key.getBytes(“UTF-8”);
Log.d(标签,“长键:”+keyBytes.length);
MessageDigest md=MessageDigest.getInstance(“SHA-256”);
md.update(keyBytes);
keyBytes=md.digest();
Log.d(标签,“长键:”+keyBytes.length);
byte[]ivB=Arrays.copyOfRange(字节,0,16);
Log.d(标记“IV:+新字符串(ivB));
byte[]codB=Arrays.copyOfRange(bytes,16,bytes.length);
AlgorithmParameterSpec ivSpec=新的IvParameterSpec(ivB);
SecretKeySpec newKey=新SecretKeySpec(keyBytes,AESEencryptionalGorithm);
Cipher Cipher=Cipher.getInstance(cipherTransformation);
cipher.init(cipher.DECRYPT_模式,newKey,ivSpec);
字节[]res=cipher.doFinal(codB);
返回res;
}
}
当我在android上运行此代码时:

String str = "this is local test";
Log.i("myTag", "step1: " + str);
String a = aesCrypt.encrypt_string(str);
Log.i("myTag", "step2: " + a);
String b = aesCrypt.decrypt_string(a);
Log.i("myTag", "step3: " + b);
然后我得到了这个答案:

step1: this is local test
step2: a0F8MhzkSpRlM+aM1MKzUdVCoXIE5y5hh4PRuwPfAhofKwLJjTUbBvmJzTsKJDqF
step3: dGhpcyBpcyBsb2NhbCB0ZXN0

有人知道为什么会这样吗?

您正在对解密后的输出进行编码

公共字符串解密\u字符串(
return new String(encryptedBytes);