Repository navigation
Expand file tree
/
Copy pathCryptoHelper.cs
More file actions
109 lines (89 loc) · 3.99 KB
/
Copy pathCryptoHelper.cs
File metadata and controls
109 lines (89 loc) · 3.99 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
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace NETworkManager.Utilities
{
public static class CryptoHelper
{
/// <summary>
///
/// </summary>
/// <param name="decryptedBytes"></param>
/// <param name="password"></param>
/// <param name="keySize"></param>
/// <param name="blockSize"></param>
/// <param name="iterations"></param>
/// <returns></returns>
public static byte[] Encrypt(byte[] decryptedBytes, string password, int keySize, int blockSize, int iterations)
{
var salt = GenerateRandomEntropy(keySize / 8); // Generate salt based
var iv = GenerateRandomEntropy(blockSize / 8); // Generate iv, has to be the same as the block size
using var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, iterations);
var key = rfc2898DeriveBytes.GetBytes(keySize / 8);
using var rijndaelManaged = new RijndaelManaged
{
KeySize = keySize,
BlockSize = blockSize,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
using var encryptor = rijndaelManaged.CreateEncryptor(key, iv);
using var memoryStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(decryptedBytes, 0, decryptedBytes.Length);
cryptoStream.FlushFinalBlock();
var cipher = salt;
cipher = cipher.Concat(iv).ToArray();
cipher = cipher.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return cipher;
}
/// <summary>
///
/// </summary>
/// <param name="encryptedBytesWithSaltAndIV"></param>
/// <param name="password"></param>
/// <param name="keySize"></param>
/// <param name="blockSize"></param>
/// <param name="iterations"></param>
/// <returns></returns>
public static byte[] Decrypt(byte[] encryptedBytesWithSaltAndIV, string password, int keySize, int blockSize, int iterations)
{
var salt = encryptedBytesWithSaltAndIV.Take(keySize / 8).ToArray(); // Take salt bytes
var iv = encryptedBytesWithSaltAndIV.Skip(keySize / 8).Take(blockSize / 8).ToArray(); // Skip salt bytes, take iv bytes
var cipher = encryptedBytesWithSaltAndIV.Skip((keySize / 8) + (blockSize / 8)).Take(encryptedBytesWithSaltAndIV.Length - ((keySize / 8) + (blockSize / 8))).ToArray(); // Skip salt and iv bytes, take cipher bytes
using var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, iterations);
var key = rfc2898DeriveBytes.GetBytes(keySize / 8);
using var rijndaelManaged = new RijndaelManaged
{
KeySize = keySize,
BlockSize = blockSize,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
using var decryptor = rijndaelManaged.CreateDecryptor(key, iv);
using var memoryStream = new MemoryStream(cipher);
using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
var text = new byte[cipher.Length];
cryptoStream.Read(text, 0, text.Length);
memoryStream.Close();
cryptoStream.Close();
return text;
}
/// <summary>
///
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
private static byte[] GenerateRandomEntropy(int size)
{
var randomBytes = new byte[size];
using (var rngCryptoServiceProvider = new RNGCryptoServiceProvider())
{
rngCryptoServiceProvider.GetBytes(randomBytes);
}
return randomBytes;
}
}
}