左晓为主开发手持机充值管理机
zuoxiao
2023-12-19 da8f72d2db0bbfc221a881d5aa31065cd5717043
app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java
@@ -1,23 +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
@@ -32,7 +22,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 +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;
    }
@@ -75,7 +66,7 @@
    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);
@@ -130,8 +121,8 @@
    /**
     * 修改密码
     *
     * @param a        书写的扇区
     *                 //     * @param callback 返回监听
     * @param a 书写的扇区
     *          //     * @param callback 返回监听
     */
    public boolean changePasword(int a) {
        MifareClassic mfc = MifareClassic.get(tag);
@@ -142,6 +133,7 @@
                int count = mfc.getSectorCount();
                boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
                //验证是否是默认密码,当默认密码时修改密码
                if (isOpen) {
                    //将密码转换为keyA