Skip to content

Commit af0538b

Browse files
committed
1.1.3 encrypt passwords
1 parent f56a284 commit af0538b

9 files changed

Lines changed: 194 additions & 92 deletions

File tree

changelog.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,27 @@ Changes to 1.1.0, released 1 July 2021.
1515
[Sql node version 3.34.8](https://github.com/tradecontrol/sqlnode/releases)
1616

1717
- Fix Web.Mail.MailInvoice.Send() - not filtering by selected email address
18-
- Mobile event log delete button permissions
18+
- Mobile event log delete button permissions
19+
20+
## 1.1.3
21+
22+
[Sql node version 3.34.8](https://github.com/tradecontrol/sqlnode/releases)
23+
24+
Some web hosting services do not support Sql Server security. This upgrade protects mail hosting service credentials by [encrypting passwords](src/TCWeb/Mail/Encrypt.cs) in the data store. Because the app is Open Source, you need to change [the key and vector](src/TCWeb/Data/NodeSettings.cs) bytes prior to compilation.
25+
26+
``` csharp
27+
/// <summary>
28+
/// Modify key bytes to protect passwords in an unsecured Sql Server context
29+
/// </summary>
30+
public static byte[] SymmetricKey
31+
{
32+
get
33+
{
34+
byte[] key = { 0x22, 0x5C, 0x53, 0x4B, 0x44, 0x2D, 0x6B, 0x6D, 0x51, 0xC, 0x58, 0x69, 0x4C, 0x56, 0x72, 0x15 };
35+
return key;
36+
}
37+
}
38+
```
39+
40+
41+

src/TCWeb/Data/Invoices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public async Task SetToPrinted()
243243
if (invoice != null)
244244
{
245245
invoice.Spooled = false;
246-
invoice.Printed = false;
246+
invoice.Printed = true;
247247
_context.Attach(invoice).State = EntityState.Modified;
248248
await _context.SaveChangesAsync();
249249
}

src/TCWeb/Data/NodeSettings.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public bool HasMailHost
7676
}
7777
}
7878
}
79-
#endregion
8079

8180
public Task<double> DataVersion => Task.Run(() =>
8281
{
@@ -120,6 +119,32 @@ public bool HasMailHost
120119
}
121120

122121
});
122+
#endregion
123+
124+
#region mail
125+
/// <summary>
126+
/// Modify key bytes to protect passwords in an unsecured Sql Server context
127+
/// </summary>
128+
public static byte[] SymmetricKey
129+
{
130+
get
131+
{
132+
byte[] key = { 0x22, 0x5C, 0x53, 0x4B, 0x44, 0x2D, 0x6B, 0x6D, 0x51, 0xC, 0x58, 0x69, 0x4C, 0x56, 0x72, 0x15 };
133+
return key;
134+
}
135+
}
136+
137+
/// <summary>
138+
/// Modify vector bytes to protect passwords in an unsecured Sql Server context
139+
/// </summary>
140+
public static byte[] SymmetricVector
141+
{
142+
get
143+
{
144+
byte[] iv = { 0x5C, 0x6B, 0xF, 0x1A, 0x5A, 0x70, 0x74, 0x71, 0x2A, 0x79, 0x14, 0x56, 0x6A, 0x77, 0x9, 0x22 };
145+
return iv;
146+
}
147+
}
123148

124149
public async Task<bool> SetHost(int? hostId)
125150
{
@@ -152,6 +177,7 @@ public async Task<MailSettings> MailHost()
152177
{
153178
try
154179
{
180+
Encrypt encrypt = new Encrypt(NodeSettings.SymmetricKey, NodeSettings.SymmetricVector);
155181
var defaultHost = await _context.App_Host.OrderBy(h => h.HostId).SingleOrDefaultAsync();
156182

157183
if (defaultHost == null)
@@ -161,7 +187,7 @@ public async Task<MailSettings> MailHost()
161187
{
162188
HostName = defaultHost.HostName,
163189
UserName = defaultHost.EmailAddress,
164-
Password = defaultHost.EmailPassword,
190+
Password = encrypt.DecryptString(defaultHost.EmailPassword),
165191
Port = defaultHost.HostPort
166192
};
167193
}
@@ -171,10 +197,7 @@ public async Task<MailSettings> MailHost()
171197
return null;
172198
}
173199
}
200+
#endregion
174201

175-
public void InitialiseNode()
176-
{
177-
178-
}
179202
}
180203
}

src/TCWeb/Mail/Encrypt.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System.Text;
2+
using System.Security.Cryptography;
3+
using System.IO;
4+
5+
namespace TradeControl.Web.Mail
6+
{
7+
/// <summary>
8+
/// Encrypts or decrypts strings according to passed keys/vectors using the RijndaelManaged algorithm
9+
/// </summary>
10+
public class Encrypt
11+
{
12+
readonly byte[] key;
13+
readonly byte[] iv;
14+
15+
public Encrypt(byte[] _key, byte[] _iv)
16+
{
17+
key = _key;
18+
iv = _iv;
19+
}
20+
21+
public Encrypt(byte[] _fullKey)
22+
{
23+
key = new byte[16];
24+
iv = new byte[16];
25+
26+
for (int i = 0; i < 16; i++)
27+
{
28+
key[i] = _fullKey[i];
29+
iv[i + 16] = _fullKey[i + 16];
30+
}
31+
32+
}
33+
34+
private byte[] ToByte(char[] _chars)
35+
{
36+
byte[] bytes = new byte[_chars.Length];
37+
for (int i = 0; i < _chars.Length; i++)
38+
bytes[i] = (byte)_chars[i];
39+
return bytes;
40+
}
41+
42+
private string ByteToString(byte[] _bytes)
43+
{
44+
string result = string.Empty;
45+
for (int i = 0; i < _bytes.Length; i++)
46+
result = result + (char)_bytes[i];
47+
48+
return result;
49+
}
50+
51+
#region Encryption
52+
private byte[] Key
53+
{
54+
get
55+
{
56+
return key;
57+
}
58+
}
59+
60+
private byte[] IV
61+
{
62+
get
63+
{
64+
return iv;
65+
}
66+
}
67+
68+
public string DecryptString(string _encrypted)
69+
{
70+
string decrypt;
71+
72+
try
73+
{
74+
ASCIIEncoding textConverter = new();
75+
byte[] encrypted = ToByte(_encrypted.ToCharArray());
76+
77+
RijndaelManaged RMCrypto = new();
78+
byte[] fromEncrypt;
79+
80+
ICryptoTransform decryptor = RMCrypto.CreateDecryptor(Key, IV);
81+
82+
MemoryStream msDecrypt = new(encrypted);
83+
CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);
84+
85+
fromEncrypt = new byte[encrypted.Length];
86+
87+
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
88+
89+
decrypt = textConverter.GetString(fromEncrypt).Trim(new char[] { ' ', '\0' });
90+
}
91+
catch
92+
{
93+
94+
decrypt = string.Empty;
95+
}
96+
97+
return decrypt;
98+
}
99+
100+
public string EncryptString(string _decrypted)
101+
{
102+
try
103+
{
104+
ASCIIEncoding textConverter = new();
105+
RijndaelManaged RMCrypto = new();
106+
107+
byte[] toEncrypt;
108+
109+
ICryptoTransform encryptor = RMCrypto.CreateEncryptor(Key, IV);
110+
111+
MemoryStream msEncrypt = new();
112+
CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write);
113+
114+
toEncrypt = textConverter.GetBytes(_decrypted);
115+
116+
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
117+
csEncrypt.FlushFinalBlock();
118+
119+
byte[] encrypted = msEncrypt.ToArray();
120+
121+
return ByteToString(encrypted) ;
122+
}
123+
catch
124+
{
125+
return string.Empty;
126+
}
127+
}
128+
#endregion
129+
}
130+
}

src/TCWeb/Pages/Admin/Host/Create.cshtml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using TradeControl.Web.Areas.Identity.Data;
1414
using TradeControl.Web.Data;
1515
using TradeControl.Web.Models;
16+
using TradeControl.Web.Mail;
1617

1718
namespace TradeControl.Web.Pages.Admin.Host
1819
{
@@ -61,7 +62,11 @@ public async Task<IActionResult> OnPostAsync()
6162
if (!ModelState.IsValid)
6263
return Page();
6364

65+
Encrypt encrypt = new(NodeSettings.SymmetricKey, NodeSettings.SymmetricVector);
66+
App_tbHost.EmailPassword = encrypt.EncryptString(App_tbHost.EmailPassword);
67+
6468
NodeContext.App_tbHosts.Add(App_tbHost);
69+
6570
await NodeContext.SaveChangesAsync();
6671

6772
if (await NodeContext.App_tbHosts.AnyAsync())

src/TCWeb/Pages/Admin/Host/Edit.cshtml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.EntityFrameworkCore;
88
using TradeControl.Web.Areas.Identity.Data;
99
using TradeControl.Web.Data;
10+
using TradeControl.Web.Mail;
1011
using TradeControl.Web.Models;
1112

1213
namespace TradeControl.Web.Pages.Admin.Host
@@ -59,6 +60,9 @@ public async Task<IActionResult> OnPostAsync()
5960
Profile profile = new(NodeContext);
6061
App_tbHost.InsertedBy = await profile.UserName(UserManager.GetUserId(User));
6162
App_tbHost.InsertedOn = DateTime.Now;
63+
64+
Encrypt encrypt = new(NodeSettings.SymmetricKey, NodeSettings.SymmetricVector);
65+
App_tbHost.EmailPassword = encrypt.EncryptString(App_tbHost.EmailPassword);
6266

6367
NodeContext.Attach(App_tbHost).State = EntityState.Modified;
6468

src/TCWeb/Pages/Invoice/Update/EmailSubmit.cshtml

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

src/TCWeb/Pages/Invoice/Update/EmailSubmit.cshtml.cs

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

src/TCWeb/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"MaxFileSize": 2097152,
1818
"SupportRequestTemplate": "support_request.html",
1919
"SupportEmailAddress": "office@tradecontrol.co.uk",
20-
"WebVersion": "1.1.2",
20+
"WebVersion": "1.1.3",
2121
"SqlNodeVersion": "3.34.8"
2222
}
2323
}

0 commit comments

Comments
 (0)