From fe79cc8b05637b7dd3fc8f4c37a54e5f85da1efc Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期三, 27 三月 2024 17:35:27 +0800
Subject: [PATCH] 优化卡标识设置界面提示
---
app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java | 204 +++++++++++++++++++++++++++++++++------------------
1 files changed, 132 insertions(+), 72 deletions(-)
diff --git a/app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java b/app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java
index 449ebe7..a1923c1 100644
--- a/app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java
+++ b/app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java
@@ -1,22 +1,15 @@
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 com.dayu.recharge.card.UserCard;
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
@@ -24,16 +17,16 @@
* email 1058083107@qq.com
* description
*/
-public class NFCWriteHelper extends BaseNFCHelper{
+public class NFCWriteHelper extends BaseNFCHelper {
private Tag tag;
- private NFCWriteHelper.NFCCallback callback;
+
private static NFCWriteHelper helper;
- private static int PASSWORD_LENTH = 6;
+ 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);
-
}
/**
@@ -42,9 +35,9 @@
* @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;
}
@@ -58,21 +51,83 @@
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 str 涔﹀啓鍐呭锛�16涓瓧鑺�
- * @param a 涔﹀啓鐨勬墖鍖� (浠�0寮�濮嬫暟)
- * @param b 涔﹀啓鐨勫潡(浠�0寮�濮嬫暟)
- * @param callback 杩斿洖鐩戝惉
+ * @param userCard 鐢ㄦ埛鍗″唴瀹�
+ * @param
*/
- public void writeData(byte[] str, int a, int b, NFCWriteHelper.NFCCallback callback) {
+ public boolean writeUserData(UserCard userCard) {
+ if (userCard != null) {
+ int a = 1;
+ try {
+ MifareClassic mfc = MifareClassic.get(tag);
+ if (null != mfc) {
+ try {
+ //杩炴帴NFC
+ mfc.connect();
+ //楠岃瘉鎵囧尯瀵嗙爜
+ boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
+ if (!isOpen) {
+ isOpen = mfc.authenticateSectorWithKeyA(a, companyKey);
+ } else {
+ //鍐欏崱鏃朵慨鏀规墍鏈夊瘑鐮�
+ changePasword(a, mfc);
+ }
+ if (isOpen) {
+ for (int b = 0; b < 3; b++) {
+ byte[] data;
+ if (b == 0) {
+ data = userCard.getZeroBytes();
+ } else if (b == 1) {
+ data = userCard.getOneBytes();
+ } else {
+ data = userCard.getTwoBytes();
+ }
+ int bIndex = mfc.sectorToBlock(a);
+ //鍐欏崱
+ mfc.writeBlock(bIndex + b, data);
+ }
+ return true;
+ }
+ return false;
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ } finally {
+ try {
+ mfc.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ return false;
+ }
+
+
+ /**
+ * 鍐欏崱
+ *
+ * @param str 涔﹀啓鍐呭锛�16涓瓧鑺�
+ * @param a 涔﹀啓鐨勬墖鍖� (浠�0寮�濮嬫暟)
+ * @param b 涔﹀啓鐨勫潡(浠�0寮�濮嬫暟)
+ * @param
+ */
+ public boolean writeData(byte[] str, int a, int b) {
+ Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b);
if (str.length <= 16) {
try {
MifareClassic mfc = MifareClassic.get(tag);
@@ -84,29 +139,32 @@
int count = mfc.getSectorCount();
//濡傛灉浼犺繘鏉ョ殑鎵囧尯澶т簡鎴栬�呭皬浜嗙洿鎺ラ��鍑烘柟娉�
if (a > count - 1 || a < 0) {
- callback.isSusses(false);
- return;
+ return false;
}
//鑾峰彇鍐欑殑鎵囧尯鐨勫潡鐨勬暟閲�
int bCount = mfc.getBlockCountInSector(a);
//濡傛灉杈撳叆鐨勫潡澶т簡鎴栬�呭皬浜嗕篃鏄洿鎺ラ��鍑�
if (b > bCount - 1 || b < 0) {
- callback.isSusses(false);
- return;
+ return false;
}
//楠岃瘉鎵囧尯瀵嗙爜
- boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes);
+ boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
+ if (!isOpen) {
+ isOpen = mfc.authenticateSectorWithKeyA(a, companyKey);
+ } else {
+ //鍐欏崱鏃朵慨鏀规墍鏈夊瘑鐮�
+ changePasword(a, mfc);
+ }
if (isOpen) {
int bIndex = mfc.sectorToBlock(a);
//鍐欏崱
mfc.writeBlock(bIndex + b, str);
- callback.isSusses(true);
- return;
+ return true;
}
- callback.isSusses(false);
+ return false;
} catch (Exception e) {
e.printStackTrace();
- callback.isSusses(false);
+ return false;
} finally {
try {
mfc.close();
@@ -117,66 +175,68 @@
}
} catch (Exception e) {
e.printStackTrace();
+ return false;
}
}
+ return false;
}
/**
* 淇敼瀵嗙爜
*
- * @param password 涔﹀啓瀵嗙爜锛�16涓瓧鑺�
- * @param a 涔﹀啓鐨勬墖鍖�
- * @param callback 杩斿洖鐩戝惉
+ * @param a 涔﹀啓鐨勬墖鍖�
+ * // * @param callback 杩斿洖鐩戝惉
*/
- public void changePasword(String password, int a, final NFCWriteHelper.NFCCallback callback) {
- MifareClassic mfc = MifareClassic.get(tag);
+ public boolean changePasword(int a, MifareClassic mfc) {
+// 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;
- }
+// mfc.connect();
+// int count = mfc.getSectorCount();
+// boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
+// //楠岃瘉鏄惁鏄粯璁ゅ瘑鐮侊紝褰撻粯璁ゅ瘑鐮佹椂淇敼瀵嗙爜
+// if (isOpen) {
//灏嗗瘑鐮佽浆鎹负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);
+ byte[] dataA = HexUtil.hexToByteArray(companyKeyA);
+ for (int i = 0; i < dataA.length; i++) {
+ data[i] = dataA[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);
- if (isOpen) {
- int bIndex = mfc.sectorToBlock(a);
- int bCount = mfc.getBlockCountInSector(a);
- //鍐欏埌鎵囧尯鐨勬渶鍚庝竴涓潡
- mfc.writeBlock(bIndex + bCount - 1, data);
+ 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];
}
- callback.isSusses(true);
+ int bIndex = mfc.sectorToBlock(a);
+ 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) {
e.printStackTrace();
- callback.isSusses(false);
+ return false;
+// callback.isSusses(false);
} finally {
- try {
- mfc.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
+// try {
+// mfc.close();
+// } catch (IOException e) {
+// e.printStackTrace();
+// }
}
}
+ return false;
}
/**
--
Gitblit v1.8.0