有人知道如何解密卡片发送的第一条消息吗? 我的意思是在身份验证成功后,您发送一个命令(例如0x51(GetRealTagUID)。它返回00 +随机32位数(始终不同)。我尝试使用以下方法进行解密:
private byte[] decrypt(byte[] raw, byte[] encrypted, byte[] iv)
throws Exception {
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
使用decrypt(sessionKey, response, iv)进行调用。
IV = 全0(16字节)
response = 在0x51命令后的32个随机比特数(去除两个零)
有人告诉我,在第一个发送的命令(0x51)之后,IV会发生变化。如何生成正确的IV以解密响应?我认为全0是错误的,因为解密后的消息总是不同的,而且应该与相同的卡片相同。
-编辑-
按照Michael Roland的说明操作后,解密响应仍然只是随机位。这是我的代码(我认为我做错了一些事情):
byte[] x = encrypt(sessionKey, iv, iv);
byte[] rx = rotateBitsLeft(x);
if ((rx[15] & 0x01) == 0x01)
rx[15] = (byte) (rx[15] ^ 0x87);
if ((rx[15] & 0x01) == 0x00)
rx[15] = (byte) (rx[15] ^ 0x01);
byte[] crc_k1 = rx;
byte[] rrx = rotateBitsLeft(rx);
if ((rrx[15] & 0x01) == 0x01)
rrx[15] = (byte) (rrx[15] ^ 0x87);
if ((rrx[15] & 0x01) == 0x00)
rrx[15] = (byte) (rrx[15] ^ 0x01);
byte[] crc_k2 = rrx;
byte[] command = { (byte) 0x51, (byte) 0x80, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00 };
for (int i = 0; i < 16; i++){
command[i] = (byte) (command[i] ^ crc_k2[i]);
}
byte[] iv2 = encrypt(sessionKey, command, iv);
byte[] RealUID = decrypt(sessionKey, ReadDataParsed, iv2);
Log.e("RealUID", ByteArrayToHexString(RealUID));
-编辑3-
仍然返回不同的值。我认为问题可能出在这里:
byte[] iv2 = encrypt(sessionKey, command, iv);
在创建新的IV以解密响应时,使用什么IV?它都是零。