Skip to content

Commit 7bd405f

Browse files
authored
Merge pull request #20 from johnkellyoxford/dev
Dev
2 parents db9d2f3 + 919eff2 commit 7bd405f

File tree

7 files changed

+141
-29
lines changed

7 files changed

+141
-29
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# EncryptorAppVS
22

3+
Master: [![Build status](https://ci.appveyor.com/api/projects/status/lspd6npm0sbwtscm?svg=true)](https://ci.appveyor.com/project/johnkellyoxford/encryptionapp)
4+
35
## Documentation:
46

57
## Right now
@@ -17,4 +19,3 @@
1719
## Updates
1820
* Add error handling if encryption/decryption fails
1921

20-
"Copied this from John's cause why not" @nightraven3142

docs/HeaderSpec.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,47 @@
22

33
## Header format
44

5-
* The items in the header are as followed (square brackets representing the start and end of the header, anything in braces is related to the previous option). Newlines and tabs are not true, but simply for formatting:
5+
* At the start of the header, it is marked by 5 null characters (byte value 0000 0000), and then the following ASCII string: "BEGIN ENCRYPTION HEADER STRING"
6+
7+
* The end of the header is marked by the following ASCII string "END ENCRYPTION HEADER STRING", and followed by 5 null characters (byte value 0000 0000)
8+
9+
* The items in the header are as followed (square brackets representing the start and end of the header, anything in braces is related to the previous option). All whitespace but simply for formatting:
610

711
```
812
[
913
-HMAC:(hash here, None)
10-
{ -HASHALGO:(PBKDF2, SHA256, bcrypt) }
11-
{ -ITERATIONS:(iterations here) }
12-
-KEYVERIFICATIONHASH:(hash here, None)
13-
{ -HASHALGO:(PBKDF2, SHA256, bcrypt) }
14-
{ -ITERATIONS:(iterations here) }
14+
{
15+
-HASHALGO:(PBKDF2, SHA256, bcrypt),
16+
-ITERATIONS:(iterations here)
17+
}
18+
1519
-ENCRYPTMODE:(AES, RSA, etc)
16-
{{-AESMODE:(ECB, CBC, CFB, CTR),
17-
-ECCMODE:(different curves fo here)}
18-
-KEYSIZE: (int)
19-
-BLOCKSIZE: (int)
20+
{
21+
-AESMODE:(ECB, CBC, CFB, CTR),
22+
-ECCMODE:(different curves fo here),
23+
-KEYSIZE:(int),
24+
-BLOCKSIZE:(int)
2025
}
21-
{ -IV:(IV here) }
26+
27+
-IV:(IV here)
2228
]
2329
```
2430

2531
## Header items
2632

27-
| Argument | Meaning | Values|
28-
| ------------------------------------------------------ |-------------------------------- | ------------------------------------ |
29-
| <a href="#HMAC">HMAC</a> | The verification hash used to confirm the file hasn't changed | 128 - 512 bit byte array (16-64 bytes) |
30-
| <a href="#KEYVERIFICATIONHASH">KEYVERIFICATIONHASH</a> | A hash of the key to verify if the password is correct | 128 - 512 bit byte array (16-64 bytes) |
31-
| <a href="#ENCRYPTMODE">ENCRYPTMODE</a> | A byte representing the encryption type | A string in the table of <a href="#ENCRYPTMODE">ENCRYPTMODE</a> page, and the (undefined ATM -- TODO) enumeration |
33+
* Subitems are in the hyperlinks
34+
35+
| Argument | Meaning | Values |
36+
| ------------------------------------------------------ |-------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
37+
| <a href="#HMAC">HMAC</a> | The verification hash used to confirm the file hasn't changed | 128 - 512 bit byte array (16-64 bytes) |
38+
| <a href="#ENCRYPTMODE">ENCRYPTMODE</a> | A byte representing the encryption type | A string in the table of <a href="#ENCRYPTMODE">ENCRYPTMODE</a> page, and the (undefined ATM -- TODO) enumeration |
39+
| <a href="IV">IV</a> | The initialization vector used to start the encryption | 128 - 512 bit byte array (16-64 bytes) |
40+
3241

3342
### <p id="HMAC">HMAC</p>
3443

35-
### <p id="KEYVERIFICATIONHASH">KEYVERIFICATIONHASH</p>
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 ****
3645

3746
### <p id ="ENCRYPTMODE">ENCRYPTMODE</P>
47+
48+
### <p id="IV">IV</p>

src/Backend/AESCryptoManager.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Encryption_App
66
{
77
class AESCryptoManager
88
{
9-
public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
9+
public void EncryptBytes(string iF, string oF, byte[] passwordBytes)
1010
{
1111

1212
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -33,13 +33,10 @@ public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
3333
cs.WriteByte((byte)data);
3434

3535
}
36-
37-
3836
}
39-
4037
}
4138

42-
public bool AES_Decrypt(string iF, string oF, byte[] passwordBytes)
39+
public bool DecryptBytes(string iF, string oF, byte[] passwordBytes)
4340
{
4441

4542
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -79,8 +76,6 @@ public bool AES_Decrypt(string iF, string oF, byte[] passwordBytes)
7976
}
8077
return true;
8178
}
82-
8379
}
84-
8580
}
8681
}

src/Backend/CryptoManager.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
8+
{
9+
abstract class CryptoManager
10+
{
11+
public abstract byte[] EncryptBytes();
12+
13+
public abstract bool DecryptBytes();
14+
}
15+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Security.Cryptography;
5+
6+
namespace Encryption_App
7+
{
8+
class MessageAuthenticator
9+
{
10+
public byte[] CreateHMAC(byte[] data, byte[] key)
11+
{
12+
byte[] hashKey;
13+
14+
using (var hmac = new HMACSHA384(key))
15+
{
16+
hashKey = hmac.ComputeHash(data);
17+
}
18+
19+
return hashKey;
20+
}
21+
22+
public byte[] CreateHMAC(byte[] data, byte[] key, Type TypeOfHash)
23+
{
24+
HMAC hmac;
25+
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
26+
{
27+
hmac = (HMAC)Activator.CreateInstance(TypeOfHash);
28+
}
29+
else
30+
{
31+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
32+
}
33+
34+
byte[] hashKey;
35+
36+
using (hmac)
37+
{
38+
hashKey = hmac.ComputeHash(data);
39+
}
40+
41+
return hashKey;
42+
}
43+
44+
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash)
45+
{
46+
byte[] hashKey;
47+
48+
using (var hmac = new HMACSHA384(key))
49+
{
50+
hashKey = hmac.ComputeHash(data);
51+
}
52+
53+
if (data.SequenceEqual(hash))
54+
{
55+
return true;
56+
}
57+
58+
return false;
59+
}
60+
61+
public bool VerifyHMAC(byte[] data, byte[] key, byte[] hash, Type TypeOfHash)
62+
{
63+
HMAC hmac;
64+
if (TypeOfHash.IsSubclassOf(typeof(HMAC)))
65+
{
66+
hmac = (HMAC)Activator.CreateInstance(TypeOfHash);
67+
}
68+
else
69+
{
70+
throw new ArgumentException("TypeOfHash is not a derivative of \"System.Security.Cryptorgaphy.HMAC\"");
71+
}
72+
73+
byte[] hashKey;
74+
75+
using (hmac)
76+
{
77+
hashKey = hmac.ComputeHash(data);
78+
}
79+
80+
if (data.SequenceEqual(hash))
81+
{
82+
return true;
83+
}
84+
85+
return false;
86+
}
87+
}
88+
}

src/EncryptionApp.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
<SubType>Designer</SubType>
6161
</Page>
6262
<Compile Include="Backend\AESCryptoManager.cs" />
63-
<Compile Include="UI\App.xaml.cs" >
63+
<Compile Include="Backend\CryptoManager.cs" />
64+
<Compile Include="Backend\MessageAuthenticator.cs" />
65+
<Compile Include="UI\App.xaml.cs">
6466
<DependentUpon>App.xaml</DependentUpon>
6567
<SubType>Code</SubType>
6668
</Compile>
@@ -97,4 +99,4 @@
9799
</ItemGroup>
98100
<ItemGroup />
99101
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
100-
</Project>
102+
</Project>

src/UI/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
7979
string pwd = InpTxtBox.Text;
8080
string ofilePath = FileTxtBox.Text;
8181
AESCryptoManager encryptor = new AESCryptoManager();
82-
encryptor.AES_Encrypt(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
82+
encryptor.EncryptBytes(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
8383
File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", ofilePath, true);
8484
}
8585

@@ -91,7 +91,7 @@ private void Decrypt_Click(object sender, RoutedEventArgs e)
9191
FileInfo f = new FileInfo(ofilePath);
9292

9393
AESCryptoManager decryptor = new AESCryptoManager();
94-
bool worked = decryptor.AES_Decrypt(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd)); ;
94+
bool worked = decryptor.DecryptBytes(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd)); ;
9595
if (worked) { File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", ofilePath, true); }
9696

9797
if (!worked)

0 commit comments

Comments
 (0)