| | |
| | | package com.dayu.recharge.tools; |
| | | |
| | | import android.app.Activity; |
| | | import android.content.Intent; |
| | | import android.nfc.NfcAdapter; |
| | | import android.nfc.Tag; |
| | | import android.nfc.tech.MifareClassic; |
| | | import android.util.Base64; |
| | | import android.util.Log; |
| | | |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | |
| | | import javax.crypto.BadPaddingException; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.IllegalBlockSizeException; |
| | | import javax.crypto.NoSuchPaddingException; |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | /** |
| | | * @author zx |
| | |
| | | private static NFCWriteHelper helper; |
| | | private static int PASSWORD_LENTH = 12; |
| | | |
| | | public NFCWriteHelper(Intent intent) { |
| | | public NFCWriteHelper(Intent intent, Activity activity) { |
| | | super(activity); |
| | | this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
| | | } |
| | | |
| | |
| | | * @param intent |
| | | * @return |
| | | */ |
| | | public static NFCWriteHelper getInstence(Intent intent) { |
| | | public static NFCWriteHelper getInstence(Intent intent, Activity activity) { |
| | | if (helper == null) { |
| | | helper = new NFCWriteHelper(intent); |
| | | helper = new NFCWriteHelper(intent, activity); |
| | | } |
| | | return helper; |
| | | } |
| | |
| | | public boolean writeData(byte[] str, int a, int b) { |
| | | Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b); |
| | | //写卡时修改所有密码 |
| | | // changePasword(a); |
| | | changePasword(a); |
| | | if (str.length <= 16) { |
| | | try { |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | |
| | | /** |
| | | * 修改密码 |
| | | * |
| | | * @param a 书写的扇区 |
| | | * // * @param callback 返回监听 |
| | | * @param a 书写的扇区 |
| | | * // * @param callback 返回监听 |
| | | */ |
| | | public boolean changePasword(int a) { |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | |
| | | data[i] = dataA[i]; |
| | | } |
| | | //输入控制位 |
| | | data[6] = (byte) 0xff; |
| | | data[6] = (byte) 0xFF; |
| | | data[7] = (byte) 0x07; |
| | | data[8] = (byte) 0x80; |
| | | data[9] = (byte) 0x69; |
| | |
| | | int bCount = mfc.getBlockCountInSector(a); |
| | | //写到扇区的最后一个块 |
| | | mfc.writeBlock(bIndex + bCount - 1, data); |
| | | // byte[] dataa = mfc.readBlock(bIndex + bCount - 1); |
| | | // // 修改密码 A |
| | | // mfc.writeBlock(mfc.sectorToBlock(a) + 3, dataA); |
| | | // // 修改密码 B |
| | | // mfc.writeBlock(mfc.sectorToBlock(a) + 7, dataB); |
| | | } |
| | | return true; |
| | | } catch (Exception e) { |