Skip to content

Commit 4b7313e

Browse files
committed
Working encryption test ft. weird newline bug.
1 parent c7dc2a8 commit 4b7313e

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

Main.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ private void button2_Click(object sender, EventArgs e)
112112
private void debugBtn_Click(object sender, EventArgs e)
113113
{
114114
Aes.setEncryptKey(System.Text.Encoding.UTF8.GetBytes("SIXTEENCHARACTRS"));
115-
outputTxt.AppendText("[DEBUG ENCRYPT] <<< " + Convert.ToBase64String(Aes.encryptOutgoing("Test Message")) + "" +Environment.NewLine);
115+
Aes.setDecryptKey(System.Text.Encoding.UTF8.GetBytes("SIXTEENCHARACTRS"));
116+
var b = Aes.encryptOutgoing("Test Message");
117+
//outputTxt.AppendText("[DEBUG ENCRYPT] <<< " + "Message: " + Convert.ToBase64String(b[1]) + $" Key: SIXTEENCHARACTRS IV: {Convert.ToBase64String(b[0])}" +Environment.NewLine);
118+
outputTxt.AppendText($"[TESTING DECRYPT] {Aes.decryptIncoming(b)} {Environment.NewLine}");
116119
}
117120
}
118-
}
121+
}

Modules/Encryption.cs

+46-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Security.Cryptography;
1+
using System.Security.Cryptography;
52
using System.Text;
6-
using System.Threading.Tasks;
73

84
namespace UDP_UI.Modules
95
{
@@ -26,31 +22,69 @@ public AesModule()
2622
_encrypt.KeySize = 128;
2723
}
2824

29-
public void setEncryptKey(byte[] key) {
25+
public void setEncryptKey(byte[] key)
26+
{
3027
_encrypt.Key = key;
3128
}
3229

33-
public void setDecryptKey(byte[] key) {
30+
public void setDecryptKey(byte[] key)
31+
{
3432
_decrypt.Key = key;
3533
}
3634

37-
public byte[] encryptOutgoing(string text)
35+
public string encryptOutgoing(string text)
3836
{
3937
_encrypt.GenerateIV();
4038
byte[] buff;
41-
using(var encryptor = _encrypt.CreateEncryptor()){
42-
using (var memStream = new MemoryStream()) {
43-
using (var csEncrypt = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write)) {
39+
using (var encryptor = _encrypt.CreateEncryptor())
40+
{
41+
using (var memStream = new MemoryStream())
42+
{
43+
using (var csEncrypt = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
44+
{
4445
using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))
4546
{
4647
bw.Write(text);
4748
bw.Close();
4849
buff = memStream.ToArray();
49-
return _encrypt.IV.Concat(buff).ToArray();
50+
5051
}
5152
}
5253
}
5354
}
55+
return $"{Convert.ToBase64String(_encrypt.IV)}*{Convert.ToBase64String(buff)}";
56+
}
57+
58+
public string decryptIncoming(string IVCipher_Base64)
59+
{
60+
string[] parsed = parseBase64(IVCipher_Base64);
61+
string IV_Base64 = parsed[0];
62+
string Cipher_Base64 = parsed[1];
63+
byte[] IV = System.Convert.FromBase64String(IV_Base64);
64+
byte[] Cipher = System.Convert.FromBase64String(Cipher_Base64);
65+
_decrypt.IV = IV;
66+
67+
string output;
68+
using (var decryptor = _decrypt.CreateDecryptor()) {
69+
using (MemoryStream ms = new MemoryStream(Cipher)) {
70+
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) {
71+
using (StreamReader sr = new StreamReader(cs)) {
72+
output = sr.ReadToEnd();
73+
}
74+
}
75+
}
76+
}
77+
78+
79+
return output;
80+
}
81+
82+
public string[] parseBase64(string b64)
83+
{
84+
var sides = b64.Split("*");
85+
var IV = sides[0];
86+
var Cipher = sides[1];
87+
return new string[] {IV, Cipher};
5488
}
5589
}
5690
}

0 commit comments

Comments
 (0)