IV 用于加密转换。预期 IV 长度为 16 但实际为 24
IV for encryption conversion. Expected IV length of 16 but was 24
有人可以告诉我为什么会收到此错误消息吗?显然这是我缺少的某种转换
预期 IV 长度为 16 但实际为 24
我用
来称呼它
String encrypted = "E5ADDEB05D9D7B3925B7DE16B560D87C";
String sKey = "3985661DD71D591665BD39476636486B";
String sIv = "75E5FBB56AA78D05D246078A782553E1";
String decrypted = decrypt2(encrypted, sKey, sIv);
Log.i("--------------------------------> ", decrypted);
这是程序
public static String decrypt2(final String EncryptedMessageBase64,
final String symKeyHex,
final String sIvHex) {
final byte[] symKeyData = Base64.decode((symKeyHex),Base64.DEFAULT);
final byte[] byIvData = Base64.decode((sIvHex), Base64.DEFAULT);
final byte[] EncryptedMessage = Base64.decode(EncryptedMessageBase64, Base64.DEFAULT);
try
{
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
final int blockSize = cipher.getBlockSize();
final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES");
Log.i("### iv size -------->", String.valueOf(blockSize));
Log.i("### symKeyHex -------->", symKeyHex);
Log.i("### sIvHex -------->", sIvHex);
Log.i("### blockSize -------->", String.valueOf(blockSize));
final IvParameterSpec iv = new IvParameterSpec(byIvData);
final byte[] encryptedMessage = new byte[EncryptedMessage.length];
cipher.init(Cipher.DECRYPT_MODE, symKey, iv);
这是输出
### iv size -------->: 16
### symKeyHex -------->: 3985661DD71D591665BD39476636486B
### sIvHex -------->: 75E5FBB56AA78D05D246078A782553E1
### blockSize -------->: 16
error: expected IV length of 16 but was 24
您正在对密钥和 IV 进行 Base64 解码,但它们是十六进制编码的,您需要对二进制进行十六进制解码。
symKeyHex
和 sIvHex
是非常明显的十六进制编码值,而 EncryptedMessageBase64
显然是 Base64 编码。
根据项目中包含的库,一种可能性是:
final byte[] symKeyData = (byte[]) new Hex().decode(symKeyHex);
final byte[] byIvData = (byte[]) new Hex().decode(sIvHex);
更多:Base64编码将3个二进制字节表示为4个ASCII字符。十六进制编码将1个二进制字节表示为2个ASCII字符。
有人可以告诉我为什么会收到此错误消息吗?显然这是我缺少的某种转换
预期 IV 长度为 16 但实际为 24
我用
来称呼它String encrypted = "E5ADDEB05D9D7B3925B7DE16B560D87C";
String sKey = "3985661DD71D591665BD39476636486B";
String sIv = "75E5FBB56AA78D05D246078A782553E1";
String decrypted = decrypt2(encrypted, sKey, sIv);
Log.i("--------------------------------> ", decrypted);
这是程序
public static String decrypt2(final String EncryptedMessageBase64,
final String symKeyHex,
final String sIvHex) {
final byte[] symKeyData = Base64.decode((symKeyHex),Base64.DEFAULT);
final byte[] byIvData = Base64.decode((sIvHex), Base64.DEFAULT);
final byte[] EncryptedMessage = Base64.decode(EncryptedMessageBase64, Base64.DEFAULT);
try
{
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
final int blockSize = cipher.getBlockSize();
final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES");
Log.i("### iv size -------->", String.valueOf(blockSize));
Log.i("### symKeyHex -------->", symKeyHex);
Log.i("### sIvHex -------->", sIvHex);
Log.i("### blockSize -------->", String.valueOf(blockSize));
final IvParameterSpec iv = new IvParameterSpec(byIvData);
final byte[] encryptedMessage = new byte[EncryptedMessage.length];
cipher.init(Cipher.DECRYPT_MODE, symKey, iv);
这是输出
### iv size -------->: 16
### symKeyHex -------->: 3985661DD71D591665BD39476636486B
### sIvHex -------->: 75E5FBB56AA78D05D246078A782553E1
### blockSize -------->: 16
error: expected IV length of 16 but was 24
您正在对密钥和 IV 进行 Base64 解码,但它们是十六进制编码的,您需要对二进制进行十六进制解码。
symKeyHex
和 sIvHex
是非常明显的十六进制编码值,而 EncryptedMessageBase64
显然是 Base64 编码。
根据项目中包含的库,一种可能性是:
final byte[] symKeyData = (byte[]) new Hex().decode(symKeyHex);
final byte[] byIvData = (byte[]) new Hex().decode(sIvHex);
更多:Base64编码将3个二进制字节表示为4个ASCII字符。十六进制编码将1个二进制字节表示为2个ASCII字符。