-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3. Data Streams : Cryptography Basics #118
- [x] review existing code - [x] add a placeholder to the project - [x] add code example - consider merging with type compatibility - [x] add unit tests - UT 👍
- Loading branch information
Showing
9 changed files
with
314 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Crypto | ||
csharp | ||
Globalization | ||
Hermetization | ||
|
89 changes: 89 additions & 0 deletions
89
Lecture/P03.DataStreams/DataStreams.UnitTest/CryptographyHelpersUnitTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//____________________________________________________________________________ | ||
// | ||
// Copyright (C) 2018, Mariusz Postol LODZ POLAND. | ||
// | ||
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP | ||
//____________________________________________________________________________ | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Xml; | ||
|
||
namespace TP.DataStreams.Cryptography | ||
{ | ||
[TestClass] | ||
public class CryptographyHelpersUnitTest | ||
{ | ||
[TestMethod] | ||
public void CalculateSHA256Test() | ||
{ | ||
const string _pasword = "Fe4ZFH2VgpOOwBdM9dkI"; | ||
(string HexString, string Base64) = _pasword.CalculateSHA256(); | ||
Assert.AreEqual<string>("D9CBFBB0AD87D8CE0CC0D2FF1BCFF67C6933D2B4B4CC4C6FA60CE29F73322CD8", HexString); | ||
Assert.AreEqual<int>(256/4, HexString.Length); | ||
Assert.AreEqual<string>("2cv7sK2H2M4MwNL/G8/2fGkz0rS0zExvpgzin3MyLNg=", Base64); | ||
Assert.AreEqual<int>(44, Base64.Length); | ||
} | ||
[TestMethod] | ||
public void CalculateSHA256LongTest() | ||
{ | ||
const string _pasword = "UAXflAoVz8wcR1VkxUgTO8HAMBYK83yQvcHI9nqsQUiI4mx6jLlCAFGPrHj99XHi3uOoUfqe7wF7JkX2wBwwPADpn9f8s2Q0CfaA"; | ||
(string HexString, string Base64) = _pasword.CalculateSHA256(); | ||
Assert.AreEqual<string>("B77B064339A0D98C722F54A7B055539BD63BAFC07EBD2B9342EA247EAA86FB18", HexString); | ||
Assert.AreEqual<int>(256 / 4, HexString.Length); | ||
Assert.AreEqual<string>("t3sGQzmg2YxyL1SnsFVTm9Y7r8B+vSuTQuokfqqG+xg=", Base64); | ||
Assert.AreEqual<int>(44, Base64.Length); | ||
} | ||
[TestMethod] | ||
public void XmlSignatureTestMethod1() | ||
{ | ||
(RSAParameters parameters, string publicKey, string privatePublicKeys) = CreateRSACryptoServiceKeys(); | ||
Assert.IsNotNull(parameters); | ||
Assert.AreNotEqual<string>(publicKey, privatePublicKeys); | ||
Debug.WriteLine(publicKey); | ||
Debug.WriteLine(string.Empty); | ||
Debug.WriteLine(privatePublicKeys); | ||
} | ||
[TestMethod] | ||
[DeploymentItem("Instrumentation")] | ||
public void XmlSignatureTest() | ||
{ | ||
const string _xmlFile = @"catalog.example.xml"; | ||
Assert.IsTrue(File.Exists(_xmlFile)); | ||
const string _publiKey = @"PubliKey.xml"; | ||
Assert.IsTrue(File.Exists(_publiKey)); | ||
const string _publiPrivateKeys = @"PubliPrivateKeys.xml"; | ||
Assert.IsTrue(File.Exists(_publiPrivateKeys)); | ||
XmlDocument _documentToSign = new XmlDocument(); | ||
_documentToSign.Load(_xmlFile); | ||
const string _signedFileName = "SignedXmlFile.xml"; | ||
string _rsaKeys = null; | ||
if (File.Exists(_signedFileName)) | ||
File.Delete(_signedFileName); | ||
using (StreamReader _reader = new StreamReader(_publiPrivateKeys, System.Text.Encoding.UTF8)) | ||
_rsaKeys = _reader.ReadToEnd(); | ||
CryptographyHelpers.SignSaveXml(_documentToSign, _rsaKeys, _signedFileName); | ||
Assert.IsTrue(File.Exists(_signedFileName)); | ||
XmlDocument _signedXmlDocument1 = CryptographyHelpers.LoadVerifyXml(_signedFileName); | ||
Assert.IsNotNull(_signedXmlDocument1); | ||
using (StreamReader _reader = new StreamReader(_publiKey, System.Text.Encoding.UTF8)) | ||
_rsaKeys = _reader.ReadToEnd(); | ||
XmlDocument _signedXmlDocument2 = CryptographyHelpers.LoadVerifyXml(_rsaKeys, _signedFileName); | ||
Assert.IsNotNull(_signedXmlDocument2); | ||
Assert.AreEqual<string>(_signedXmlDocument1.ToString(), _signedXmlDocument2.ToString()); | ||
} | ||
private (RSAParameters parameters, string publicKey, string privatePublicKeys) CreateRSACryptoServiceKeys() | ||
{ | ||
using (RSACryptoServiceProvider _rsa = new RSACryptoServiceProvider(2048)) | ||
{ | ||
RSAParameters _parameters = _rsa.ExportParameters(true); | ||
string _public = _rsa.ToXmlString(false); | ||
string _both = _rsa.ToXmlString(true); | ||
return (_parameters, _public, _both); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
Lecture/P03.DataStreams/DataStreams.UnitTest/Instrumentation/PubliKey.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<RSAKeyValue> | ||
<Modulus>2oFiov7xnJGWf++WBHOdkzp1IHzNZRXhFL0MDFxBIJQvlm3p80WwhoxUQVawJTGRbdGMutOHHOTj+uxFAFlmKrvcaltfRV/rq0Z43LeIbfeX8uwsr10lnxqLhONlP+kY9+RiAartXP3nTG88gFah9j7WrSmlrJWvSCD0lZvUxcuFioXXZw8UQ43046SWhXMYABkZ+1RYKsSjfz80/TITuR0kkY3wIwHiIWhZUjWwNxEh0CLmM06oFLjSKgZwe4UyhCRlg3iXNK3CeMC+ToSyo+lehGnWD226uNECvB3SWC4YN7r96u0xrQcf0//x6nzSdEArohFVyGgB3yb6rlFLjw==</Modulus> | ||
<Exponent>AQAB</Exponent> | ||
</RSAKeyValue> |
12 changes: 12 additions & 0 deletions
12
Lecture/P03.DataStreams/DataStreams.UnitTest/Instrumentation/PubliPrivateKeys.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<RSAKeyValue> | ||
<Modulus>2oFiov7xnJGWf++WBHOdkzp1IHzNZRXhFL0MDFxBIJQvlm3p80WwhoxUQVawJTGRbdGMutOHHOTj+uxFAFlmKrvcaltfRV/rq0Z43LeIbfeX8uwsr10lnxqLhONlP+kY9+RiAartXP3nTG88gFah9j7WrSmlrJWvSCD0lZvUxcuFioXXZw8UQ43046SWhXMYABkZ+1RYKsSjfz80/TITuR0kkY3wIwHiIWhZUjWwNxEh0CLmM06oFLjSKgZwe4UyhCRlg3iXNK3CeMC+ToSyo+lehGnWD226uNECvB3SWC4YN7r96u0xrQcf0//x6nzSdEArohFVyGgB3yb6rlFLjw==</Modulus> | ||
<Exponent>AQAB</Exponent> | ||
<P>9A0KwiudcPWDrwVIq+2ANkuu7IILGf9D94U+qViZAKclqxc9+7UcCeP1a0LCmTiJCVLvNam4ISvU3I7rBnE46cFvJ7c+9SIQZTfb9HOFB78Q5+ulx9k9lv8VKypfuBynoV15f3OHkr2FddSsyKf4EuF20UVWsV8YSJ+iJ5gCc8M=</P> | ||
<Q>5TQnMXCb2DjgY1sGfQgaOtVOakjem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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
Lecture/P03.DataStreams/DataStreams/Cryptography/CryptographyHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
//____________________________________________________________________________ | ||
// | ||
// Copyright (C) 2018, Mariusz Postol LODZ POLAND. | ||
// | ||
// To be in touch join the community at GITTER: https://gitter.im/mpostol/TP | ||
//____________________________________________________________________________ | ||
|
||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.Xml; | ||
using System.Text; | ||
using System.Xml; | ||
|
||
namespace TP.DataStreams.Cryptography | ||
{ | ||
|
||
public static class CryptographyHelpers | ||
{ | ||
public static (string Hex, string Base64) CalculateSHA256 (this string inputStream) | ||
{ | ||
byte[] _inputStreamBytes = Encoding.UTF8.GetBytes(inputStream); | ||
using (SHA256 mySHA256 = SHA256Managed.Create()) | ||
{ | ||
byte[] hashValue = mySHA256.ComputeHash(_inputStreamBytes); | ||
return (hashValue.ToHexString(), Convert.ToBase64String(hashValue, Base64FormattingOptions.InsertLineBreaks)); | ||
} | ||
} | ||
public static void EncryptData(string inFileName, string outFileName, byte[] dESKey, byte[] dESIV, IProgress<long> progress) | ||
{ | ||
//Create the file streams to handle the input and output files. | ||
using (FileStream _inFileStream = new FileStream(inFileName, FileMode.Open, FileAccess.Read)) | ||
{ | ||
using (FileStream _outFileStream = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write)) | ||
{ | ||
_outFileStream.SetLength(0); | ||
//Create variables to help with read and write. | ||
byte[] _buffer = new byte[100]; //This is intermediate storage for the encryption. | ||
long _bytesWritten = 0; //This is the total number of bytes written. | ||
long _inFileStreamLength = _inFileStream.Length; //This is the total length of the input file. | ||
int _length; //This is the number of bytes to be written at a time. | ||
TripleDESCryptoServiceProvider _tripleProvider = new TripleDESCryptoServiceProvider(); | ||
using (CryptoStream _outCryptoStream = new CryptoStream(_outFileStream, _tripleProvider.CreateEncryptor(dESKey, dESIV), CryptoStreamMode.Write)) | ||
{ | ||
//Read from the input file, then encrypt and write to the output file. | ||
while (_bytesWritten < _inFileStreamLength) | ||
{ | ||
_length = _inFileStream.Read(_buffer, 0, 100); | ||
_outCryptoStream.Write(_buffer, 0, _length); | ||
_bytesWritten = _bytesWritten + _length; | ||
progress.Report(_bytesWritten); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
/// <summary> | ||
/// Sign and save an XML document. | ||
/// </summary> | ||
/// <param name="document">Document to be signed</param> | ||
/// <param name="rsa">The keys ro be used by the <see cref="RSACryptoServiceProvider"/> RSA algorithm implementation.</param> | ||
/// <remarks> | ||
/// This document cannot be verified unless the verifying code has the public key with which it was signed. | ||
/// </remarks> | ||
public static void SignSaveXml(this XmlDocument document, string rsaKeys, string fileName) | ||
{ | ||
#region Check arguments | ||
if (document == null) | ||
throw new ArgumentException($"The {nameof(document)} parameter cannot be null"); | ||
if (string.IsNullOrEmpty(rsaKeys)) | ||
throw new ArgumentException($"The {nameof(rsaKeys)} parameter cannot be null"); | ||
if (string.IsNullOrEmpty(fileName)) | ||
throw new ArgumentException($"The {nameof(fileName)} parameter cannot be null"); | ||
#endregion | ||
using (RSACryptoServiceProvider _rsaProvider = new RSACryptoServiceProvider()) | ||
{ | ||
_rsaProvider.FromXmlString(rsaKeys); | ||
KeyInfo _keyInfo = new KeyInfo();// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). | ||
_keyInfo.AddClause(new RSAKeyValue(_rsaProvider)); | ||
SignedXml _signedXml = new SignedXml(document) | ||
{ | ||
SigningKey = _rsaProvider, // Add the key to the SignedXml document. | ||
KeyInfo = _keyInfo | ||
}; | ||
Reference _reference = new Reference // Create a reference to be signed. | ||
{ | ||
Uri = "" //An entire XML document is to be signed using an enveloped signature. | ||
}; | ||
XmlDsigEnvelopedSignatureTransform _envelope = new XmlDsigEnvelopedSignatureTransform(); // Add an enveloped transformation to the reference. | ||
_reference.AddTransform(_envelope); | ||
_signedXml.AddReference(_reference); // Add the reference to the SignedXml object. | ||
_signedXml.ComputeSignature(); // Compute the signature. | ||
XmlElement _xmlDigitalSignature = _signedXml.GetXml(); // Get the XML representation of the signature and save it to an XmlElement object. | ||
document.DocumentElement.AppendChild(document.ImportNode(_xmlDigitalSignature, true));// Append the element to the XML document. | ||
} | ||
document.Save(fileName); | ||
} | ||
/// <summary> | ||
/// Load and verify the signature of an XML file against an asymmetric RSA algorithm and return the document. | ||
/// </summary> | ||
/// <param name="rsaKeys">The RSA keys as the xml document.</param> | ||
/// <param name="fileName">Name of the file.</param> | ||
/// <returns>An instance of the <see cref="XmlDocument"/> capturing the xml file.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// rsaKeys | ||
/// or | ||
/// fileName | ||
/// </exception> | ||
/// <exception cref="System.Security.Cryptography.CryptographicException"></exception> | ||
/// <remarks>There must be only one signature.</remarks> | ||
public static XmlDocument LoadVerifyXml(string rsaKeys, string fileName) | ||
{ | ||
#region Check arguments | ||
if (string.IsNullOrEmpty(rsaKeys)) | ||
throw new ArgumentException($"The {nameof(rsaKeys)} parameter cannot be null"); | ||
if (string.IsNullOrEmpty(fileName)) | ||
throw new ArgumentException($"The {nameof(fileName)} parameter cannot be null"); | ||
#endregion | ||
(XmlDocument _document, SignedXml _signedXml) = LoadsIGNEDXmlFile(fileName); | ||
using (RSACryptoServiceProvider _provider = new RSACryptoServiceProvider()) | ||
{ | ||
_provider.FromXmlString(rsaKeys); | ||
if (!_signedXml.CheckSignature(_provider))// Check the signature and return the result. | ||
throw new CryptographicException($"Wrong signature of the document."); | ||
} | ||
return _document; | ||
} | ||
public static XmlDocument LoadVerifyXml(string fileName) | ||
{ | ||
#region Check arguments | ||
if (string.IsNullOrEmpty(fileName)) | ||
throw new ArgumentException($"The {nameof(fileName)} parameter cannot be null"); | ||
#endregion | ||
(XmlDocument _document, SignedXml _signedXml) = LoadsIGNEDXmlFile(fileName); | ||
if (!_signedXml.CheckSignature())// Check the signature and return the result. | ||
throw new System.Security.Cryptography.CryptographicException($"Wrong signature of the document."); | ||
return _document; | ||
} | ||
private static string ToHexString(this byte[] bytes) | ||
{ | ||
char[] c = new char[bytes.Length * 2]; | ||
int b; | ||
for (int i = 0; i < bytes.Length; i++) | ||
{ | ||
b = bytes[i] >> 4; | ||
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7)); | ||
b = bytes[i] & 0xF; | ||
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7)); | ||
} | ||
return new string(c); | ||
} | ||
private static (XmlDocument document, SignedXml signedXml) LoadsIGNEDXmlFile(string fileName) | ||
{ | ||
XmlDocument _document = new XmlDocument(); | ||
_document.Load(fileName); | ||
SignedXml _signedXml = new SignedXml(_document); | ||
XmlNodeList _nodeList = _document.GetElementsByTagName("Signature");// Find the "Signature" node and create a new XmlNodeList object. | ||
if (_nodeList.Count != 1) // There must be only one signature. Return false if 0 or more than one signatures was found. | ||
throw new CryptographicException($"Expected exactly one signature but the file contaons {_nodeList.Count}."); | ||
_signedXml.LoadXml((XmlElement)_nodeList[0]);// Load the first <signature> node. | ||
return (_document, _signedXml); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="System.Security.AccessControl" version="4.5.0" targetFramework="net471" /> | ||
<package id="System.Security.Cryptography.Xml" version="4.5.0" targetFramework="net471" /> | ||
<package id="System.Security.Permissions" version="4.5.0" targetFramework="net471" /> | ||
<package id="System.Security.Principal.Windows" version="4.5.0" targetFramework="net471" /> | ||
</packages> |