Skip to content

Commit f52d3a0

Browse files
authored
Merge pull request #21 from johnkellyoxford/dev
Dev
2 parents 7bd405f + 0f58e71 commit f52d3a0

File tree

14 files changed

+229
-139
lines changed

14 files changed

+229
-139
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
This file was deleted.

EncryptionApp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28010.2016
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncryptionApp", "src\EncryptionApp.csproj", "{701B9935-7BF8-4BA2-861A-7764761C1318}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "utils", "utils\utils.csproj", "{0FA675C6-6565-4F9A-B9D0-56118C9AE1A8}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{701B9935-7BF8-4BA2-861A-7764761C1318}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{701B9935-7BF8-4BA2-861A-7764761C1318}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{701B9935-7BF8-4BA2-861A-7764761C1318}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{0FA675C6-6565-4F9A-B9D0-56118C9AE1A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{0FA675C6-6565-4F9A-B9D0-56118C9AE1A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{0FA675C6-6565-4F9A-B9D0-56118C9AE1A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{0FA675C6-6565-4F9A-B9D0-56118C9AE1A8}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

docs/HeaderSpec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
### <p id="HMAC">HMAC</p>
4343

44-
The HMAC (Hash Message Authentication Code) is a hash that is used to verify the message hasn't been tampered with. The HMAC is the hashed value of the encrypted message. The receiver can re-hash the received message to verify the message hasn't been tampered with. **IMPORTANT: HMAC construction MUST be actual HMAC algorithm, not just hashing. See [this](https://en.wikipedia.org/wiki/HMAC#Implementation) for details ****
44+
The HMAC (Hash Message Authentication Code) is a hash that is used to verify the message hasn't been tampered with. The HMAC is the hashed value of the encrypted message. The receiver can re-hash the received message to verify the message hasn't been tampered with. **IMPORTANT: HMAC construction MUST be actual HMAC algorithm, not just hashing. See [this](https://en.wikipedia.org/wiki/HMAC#Implementation) for details ***
4545

4646
### <p id ="ENCRYPTMODE">ENCRYPTMODE</P>
4747

src/Backend/AESCryptoManager.cs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,78 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Security.Cryptography;
43

5-
namespace Encryption_App
4+
namespace Encryption_App.Backend
65
{
7-
class AESCryptoManager
6+
internal class AesCryptoManager
87
{
9-
public void EncryptBytes(string iF, string oF, byte[] passwordBytes)
8+
public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes)
109
{
1110

12-
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
11+
var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
1312

14-
using (var AES = new AesManaged())
13+
using (var aes = new AesManaged())
1514
{
1615

17-
AES.KeySize = 256;
18-
AES.BlockSize = 128;
16+
// AESManaged properties
17+
aes.KeySize = 256;
18+
aes.BlockSize = 128;
19+
aes.Padding = PaddingMode.PKCS7;
20+
aes.Mode = CipherMode.CBC;
21+
22+
23+
// Derives a key using PBKDF2 from the password and a salts
1924
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
20-
AES.Key = key.GetBytes(AES.KeySize / 8);
21-
AES.IV = key.GetBytes(AES.BlockSize / 8);
22-
AES.Padding = PaddingMode.PKCS7;
23-
AES.Mode = CipherMode.CBC;
24-
25-
using (var outFile = File.Create(oF))
26-
using (var cs = new CryptoStream(outFile, AES.CreateEncryptor(), CryptoStreamMode.Write))
27-
using (var inFile = File.OpenRead(iF))
28-
using (var br = new BinaryReader(inFile))
25+
26+
27+
// Set actual IV and key
28+
aes.Key = key.GetBytes(aes.KeySize / 8);
29+
aes.IV = key.GetBytes(aes.BlockSize / 8);
30+
31+
using (var outFileStream = new FileStream(outFile, FileMode.Create))
32+
using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
33+
using (var inFileStream = new FileStream(inputFile, FileMode.Create))
2934
{
3035

3136
sbyte data;
32-
while ((data = (sbyte)inFile.ReadByte()) != -1)
37+
while ((data = (sbyte)inFileStream.ReadByte()) != -1)
3338
cs.WriteByte((byte)data);
34-
3539
}
3640
}
3741
}
3842

39-
public bool DecryptBytes(string iF, string oF, byte[] passwordBytes)
43+
public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes)
4044
{
4145

42-
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
46+
var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
4347

44-
using (var AES = new AesManaged())
48+
using (var aes = new AesManaged())
4549
{
4650

4751
// AESManaged properties
48-
AES.KeySize = 256;
49-
AES.BlockSize = 128;
50-
AES.Padding = PaddingMode.PKCS7;
51-
AES.Mode = CipherMode.CBC;
52+
aes.KeySize = 256;
53+
aes.BlockSize = 128;
54+
aes.Padding = PaddingMode.PKCS7;
55+
aes.Mode = CipherMode.CBC;
5256

57+
58+
// Derives a key using PBKDF2 from the password and a salt
5359
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
5460

5561

5662
// Set actual IV and key
57-
AES.Key = key.GetBytes(AES.KeySize / 8);
58-
AES.IV = key.GetBytes(AES.BlockSize / 8);
63+
aes.Key = key.GetBytes(aes.KeySize / 8);
64+
aes.IV = key.GetBytes(aes.BlockSize / 8);
5965

6066
try
6167
{
62-
using (var inFile = File.OpenRead(iF))
63-
using (var cs = new CryptoStream(inFile, AES.CreateDecryptor(), CryptoStreamMode.Read))
64-
using (var outFile = File.Create(oF))
68+
using (var outFileStream = new FileStream(outFile, FileMode.Create))
69+
using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
70+
using (var inFileStream = new FileStream(inputFile, FileMode.Create))
6571
{
6672

6773
sbyte data;
68-
while ((data = (sbyte)cs.ReadByte()) != -1)
69-
outFile.WriteByte((byte)data);
70-
74+
while ((data = (sbyte)inFileStream.ReadByte()) != -1)
75+
cs.WriteByte((byte)data);
7176
}
7277
}
7378
catch (CryptographicException)

src/Backend/CryptoManager.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Encryption_App.Backend
1+
namespace Encryption_App.Backend
82
{
9-
abstract class CryptoManager
3+
internal abstract class CryptoManager
104
{
115
public abstract byte[] EncryptBytes();
12-
6+
137
public abstract bool DecryptBytes();
148
}
159
}
Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
using System;
2-
using System.IO;
32
using System.Linq;
43
using System.Security.Cryptography;
54

6-
namespace Encryption_App
5+
namespace Encryption_App.Backend
76
{
8-
class MessageAuthenticator
7+
/// <summary>
8+
/// Used for signing and verifying HMACs
9+
/// </summary>
10+
internal class MessageAuthenticator
911
{
12+
/// <summary>
13+
/// Creates a byte[] hashcode that represents the file and key hashed with SHA384. Do not try and verify this yourself, use the VerifyHMAC() func
14+
/// </summary>
15+
/// <param name="data">A byte[] of the encrypted message data</param>
16+
/// <param name="key">A byte[] of the key</param>
17+
/// <returns>A byte[] hash that is the file and key hashed</returns>
1018
public byte[] CreateHMAC(byte[] data, byte[] key)
1119
{
1220
byte[] hashKey;
@@ -18,17 +26,24 @@ public byte[] CreateHMAC(byte[] data, byte[] key)
1826

1927
return hashKey;
2028
}
21-
22-
public byte[] CreateHMAC(byte[] data, byte[] key, Type TypeOfHash)
29+
30+
/// <summary>
31+
/// Signs a encrypted file and key with a hash algorithm of your choosing. Do not try and verify this yourself, use the VerifyHMAC() func
32+
/// </summary>
33+
/// <param name="data">A byte[] of the encrypted message data</param>
34+
/// <param name="key">A byte[] of the key</param>
35+
/// <param name="typeOfHash">typeof() any derivative of the System.Security.Cryptography.HMAC class</param>
36+
/// <returns>A byte[] hash that is the file and key hashed</returns>
37+
public byte[] CreateHMAC(byte[] data, byte[] key, Type typeOfHash)
2338
{
2439
HMAC hmac;
25-
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
40+
if (typeOfHash.IsSubclassOf(typeof(HMAC)))
2641
{
27-
hmac = (HMAC)Activator.CreateInstance(TypeOfHash);
42+
hmac = (HMAC)Activator.CreateInstance(typeOfHash);
2843
}
2944
else
3045
{
31-
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
46+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptography.HMAC\"");
3247
}
3348

3449
byte[] hashKey;
@@ -41,6 +56,13 @@ public byte[] CreateHMAC(byte[] data, byte[] key, Type TypeOfHash)
4156
return hashKey;
4257
}
4358

59+
/// <summary>
60+
/// A function that verifies a HMAC file with SHA384
61+
/// </summary>
62+
/// <param name="data">A byte[] of encrypted message data</param>
63+
/// <param name="key">A byte[] of the key</param>
64+
/// <param name="hash">The hash in the header file/the hash provided, that's been hashed with SHA384</param>
65+
/// <returns>True if they match, otherwise false</returns>
4466
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash)
4567
{
4668
byte[] hashKey;
@@ -50,24 +72,28 @@ public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash)
5072
hashKey = hmac.ComputeHash(data);
5173
}
5274

53-
if (data.SequenceEqual(hash))
54-
{
55-
return true;
56-
}
57-
58-
return false;
75+
return hashKey.SequenceEqual(hash);
5976
}
6077

61-
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type TypeOfHash)
78+
79+
/// <summary>
80+
/// A function that verifies a HMAC file with a hash algorithm of your choice
81+
/// </summary>
82+
/// <param name="data">A byte[] of encrypted message data</param>
83+
/// <param name="key">A byte[] of the key</param>
84+
/// <param name="hash">The hash in the header file/the hash provided, that's been hashed with typeOfHash</param>
85+
/// <param name="typeOfHash">typeof() the hash algorithm you used to create this, derived from System.Security.Cryptography.HMAC</param>
86+
/// <returns>True if they match, otherwise false</returns>
87+
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type typeOfHash)
6288
{
6389
HMAC hmac;
64-
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
90+
if (typeOfHash.IsSubclassOf(typeof(HMAC)))
6591
{
66-
hmac = (HMAC)Activator.CreateInstance(TypeOfHash);
92+
hmac = (HMAC)Activator.CreateInstance(typeOfHash, key);
6793
}
6894
else
6995
{
70-
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
96+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptography.HMAC\"");
7197
}
7298

7399
byte[] hashKey;
@@ -77,12 +103,7 @@ public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type TypeOfHash)
77103
hashKey = hmac.ComputeHash(data);
78104
}
79105

80-
if (data.SequenceEqual(hash))
81-
{
82-
return true;
83-
}
84-
85-
return false;
106+
return data.SequenceEqual(hashKey); // returns true if they match
86107
}
87108
}
88-
}
109+
}

src/EncryptionApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
6161
</Page>
62-
<Compile Include="Backend\AESCryptoManager.cs" />
62+
<Compile Include="Backend\AesCryptoManager.cs" />
6363
<Compile Include="Backend\CryptoManager.cs" />
6464
<Compile Include="Backend\MessageAuthenticator.cs" />
6565
<Compile Include="UI\App.xaml.cs">

src/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Reflection;
2-
using System.Resources;
3-
using System.Runtime.CompilerServices;
42
using System.Runtime.InteropServices;
53
using System.Windows;
64

src/UI/App.xaml.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
using System.Windows;
1+
using System.Windows;
82

93
namespace Encryption_App
104
{

0 commit comments

Comments
 (0)