全体の更新 1: 質問をもう一度参照してください。
私は最近カードで作業しています。現在、PICCのデフォルトのマスター キーDESFire
を変更することにしました。
(私はすでにマスターキーですべての 8 バイト 0x00 を正常に認証できました)
1- デフォルトmaster key
は 0 の 8byte
です00 00 00 00 00 00 00 00
。
2-master key
私が選んだ新作は 16byte
です。
それは:
byte[] newPICCKey= new byte[]{(byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
(byte)0x55, (byte)0x66, (byte)0x77 ,(byte)0x88,
(byte)0x12, (byte)0x23, (byte)0x34 ,(byte)0x45 ,
(byte)0x56, (byte)0x67, (byte)0x78 ,(byte)0x89};
3-古いマスターキーで認証します(まだ変更されておらず、8バイトのゼロです)。乱数は次のとおりです。
ランダム A = 8 バイトの数値
ランダム B = 8 バイトの数値
4 - ランダム A とランダム B を使用して毎回セッション キーを作成します。セッション キー = ランダム A の最初の 4 バイト + ランダム B の最初の 4 バイト
//fill sessionKey with RandomA and RandomB
for(int i=0; i<4; i++)
sessionKey[i] = randomA[i];
for(int i=4; i<8; i++)
sessionKey[i] = randomB[i-4];
5 - i 次の方法で新しいマスター キーの CRC16 を作成します。結果は次のとおりです: D8 EC (新しい PICC マスター キーは 16 バイトにすることができますか? それとも 8 バイトにする必要がありますか? ちなみに、新しいマスター キーに 16 バイトの値を選択しました)
public class CRC16
{
public static short Crc16(byte[] buffer, short offset, short len)
{
short crcTmp = 0x6363;
for (int i = 0; i < len; ++i)
{
short temp = (short)(buffer[offset + i] ^ crcTmp);
temp = (short)((temp ^ (temp << 4)) & 0xff);
crcTmp = (short)(((crcTmp >> 8) & 0xff) ^ (temp << 8) ^ (temp << 3) ^ (temp >> 4));
}
return crcTmp;
}
}
6 - crc とパディング (解読済み) を使用して新しい PICC キーを作成します
byte[] newPICCKey_deciphered = new byte[]{(byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
(byte)0x55, (byte)0x66, (byte)0x77 ,(byte)0x88,
(byte)0x12, (byte)0x23, (byte)0x34 ,(byte)0x45 ,
(byte)0x56, (byte)0x67, (byte)0x78 ,(byte)0x89,
(byte)0x00 , (byte)0x00,
(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,
(byte)0x00 ,(byte)0x00 ,(byte)0x00 };
txtNewPICCKeyDeciphered.setText(Utils.bytesToHex(newPICCKey_deciphered));
7- crc16 を計算し、それを新しいキーに追加します。
short res = CRC16_3.Crc16(newPICCKey, (short)0, (short)16);
newPICCKey_deciphered[16] = (byte) (res & 0xFF);
newPICCKey_deciphered[17] = (byte) ((res >> 8) & 0xFF);
8 - 上記の新しいマスター キーを以下の方法で暗号化し、24 バイトの暗号化を取得しました。
byte[] iv1=new byte[]{(byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 ,
(byte)0x00 , (byte)0x00 , (byte)0x00 , (byte)0x00 };
byte[] newPICCKeyEnciphered = new byte[24];
//..............................
byte[] block1 = new byte[]{(byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
(byte)0x55, (byte)0x66, (byte)0x77 ,(byte)0x88};
byte[] block2 = new byte[]{
(byte)0x11, (byte)0x22, (byte)0x33 ,(byte)0x44 ,
(byte)0x55, (byte)0x66, (byte)0x77 ,(byte)0x88};
byte[] block3 = new byte[]{(byte)0x00 , (byte)0x00,
(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,
(byte)0x00 ,(byte)0x00 ,(byte)0x00};
block3[0] = newPICCKey_deciphered[16];
block3[1] = newPICCKey_deciphered[17];
try
{
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
SecretKeyFactory desKeyFact = SecretKeyFactory.getInstance("DES");
DESKeySpec desKeySpec = new DESKeySpec(sessionKey);
SecretKey s = desKeyFact.generateSecret(desKeySpec);
cipher.init(Cipher.DECRYPT_MODE, s);
byte[] r1 = new byte[8];
r1 =Utils.doXorTwoByteArray(block1, iv1);
byte[] r2 = new byte[8];
r2 = cipher.doFinal(r1, 0, 8);
//...............
byte[] r3 = new byte[8];
r3 =Utils.doXorTwoByteArray(block2, r2);
byte[] r4 = new byte[8];
r4 =cipher.doFinal(r3, 0, 8);
//................
byte[] r5 = new byte[8];
r5 =Utils.doXorTwoByteArray(block3, r4);
byte[] r6 = new byte[8];
r6 =cipher.doFinal(r5, 0, 8);
for(int i=0; i<8;i++)
newPICCKeyEnciphered[i] = r2[i];
for(int i=8; i<16;i++)
newPICCKeyEnciphered[i] = r4[i-8];
for(int i=16; i<24;i++)
newPICCKeyEnciphered[i] = r6[i-16];
}
catch(Exception e)
{
e.printStackTrace();
}
暗号化で使用されるクラス DES は次のとおりです。
public class DES {
public static byte[] doDecryptData(byte[] OriginalData,byte[]key , int sizeKey , byte[] iv , int sizeIV)
{
byte[] masterKeyBytes =new byte[sizeKey];
masterKeyBytes = key;
byte[] ivBytes = new byte[sizeIV];
ivBytes = iv;
byte[] encipheredData=new byte[sizeIV];
try{
DESKeySpec desKeySpec = new DESKeySpec(masterKeyBytes);
SecretKeyFactory desKeyFact = SecretKeyFactory.getInstance("DES");
SecretKey s = desKeyFact.generateSecret(desKeySpec);
Cipher aliceCipher = Cipher.getInstance("DES/CBC/NoPadding");
aliceCipher.init(Cipher.DECRYPT_MODE, s, new IvParameterSpec(ivBytes));
encipheredData= aliceCipher.doFinal(OriginalData);
return encipheredData;
}
catch(Exception e)
{
Log.e("error", "111"+e.toString());
}
return null;
}
9- そして最後に、パラメータ バイト配列を入力し、命令 (c4) と共に送信します。
byte[] cmd = new byte[]{(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 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00
,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00
,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 };
//fill cmd
for(int i=1 ;i<cmd.length ; i++)
cmd[i] = newPICCKeyEnciphered[i -1];
try {
responseChangeKey = isodep.transceive(Utils.wrapMessage((byte)0xC4, cmd));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
そして、ここに Utils クラスがあります:
public class Utils {
public static byte[] wrapMessage (byte command, byte[] parameters) throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.write((byte) 0x90);
stream.write(command);
stream.write((byte) 0x00);
stream.write((byte) 0x00);
if (parameters != null) {
stream.write((byte) parameters.length);
stream.write(parameters);
}
stream.write((byte) 0x00);
byte[] b = stream.toByteArray();
return b;
}
}
最後のステップ (マスター キーをカードに変更するための apdu の送信) で、例外 0x1E エラーを受け取りました。これは、完全性エラーを意味します:CRC または MAC がデータと一致しません。キーの変更を正しく実行するにはどうすればよいですか? それは私にとって必要です。ありがとう。