mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-06-11 02:53:23 +02:00
Added: DESFire Class V2
This commit is contained in:
45
NFC/Crypto/AES.cs
Normal file
45
NFC/Crypto/AES.cs
Normal 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
44
NFC/Crypto/CRC16.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace NFC.Crypto
|
||||
{
|
||||
|
Reference in New Issue
Block a user