我已经成功地使用 javax.crypto.Cipher.getInstance("DESede/CBC/NoPadding") 在 Android 上使用 DESFire 卡进行身份验证(按照此处的示例:https ://stackoverflow.com/a/14160507/2095694 )。它一直在从 Android 4 到 5 的几台设备上运行,但在我更新到 6 Marshmallow(和 6.0.1)的 Nexus 7 上停止工作。在更新之前,它一直在同一台设备上工作。
似乎 Cipher 的工作方式不同,对相同的密钥和数据给出不同的结果。运行以下代码...
public static void testCipher() throws Exception
{
byte[] KEY =
new byte[]{
(byte) 0x0C, (byte) 0x09, (byte) 0x03, (byte) 0x0E,
(byte) 0x05, (byte) 0x0A, (byte) 0x0D, (byte) 0x02,
(byte) 0x03, (byte) 0x0A, (byte) 0x09, (byte) 0x0B,
(byte) 0x06, (byte) 0x10, (byte) 0x04, (byte) 0x10
};
byte[] DATA =
new byte[]{
(byte) 0x29, (byte) 0xDA, (byte) 0xC0, (byte) 0xC4,
(byte) 0xB8, (byte) 0x47, (byte) 0x13, (byte) 0xA2};
byte[] newByte8 = new byte[8]; //Zeroes
android.util.Log.d("TEST", "KEY : " + bin2hex(KEY));
android.util.Log.d("TEST", "DATA: " + bin2hex(DATA));
android.util.Log.d("TEST", "IVPS: " + bin2hex(newByte8));
android.util.Log.d("TEST", "----");
javax.crypto.Cipher cipher =
javax.crypto.Cipher.getInstance("DESede/CBC/NoPadding");
cipher.init(
Cipher.DECRYPT_MODE,
new javax.crypto.spec.SecretKeySpec(KEY, "DESede"),
new javax.crypto.spec.IvParameterSpec(newByte8));
byte[] result = cipher.doFinal(DATA);
android.util.Log.d("TEST", "RSLT: " + bin2hex(result));
}
public static String bin2hex(byte[] data) {
return String.format("%0" + (data.length * 2) + "X", new java.math.BigInteger(1, data));
}
...给我以下输出:
KEY : 0C09030E050A0D02030A090B06100410
DATA: 29DAC0C4B84713A2
IVPS: 0000000000000000
----
RSLT: 47BC415065B8155E
正常价值,应该是什么,总是有效的,并且卡最终可以正确验证,所以它按照卡的预期方式进行。如前所述,我在几种设备(Android 4 和 5)上进行了尝试,它们给出了相同的结果。
但是现在在我的 Nexus 7 上使用 Marshmallow 我得到了其他东西(并且身份验证最终失败)
RSLT: F3ADA5969FA9369C
图书馆有什么变化吗?