android关于IC卡读写及加密解密

                  

byte[] key = {(byte) 0xA0, (byte) 0xB7, (byte) 0xA5, (byte) 0xC5, (byte) 0x80, (byte) 0x88};
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//加密IC卡
MifareClassic mifareClassic = MifareClassic.get(tag);
mifareClassic.connect();
//获取扇区数量
int count = mifareClassic.getSectorCount();
//判断是否为默认密码
boolean isOpen = mifareClassic.authenticateSectorWithKeyA(1, MifareClassic.KEY_DEFAULT);
if (isOpen) {
    //加密
    int bCount = mifareClassic.getBlockCountInSector(1);
    int bIndex = mifareClassic.sectorToBlock(1);
    for (int j = 0; j < bCount; j++) {
        //修改KeyA和KeyB
        if ((bIndex + j) == 7) {
            mifareClassic.writeBlock(bIndex + j, new byte[]{(byte) 0xa0, (byte) 0xb7, (byte) 0xa5, (byte) 0xc5, (byte) 0x80, (byte) 0x88, (byte) 0xff, 0x07, (byte) 0x80, (byte) 0x69, (byte) 0xa0, (byte) 0xb7, (byte) 0xa5, (byte) 0xc5, (byte) 0x80, (byte) 0x88});

        }
    }
} else {
    //解密
    boolean isPassword = mifareClassic.authenticateSectorWithKeyA(1, key);
    if (isPassword) {
        Log.e("onNewIntent:", "密码正确");

    }

猜你喜欢

转载自blog.csdn.net/androidisgod/article/details/103944180