From b5b0bd260898f816f171aec59f20098fe67eaffe Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期三, 27 三月 2024 15:46:46 +0800
Subject: [PATCH] IP设置界面调整
---
app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java | 160 ++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 109 insertions(+), 51 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 120c3eb..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,23 +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 java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
+import com.dayu.recharge.card.UserCard;
-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;
+import java.io.IOException;
/**
* @author zx
@@ -32,7 +24,8 @@
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);
}
@@ -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;
}
@@ -64,6 +57,67 @@
return helper;
}
+
+ /**
+ * 鍐欏崱
+ *
+ * @param userCard 鐢ㄦ埛鍗″唴瀹�
+ * @param
+ */
+ 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;
+ }
+
+
/**
* 鍐欏崱
*
@@ -74,8 +128,6 @@
*/
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);
@@ -99,6 +151,9 @@
boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
if (!isOpen) {
isOpen = mfc.authenticateSectorWithKeyA(a, companyKey);
+ } else {
+ //鍐欏崱鏃朵慨鏀规墍鏈夊瘑鐮�
+ changePasword(a, mfc);
}
if (isOpen) {
int bIndex = mfc.sectorToBlock(a);
@@ -130,52 +185,55 @@
/**
* 淇敼瀵嗙爜
*
- * @param a 涔﹀啓鐨勬墖鍖�
- * // * @param callback 杩斿洖鐩戝惉
+ * @param a 涔﹀啓鐨勬墖鍖�
+ * // * @param callback 杩斿洖鐩戝惉
*/
- public boolean changePasword(int a) {
- 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();
-
- int count = mfc.getSectorCount();
- 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);
+// mfc.connect();
+// int count = mfc.getSectorCount();
+// 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);
+// 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();
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