Added: DESFire Class V2

This commit is contained in:
TheJoKlLa
2020-10-07 19:25:51 +02:00
parent f5dffcec5b
commit 7381341d37
24 changed files with 1806 additions and 80 deletions

45
NFC/Crypto/AES.cs Normal file
View File

@ -0,0 +1,45 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
namespace NFC.Crypto
{
public class AES
{
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
{
AesEngine engine = new AesEngine();
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
KeyParameter keyParam = new KeyParameter(key);
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
// Encrypt
cipher.Init(true, keyParamWithIV);
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
int length = cipher.ProcessBytes(data, outputBytes, 0);
cipher.DoFinal(outputBytes, length);
return outputBytes;
}
public byte[] Decrypt(byte[] data, byte[] key, byte[] iv)
{
AesEngine engine = new AesEngine();
CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
BufferedBlockCipher cipher = new BufferedBlockCipher(blockCipher);
KeyParameter keyParam = new KeyParameter(key);
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);
// Decrypt
cipher.Init(false, keyParamWithIV);
byte[] outputBytes = new byte[cipher.GetOutputSize(data.Length)];
int length = cipher.ProcessBytes(data, outputBytes, 0);
cipher.DoFinal(outputBytes, length);
return outputBytes;
}
}
}

44
NFC/Crypto/CRC16.cs Normal file
View File

@ -0,0 +1,44 @@
using System;
namespace NFC.Crypto
{
public class CRC16
{
public byte[] Calculate(byte[] data)
{
UInt16 crc16 = 0x6363;
crc16 = Calculate(data, crc16);
return BitConverter.GetBytes(crc16);
}
public byte[] Calculate(byte[] cmd, byte[] data)
{
UInt16 crc16 = 0x6363;
crc16 = Calculate(cmd, crc16);
crc16 = Calculate(data, crc16);
return BitConverter.GetBytes(crc16);
}
public UInt16 Calculate(byte[] data, UInt16 crc16)
{
for (int i = 0; i < data.Length; i++)
{
crc16 ^= data[i];
for (int b = 0; b < 8; b++)
{
bool b_Bit = (crc16 & 0x01) > 0;
crc16 >>= 1;
if (b_Bit)
{
crc16 ^= 0x8408;
}
}
}
return crc16;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Linq;
namespace NFC.Crypto
{