| | |
| | | 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; |
| | |
| | | private Tag tag; |
| | | |
| | | private static NFCWriteHelper helper; |
| | | private static int PASSWORD_LENTH = 6; |
| | | private static int PASSWORD_LENTH = 12; |
| | | |
| | | public NFCWriteHelper(Intent intent) { |
| | | this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | public NFCWriteHelper setReadPassword(String str) { |
| | | if (null != str && (str.length() <= PASSWORD_LENTH)) { |
| | | for (int i = 0; i < str.length(); i++) { |
| | | bytes[i] = (byte) str.charAt(i); |
| | | defauleKey[i] = (byte) str.charAt(i); |
| | | } |
| | | } |
| | | return helper; |
| | |
| | | * @param |
| | | */ |
| | | public boolean writeData(byte[] str, int a, int b) { |
| | | Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b); |
| | | //写卡时修改所有密码 |
| | | // changePasword(a); |
| | | if (str.length <= 16) { |
| | | try { |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | |
| | | return false; |
| | | } |
| | | //验证扇区密码 |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes); |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey); |
| | | if (!isOpen) { |
| | | isOpen = mfc.authenticateSectorWithKeyA(a, companyKey); |
| | | } |
| | | if (isOpen) { |
| | | int bIndex = mfc.sectorToBlock(a); |
| | | //写卡 |
| | |
| | | /** |
| | | * 修改密码 |
| | | * |
| | | * @param password 书写密码,16个字节 |
| | | * @param a 书写的扇区 |
| | | * @param callback 返回监听 |
| | | * // * @param callback 返回监听 |
| | | */ |
| | | public void changePasword(String password, int a, final NFCWriteHelper.NFCCallback callback) { |
| | | public boolean changePasword(int a) { |
| | | MifareClassic mfc = MifareClassic.get(tag); |
| | | byte[] data = new byte[16]; |
| | | if (null != mfc) { |
| | | try { |
| | | mfc.connect(); |
| | | if (password.length() != PASSWORD_LENTH) { |
| | | callback.isSusses(false); |
| | | return; |
| | | } |
| | | |
| | | int count = mfc.getSectorCount(); |
| | | if (a > count - 1 || a < 0) { |
| | | callback.isSusses(false); |
| | | return; |
| | | } |
| | | //将密码转换为keyA |
| | | for (int i = 0; i < password.length(); i++) { |
| | | data[i] = (byte) password.charAt(i); |
| | | } |
| | | //将密码转换为KeyB |
| | | for (int i = 0; i < password.length(); i++) { |
| | | data[i + password.length() + 4] = (byte) password.charAt(i); |
| | | } |
| | | //输入控制位 |
| | | data[password.length()] = (byte) 0xff; |
| | | data[password.length() + 1] = (byte) 0x07; |
| | | data[password.length() + 2] = (byte) 0x80; |
| | | data[password.length() + 3] = (byte) 0x69; |
| | | //验证密码 |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes); |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey); |
| | | |
| | | //验证是否是默认密码,当默认密码时修改密码 |
| | | if (isOpen) { |
| | | //将密码转换为keyA |
| | | byte[] dataA = HexUtil.hexToByteArray(companyKeyA); |
| | | for (int i = 0; i < dataA.length; i++) { |
| | | data[i] = dataA[i]; |
| | | } |
| | | //输入控制位 |
| | | data[6] = (byte) 0xff; |
| | | data[7] = (byte) 0x07; |
| | | data[8] = (byte) 0x80; |
| | | data[9] = (byte) 0x69; |
| | | byte[] dataB = HexUtil.hexToByteArray(companyKeyB); |
| | | //将密码转换为KeyB |
| | | for (int i = 0; i < dataB.length; i++) { |
| | | data[i + 10] = dataB[i]; |
| | | } |
| | | int bIndex = mfc.sectorToBlock(a); |
| | | int bCount = mfc.getBlockCountInSector(a); |
| | | //写到扇区的最后一个块 |
| | | mfc.writeBlock(bIndex + bCount - 1, data); |
| | | } |
| | | callback.isSusses(true); |
| | | return true; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | callback.isSusses(false); |
| | | return false; |
| | | // callback.isSusses(false); |
| | | } finally { |
| | | try { |
| | | mfc.close(); |
| | |
| | | } |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |