我使用AES加密,但是出现了意外错误,并且没有找到与此错误相关的信息。我想知道为什么会发生此错误以及如何处理。
Error occurred while Android encrypted AES (javax. crypto. IllegalBlockSizeException:
error: 1e00006a: Cipher functions: OPENSSL_internal: DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH)
无法处理次要错误
byte[] bytes = aesUtil.hexStringToBytes("8986c36eac958ec1101ae1be8a0d2d74");
byte[] decryptor = aesUtil.Decryptor(bytes);
AesUtil.java
public class AESUtil {
private byte[] keys = {
0x0f,0x15,0x71,(byte)0xc9, 0x47,(byte)0xd9,(byte)0xe8,0x59,
0x0c,(byte)0xb7,(byte)0xad,(byte)0xd6,(byte)0xaf,0x7f,0x67,(byte)0x98
};
//KeyGenerator 提供对称密钥生成器的功能,支持各种算法
private KeyGenerator keygen;
//SecretKey 负责保存对称密钥
private SecretKey deskey;
//Cipher负责完成加密或解密工作
private Cipher c;
//该字节数组负责保存加密的结果
private byte[] cipherByte;
public AESUtil() throws NoSuchAlgorithmException, NoSuchPaddingException {
//Security.addProvider(new SunJCE());
// //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
// keygen = KeyGenerator.getInstance("AES");
// //生成密钥
// deskey = keygen.generateKey();
deskey= new SecretKeySpec("8986c36eac958ec1101ae1be8a0d2d74".getBytes(), "AES");
LogUtil.e("密钥:" + new String (deskey.getEncoded()));
//System.out.println("密钥:" + UBytes.toHexString(deskey.getEncoded()));
//生成Cipher对象,指定其支持的DES算法
c = Cipher.getInstance("AES/ECB/NOPADDING"); // PKCS5Padding
}
/**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public byte[] Encrytor(String str) throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes();
//System.out.println("明文:" + UBytes.toHexString(src));
LogUtil.e("明文:" + new String(src));
// 加密,结果保存进cipherByte
cipherByte = c.doFinal(src);
//System.out.println("密文:" + UBytes.toHexString(cipherByte));
LogUtil.e("密文:" + bytesToHexString(cipherByte));
return cipherByte;
}
/**
* 对字符串解密
*
* @param buff
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(buff);
return cipherByte;
}
public byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public String printHexString( byte[] b) {
String a = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
a = a+hex;
}
return a;
}
}
无法使用密钥加密字符串