From da8f72d2db0bbfc221a881d5aa31065cd5717043 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期二, 19 十二月 2023 14:56:01 +0800 Subject: [PATCH] 修改密码登录相关 --- app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java | 88 ++++++++++++++++++++----------------------- 1 files changed, 41 insertions(+), 47 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 856fc30..a395c28 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,13 @@ 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 @@ -29,11 +20,11 @@ private Tag tag; 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 +33,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,7 +49,7 @@ 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; @@ -73,6 +64,9 @@ * @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); @@ -93,7 +87,10 @@ 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); //鍐欏崱 @@ -124,50 +121,46 @@ /** * 淇敼瀵嗙爜 * - * @param password 涔﹀啓瀵嗙爜锛�16涓瓧鑺� - * @param a 涔﹀啓鐨勬墖鍖� - * @param callback 杩斿洖鐩戝惉 + * @param a 涔﹀啓鐨勬墖鍖� + * // * @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(); @@ -176,6 +169,7 @@ } } } + return false; } /** -- Gitblit v1.8.0