-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionService.cs
More file actions
143 lines (125 loc) · 4.78 KB
/
EncryptionService.cs
File metadata and controls
143 lines (125 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Security.Cryptography;
using System.Text;
namespace FileLocker
{
public class HashStore
{
public string Salt { get; set; }
public string PasswordHash { get; set; }
public string UID { get; set; }
/// <summary>
/// This is for storing Password Hashes, not for Encryption Salt.
/// Salt should be generated through BCrypt.Net.BCrypt.GenerateSalt(int length)
/// Hashes should be generated from BCrypt.Net.BCrypt.HashPassword(string password, string salt)
/// </summary>
/// <param name="uid"></param>
/// <param name="PHash"></param>
/// <param name="salt"></param>
public HashStore(string uid, string PHash, string salt)
{
UID = uid;
PasswordHash = PHash;
Salt = salt;
}
public override string ToString()
{
return $"UID: {UID} || Salt: {Salt} || PasswordHash: {PasswordHash}";
}
}
public static class EncryptionService
{
public static int GetHash(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = SHA256.HashData(bytes);
// Convert the hashBytes to an integer (you can choose a different method if needed)
int hash = BitConverter.ToInt32(hashBytes, 0);
return hash;
}
/// <summary>
/// Generates salt for AES Key and IV Generation
/// </summary>
/// <param name="SizeInBytes"></param>
/// <returns></returns>
public static byte[] GenSalt(int SizeInBytes)
{
byte[] salt = new byte[SizeInBytes];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
return salt;
}
/// <summary>
/// For AES-256, 16byte IV and 32 byte key
/// </summary>
/// <param name="password"></param>
/// <param name="salt"></param>
/// <param name="keySizeInBytes"></param>
/// <param name="ivSizeInBytes"></param>
/// <param name="iterationCount"></param>
/// <returns></returns>
public static (byte[], byte[]) GenerateKeyAndIV(string password, byte[] salt, int keySizeInBytes, int ivSizeInBytes, int iterationCount)
{
using (Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterationCount, HashAlgorithmName.SHA256))
{
byte[] key = pbkdf2.GetBytes(keySizeInBytes);
byte[] iv = pbkdf2.GetBytes(ivSizeInBytes);
return (key, iv);
}
}
/// <summary>
/// Encrypts a string into a byte[] using AES
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
return msEncrypt.ToArray();
}
}
}
/// <summary>
/// Decrypts an AES encrypted byte[] into a string
/// </summary>
/// <param name="encryptedData"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string Decrypt(byte[] encryptedData, byte[] key, byte[] iv)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
}
}