BackUp DES

This commit is contained in:
TheJoKlLa
2020-10-01 19:06:09 +02:00
parent b9708da234
commit c943a51d9c
9 changed files with 393 additions and 8 deletions

45
NFC/Crypto/DES.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 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
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 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;
}
}
}

View File

@ -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();
}

View File

@ -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>