作为我的项目要求的一部分,我想用密码保护我的 NFC 标签以防止覆盖。我在互联网上进行了很多搜索,但找不到任何可靠的指南或在 Xamarin 中解释的文章。所以如果有人使用Xamarin做过NFC标签的密码保护,请指导我。
您可以在 Xamarin.Android 中使用以下步骤在 NFC NTAG215 标签中添加密码保护
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, NFCTagConfiguration
{
public Tag maintag;
protected override void OnCreate(Bundle savedInstanceState)
{
CrossCurrentActivity.Current.Init(this, savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
CrossNFC.Init(this);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
base.OnCreate(savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnResume()
{
base.OnResume();
// Plugin NFC: Restart NFC listening on resume (needed for Android 10+)
CrossNFC.OnResume();
}
protected override async void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
maintag = (Tag)intent.GetParcelableExtra(NfcAdapter.ExtraTag);
// Plugin NFC: Tag Discovery Interception
CrossNFC.OnNewIntent(intent);
if (maintag != null)
{
////Handle your logic here
//1.Check password configuration
//2.Add password protection
//3.Remove password protection
}
}
public bool CheckConfiguration()
{
bool passExist;
try
{
MifareUltralight mu = MifareUltralight.Get(maintag);
mu.Connect();
var answer = mu.ReadPages(131);
byte auth0 = answer[3];
passExist = auth0 < (byte)0xEB;
mu.Close();
}
catch (Exception e)
{
// mu.Close();
passExist = true;
}
return passExist;
}
public bool AddConfiguration(string PSWD)
{
NfcA nfcaTag = NfcA.Get(maintag);
bool IsConfigured;
try
{
byte[] SET_PACK_VALUE = { (byte)0xA2, (byte)0x86, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
byte[] PWD = Encoding.ASCII.GetBytes("1234");
byte[] SET_PASSWORD = { (byte)0xA2, (byte)0x85, PWD[0], PWD[1], PWD[2], PWD[3] };
byte[] SET_AUTH0_VALUE = { (byte)0xA2, (byte)0x83, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x04 };
string pack = Encoding.ASCII.GetString(SET_PASSWORD);
nfcaTag.Connect();
nfcaTag.Transceive(SET_PACK_VALUE);
nfcaTag.Transceive(SET_PASSWORD);
nfcaTag.Transceive(SET_AUTH0_VALUE);
nfcaTag.Close();
IsConfigured = true;
}
catch (Exception ex)
{
nfcaTag.Close();
IsConfigured = false;
}
return IsConfigured;
}
public bool RemoveConfiguration(string PSWD)
{
byte[] RESET_PACK_VALUE = { (byte)0xA2, (byte)0x86, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
byte[] RESET_AUTH0_VALUE = { (byte)0xA2, (byte)0x83, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xFF };
byte[] PWD = Encoding.ASCII.GetBytes("1234");
byte[] SET_PASSWORD = { (byte)0x1b, PWD[0], PWD[1], PWD[2], PWD[3] };
MifareUltralight mfu = MifareUltralight.Get(maintag);
bool IsConfigured;
try
{
byte[] answer;
mfu.Connect();
answer = mfu.Transceive(SET_PASSWORD);
mfu.Transceive(RESET_AUTH0_VALUE);
mfu.Transceive(RESET_PACK_VALUE);
mfu.Close();
IsConfigured = true;
}
catch (Exception e)
{
mfu.Close();
IsConfigured = false;
}
return IsConfigured;
}
}
如果您使用其他标签,您可以参考NTAG 213/215/216数据表了解APDU命令。