mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-04-20 02:16:30 +02:00
BackUp DES
This commit is contained in:
parent
b9708da234
commit
c943a51d9c
45
NFC/Crypto/DES.cs
Normal file
45
NFC/Crypto/DES.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 DES
|
||||
{
|
||||
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEngine();
|
||||
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)
|
||||
{
|
||||
DesEngine engine = new DesEngine();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
45
NFC/Crypto/TripleDES.cs
Normal file
45
NFC/Crypto/TripleDES.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 TripleDES
|
||||
{
|
||||
public byte[] Encrypt(byte[] data, byte[] key, byte[] iv)
|
||||
{
|
||||
DesEngine engine = new DesEdeEngine();
|
||||
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)
|
||||
{
|
||||
DesEngine engine = new DesEdeEngine();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +1,28 @@
|
||||
using NFC.Mifare_DESFire.Enums;
|
||||
using NFC.Crypto;
|
||||
using NFC.Mifare_DESFire.Enums;
|
||||
using PCSC.Iso7816;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
|
||||
namespace NFC.Mifare_DESFire
|
||||
{
|
||||
|
||||
public class MifareDESFire
|
||||
{
|
||||
public byte[] GenerateDefaultKey(int size)
|
||||
{
|
||||
List<byte> key = new List<byte>();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
key.Add(0);
|
||||
}
|
||||
|
||||
return key.ToArray();
|
||||
}
|
||||
|
||||
private ICard _Card;
|
||||
|
||||
public MifareDESFire(ICard card)
|
||||
@ -59,9 +74,113 @@ namespace NFC.Mifare_DESFire
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Authenticate(int v, byte[] aPP_MasterKey)
|
||||
/// <summary>
|
||||
/// Authenticate to Card
|
||||
/// </summary>
|
||||
/// <param name="key_id">0x01 - 0x0D</param>
|
||||
/// <param name="key"></param>
|
||||
public void Authenticate(byte key_id, byte[] key)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
APDUCommand cmd_getchallange = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)0x1A,
|
||||
Data = new byte[]
|
||||
{
|
||||
key_id
|
||||
}
|
||||
};
|
||||
APDUResponse response = _Card.Transmit(cmd_getchallange);
|
||||
|
||||
byte[] challenge = response.Body;
|
||||
Console.WriteLine("Challange: {0}", toHexString(challenge));
|
||||
|
||||
byte[] rndA = new byte[]
|
||||
{
|
||||
0x92, 0x31, 0x34, 0x8B, 0x66, 0x35, 0xA8, 0xAF
|
||||
};
|
||||
Console.WriteLine("rndA: {0}", toHexString(rndA));
|
||||
|
||||
TripleDES des = new TripleDES();
|
||||
byte[] rndB = des.Decrypt(challenge, key, GenerateDefaultKey(8));
|
||||
Console.WriteLine("rndB: {0}", toHexString(rndB));
|
||||
|
||||
byte[] leftRotatedRndB = rotateLeft(rndB);
|
||||
Console.WriteLine("leftRotatedRndB: {0}", toHexString(leftRotatedRndB));
|
||||
|
||||
byte[] rndA_rndB = concatenate(rndA, leftRotatedRndB);
|
||||
Console.WriteLine("rndA_rndB: {0}", toHexString(rndA_rndB));
|
||||
|
||||
byte[] challengeAnswer = des.Encrypt(rndA_rndB, key, GenerateDefaultKey(8));
|
||||
Console.WriteLine("challengeAnswer: {0}", toHexString(challengeAnswer));
|
||||
|
||||
APDUCommand cmd_answerchallange = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)0xAF,
|
||||
Data = challengeAnswer
|
||||
};
|
||||
Console.WriteLine("cmd_answerchallange: {0}", toHexString(cmd_answerchallange.ToArray()));
|
||||
|
||||
response = _Card.Transmit(cmd_answerchallange);
|
||||
|
||||
byte[] encryptedRndAFromCard = response.Body;
|
||||
Console.WriteLine("encryptedRndAFromCard: {0}", toHexString(encryptedRndAFromCard));
|
||||
|
||||
byte[] rotatedRndAFromCard = des.Decrypt(encryptedRndAFromCard, key, GenerateDefaultKey(8));
|
||||
Console.WriteLine("rotatedRndAFromCard: {0}", toHexString(rotatedRndAFromCard));
|
||||
|
||||
byte[] rndAFromCard = rotateRight(rotatedRndAFromCard);
|
||||
Console.WriteLine("rndAFromCard: {0}", toHexString(rndAFromCard));
|
||||
|
||||
if (!rndA.Equals(rndAFromCard))
|
||||
{
|
||||
throw new Exception("???");
|
||||
}
|
||||
}
|
||||
|
||||
private String toHexString(byte[] data)
|
||||
{
|
||||
return BitConverter.ToString(data).Replace("-", string.Empty);
|
||||
}
|
||||
|
||||
public byte[] rotateLeft(byte[] data)
|
||||
{
|
||||
byte[] rotate = new byte[data.Length];
|
||||
data.CopyTo(rotate, 0);
|
||||
|
||||
byte temp = rotate[0];
|
||||
for (var i = 0; i < rotate.Length - 1; i++)
|
||||
{
|
||||
rotate[i] = rotate[i + 1];
|
||||
}
|
||||
rotate[rotate.Length - 1] = temp;
|
||||
|
||||
return rotate;
|
||||
}
|
||||
|
||||
public byte[] rotateRight(byte[] data)
|
||||
{
|
||||
byte[] rotate = new byte[data.Length];
|
||||
data.CopyTo(rotate, 0);
|
||||
|
||||
byte temp = rotate[rotate.Length - 1];
|
||||
for (var i = rotate.Length - 1; i > 1; i--)
|
||||
{
|
||||
rotate[i] = rotate[i - 1];
|
||||
}
|
||||
rotate[0] = temp;
|
||||
|
||||
return rotate;
|
||||
}
|
||||
|
||||
public byte[] concatenate(byte[] a, byte[] b)
|
||||
{
|
||||
byte[] c = new byte[a.Length + b.Length];
|
||||
a.CopyTo(c, 0);
|
||||
b.CopyTo(c, a.Length);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -148,7 +267,7 @@ namespace NFC.Mifare_DESFire
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public void CreateFile(byte fabAccessIdentFileID, FileCommunication pLAIN, ushort fileAccessRight, int v)
|
||||
public void CreateFile(byte fabAccessIdentFileID, FileCommunication pLAIN, ushort fileAccessRight, UInt32 v)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
@ -7,5 +7,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="PCSC" Version="5.0.0" />
|
||||
<PackageReference Include="PCSC.Iso7816" Version="5.0.0" />
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.8.6.7" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
129
NFC_Test/AuthCrypto_Test.cs
Normal file
129
NFC_Test/AuthCrypto_Test.cs
Normal file
@ -0,0 +1,129 @@
|
||||
using NFC;
|
||||
using NFC.Crypto;
|
||||
using NFC.Mifare_DESFire;
|
||||
using NSubstitute;
|
||||
using NUnit.Framework;
|
||||
using PCSC.Iso7816;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace NFC_Test
|
||||
{
|
||||
public class AuthCrypto_Test
|
||||
{
|
||||
public byte[] GenerateDefaultKey(int size)
|
||||
{
|
||||
List<byte> key = new List<byte>();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
key.Add(0);
|
||||
}
|
||||
|
||||
return key.ToArray();
|
||||
}
|
||||
|
||||
[Test]
|
||||
// https://www.linkedin.com/pulse/mifare-desfire-introduction-david-coelho
|
||||
public void AuthExample()
|
||||
{
|
||||
DES des = new DES();
|
||||
|
||||
byte[] challenge = new byte[]
|
||||
{
|
||||
0x93 ,0x9d ,0x2c ,0x2e ,0xa1 ,0x65 ,0x75 ,0xd5
|
||||
};
|
||||
|
||||
byte[] key = GenerateDefaultKey(8);
|
||||
byte[] iv = GenerateDefaultKey(8);
|
||||
|
||||
byte[] rndA = new byte[]
|
||||
{
|
||||
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07
|
||||
};
|
||||
|
||||
byte[] rndB = des.Decrypt(challenge, key, iv);
|
||||
|
||||
byte[] rndB_expected = new byte[]
|
||||
{
|
||||
0xea ,0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67
|
||||
};
|
||||
|
||||
Assert.AreEqual(rndB_expected, rndB);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void concatenate()
|
||||
{
|
||||
MifareDESFire mifareDESFire = new MifareDESFire(null);
|
||||
|
||||
byte[] rndA = new byte[]
|
||||
{
|
||||
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07
|
||||
};
|
||||
|
||||
byte[] rndB = new byte[]
|
||||
{
|
||||
0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67 ,0xea
|
||||
};
|
||||
|
||||
byte[] rndA_rndB_expected = new byte[]
|
||||
{
|
||||
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07, 0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67 ,0xea
|
||||
};
|
||||
|
||||
byte[] rndA_rndB = mifareDESFire.concatenate(rndA, rndB);
|
||||
|
||||
Assert.AreEqual(rndA_rndB_expected, rndA_rndB);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Auth()
|
||||
{
|
||||
APDUCommand cmd_getchallange = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)0x1A,
|
||||
Data = new byte[]
|
||||
{
|
||||
0x00
|
||||
}
|
||||
};
|
||||
|
||||
APDUCommand cmd_answerchallange = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)0xAF,
|
||||
Data = new byte[]
|
||||
{
|
||||
0x69, 0x17, 0x8b, 0x93, 0x8c, 0x03, 0xed, 0xf1, 0x86, 0xd3, 0x05, 0x6b, 0xed, 0xc8, 0xd6, 0xcf
|
||||
}
|
||||
};
|
||||
|
||||
APDUResponse response = new APDUResponse()
|
||||
{
|
||||
Body = new byte[]
|
||||
{
|
||||
0xB8, 0x90, 0x04, 0x7F, 0x2D, 0xC8, 0xD6, 0x8B
|
||||
}
|
||||
};
|
||||
|
||||
APDUResponse response2 = new APDUResponse()
|
||||
{
|
||||
Body = new byte[]
|
||||
{
|
||||
0x04, 0x79, 0xed, 0x6c, 0x4f, 0x74, 0xda, 0x4a
|
||||
}
|
||||
};
|
||||
|
||||
ICard card = Substitute.For<ICard>();
|
||||
|
||||
card.Transmit(cmd_getchallange).ReturnsForAnyArgs(response);
|
||||
card.Transmit(cmd_answerchallange).Returns(response2);
|
||||
|
||||
MifareDESFire mifareDESFire = new MifareDESFire(card);
|
||||
|
||||
mifareDESFire.Authenticate(0x00, mifareDESFire.GenerateDefaultKey(16));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using NFC;
|
||||
using NFC.Mifare_DESFire;
|
||||
using NFC.Mifare_DESFire.Enums;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -15,7 +16,7 @@ namespace NFC_Test
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_MifareDESFire = new MifareDESFire();
|
||||
//_MifareDESFire = new MifareDESFire();
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -27,7 +28,7 @@ namespace NFC_Test
|
||||
[Test]
|
||||
public void GenerateKeySetting1()
|
||||
{
|
||||
Assert.AreEqual(0xEF, _MifareDESFire.GenerateKeySetting1(MifareDESFire.ChangeApplicationKey.SAMEKEY, MifareDESFire.ChangeMasterKeySettings.WITHMASTERKEY, MifareDESFire.CreateDeleteFile.NOKEY, MifareDESFire.FileDirectoryAccess.NOKEY, MifareDESFire.ChangeMasterKey.CHANGEABLE));
|
||||
Assert.AreEqual(0xEF, _MifareDESFire.GenerateKeySetting1(ChangeApplicationKey.SAMEKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.NOKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||
<PackageReference Include="NSubstitute" Version="4.2.2" />
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||
</ItemGroup>
|
||||
|
@ -212,5 +212,49 @@ namespace NFC_Test
|
||||
reader.Stop();
|
||||
reader.CardDiscovered -= handler;
|
||||
}
|
||||
|
||||
public byte[] GenerateDefaultKey(int size)
|
||||
{
|
||||
List<byte> key = new List<byte>();
|
||||
for(int i = 0; i < size; i++)
|
||||
{
|
||||
key.Add(0);
|
||||
}
|
||||
|
||||
return key.ToArray();
|
||||
}
|
||||
/// <summary>
|
||||
/// Used Default PICC Key with PICC authenticate
|
||||
/// </summary>
|
||||
/// <param name="readerID"></param>
|
||||
[TestCase("ACS ACR122U PICC Interface 0")]
|
||||
public void Authenticate(string readerID)
|
||||
{
|
||||
IHardware hardware = new Hardware();
|
||||
IReader reader = hardware.OpenReader(readerID);
|
||||
|
||||
bool transmit_successfully = false;
|
||||
|
||||
ReaderEventHandler handler = (sender, card) =>
|
||||
{
|
||||
card.Connect();
|
||||
|
||||
MifareDESFire desfire = new MifareDESFire(card);
|
||||
|
||||
desfire.Authenticate(0x00, GenerateDefaultKey(8));
|
||||
|
||||
transmit_successfully = true;
|
||||
|
||||
card.Disconnect();
|
||||
};
|
||||
|
||||
reader.CardDiscovered += handler;
|
||||
reader.Start();
|
||||
|
||||
Assert.AreEqual(true, transmit_successfully);
|
||||
|
||||
reader.Stop();
|
||||
reader.CardDiscovered -= handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ namespace NFC_Test
|
||||
|
||||
MifareDESFire mifareDESFire = new MifareDESFire(card);
|
||||
|
||||
mifareDESFire.Authenticate(0x01, APP_MasterKey);
|
||||
mifareDESFire.Authenticate(0x01, PICC_MasterKey);
|
||||
|
||||
mifareDESFire.Format();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user