forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypt.cs
145 lines (131 loc) · 5.79 KB
/
Encrypt.cs
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
144
145
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
namespace DBADash
{
public static class EncryptText
{
// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
private const string initVector = "pemgail9uzpgzl88";
// This constant is used to determine the keysize of the encryption algorithm
private const int keysize = 256;
//Encrypt
public static string EncryptString(this string plainText, string passPhrase)
{
var initVectorBytes = Encoding.UTF8.GetBytes(initVector);
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new(passPhrase, null))
{
var keyBytes = password.GetBytes(keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
//Decrypt
public static string DecryptString(this string cipherText, string passPhrase)
{
var initVectorBytes = Encoding.UTF8.GetBytes(initVector);
var cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new(passPhrase, null))
{
var keyBytes = password.GetBytes(keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
using (MemoryStream memoryStream = new(cipherTextBytes))
using (CryptoStream cryptoStream = new(memoryStream, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new(cryptoStream))
{
return srDecrypt.ReadToEnd();
}
}
}
}
[SupportedOSPlatform("windows")]
public static string EncryptString(this string value, DataProtectionScope scope)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (string.IsNullOrEmpty(value)) return value;
var data = Encoding.Unicode.GetBytes(value);
var encryptedData = ProtectedData.Protect(data, null, scope);
return Convert.ToBase64String(encryptedData);
}
else
{
return value;
}
}
[SupportedOSPlatform("windows")]
public static string DecryptString(this string value, DataProtectionScope scope)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (string.IsNullOrEmpty(value)) return value;
var encryptedData = Convert.FromBase64String(value);
var data = ProtectedData.Unprotect(encryptedData, null, scope);
return Encoding.Unicode.GetString(data);
}
else
{
return value;
}
}
[SupportedOSPlatform("windows")]
public static string UserEncryptString(this string value)
{
return EncryptString(value, DataProtectionScope.CurrentUser);
}
[SupportedOSPlatform("windows")]
public static string UserDecryptString(this string value)
{
return DecryptString(value, DataProtectionScope.CurrentUser);
}
[SupportedOSPlatform("windows")]
public static string MachineEncryptString(this string value)
{
return EncryptString(value, DataProtectionScope.LocalMachine);
}
[SupportedOSPlatform("windows")]
public static string MachineDecryptString(this string value)
{
return DecryptString(value, DataProtectionScope.LocalMachine);
}
public static string GetHash(string input)=>GetShortHash(input, 64);
public static string GetShortHash(string input, int length = 8)
{
if (length is < 0 or > 64) // SHA256 produces a 64-character hex string
{
throw new ArgumentOutOfRangeException(nameof(length), @"Length must be between 0 and 64.");
}
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(input));
var builder = new StringBuilder(length); // Initialize StringBuilder with the exact length needed
for (var i = 0; i < bytes.Length && builder.Length < length; i++)
{
// Append each byte as a two-character hexadecimal string
builder.Append(bytes[i].ToString("x2"));
if (builder.Length > length) // If appending the last byte exceeds the desired length
{
builder.Length = length; // Truncate the StringBuilder to the desired length
}
}
return builder.ToString();
}
}
}