-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.cs
More file actions
150 lines (139 loc) · 5.65 KB
/
Encryption.cs
File metadata and controls
150 lines (139 loc) · 5.65 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
144
145
146
147
148
149
150
using AuthSharp.SDK.Classes;
using AuthSharp.SDK.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace AuthSharp.SDK
{
/// <summary>
/// Internal class providing encryption, decryption, and hashing utilities.
/// </summary>
internal static class Encryption
{
/// <summary>
/// Creates a new symmetric key pair for AES encryption.
/// </summary>
/// <returns>A SymmetricKeyPair containing a randomly generated key and IV.</returns>
public static SymmetricKeyPair CreateSymmetricKey()
{
using (var aes = Aes.Create())
{
aes.GenerateKey();
aes.GenerateIV();
return new SymmetricKeyPair()
{
Key = aes.Key,
IV = aes.IV
};
}
}
/// <summary>
/// Calculates the SHA1 hash of a byte array.
/// </summary>
/// <param name="b">The byte array to hash.</param>
/// <returns>The SHA1 hash as a lowercase hexadecimal string.</returns>
public static string CalculateSHA1(byte[] b)
{
using (var algorithm = SHA1.Create())
{
var hash = algorithm.ComputeHash(b);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
/// <summary>
/// Calculates the SHA1 hash of a string.
/// </summary>
/// <param name="s">The string to hash.</param>
/// <returns>The SHA1 hash as a lowercase hexadecimal string.</returns>
public static string CalculateSHA1(string s)
{
return CalculateSHA1(Encoding.UTF8.GetBytes(s));
}
/// <summary>
/// Calculates the SHA256 hash of a byte array.
/// </summary>
/// <param name="b">The byte array to hash.</param>
/// <returns>The SHA256 hash as a lowercase hexadecimal string.</returns>
public static string CalculateSHA256(byte[] b)
{
using (var algorithm = SHA256.Create())
{
var hash = algorithm.ComputeHash(b);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
/// <summary>
/// Calculates the SHA256 hash of a string.
/// </summary>
/// <param name="s">The string to hash.</param>
/// <returns>The SHA256 hash as a lowercase hexadecimal string.</returns>
public static string CalculateSHA256(string s)
{
return CalculateSHA256(Encoding.UTF8.GetBytes(s));
}
/// <summary>
/// Encrypts data using RSA encryption with OAEP SHA1 padding.
/// </summary>
/// <param name="data">The data to encrypt.</param>
/// <param name="publicKey">The RSA public key in Subject Public Key Info format.</param>
/// <returns>The encrypted data as a byte array.</returns>
public static byte[] RSA_Encrypt(byte[] data, byte[] publicKey)
{
var rsa = Utils.CreateRsaFromSubjectPublicKeyInfo(publicKey);
var encryptedData = rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA1);
return encryptedData;
}
/// <summary>
/// Encrypts a string using RSA encryption with OAEP SHA1 padding.
/// </summary>
/// <param name="data">The string to encrypt.</param>
/// <param name="publicKey">The RSA public key in Subject Public Key Info format.</param>
/// <returns>The encrypted data as a byte array.</returns>
public static byte[] RSA_Encrypt(string data, byte[] publicKey)
{
var rsa = Utils.CreateRsaFromSubjectPublicKeyInfo(publicKey);
var dataBytes = Encoding.UTF8.GetBytes(data);
var encryptedData = rsa.Encrypt(dataBytes, RSAEncryptionPadding.OaepSHA1);
return encryptedData;
}
/// <summary>
/// Encrypts data using AES encryption in CBC mode with PKCS7 padding.
/// </summary>
/// <param name="b">The data to encrypt.</param>
/// <param name="keyPair">The symmetric key pair containing the key and IV.</param>
/// <returns>The encrypted data as a byte array.</returns>
public static byte[] Aes_Encrypt(byte[] b, SymmetricKeyPair keyPair)
{
using (var algorithm = Aes.Create())
{
algorithm.Key = keyPair.Key;
algorithm.IV = keyPair.IV;
algorithm.Mode = CipherMode.CBC;
algorithm.Padding = PaddingMode.PKCS7;
var encrypted = algorithm.CreateEncryptor().TransformFinalBlock(b, 0, b.Length);
return encrypted;
}
}
/// <summary>
/// Decrypts data using AES decryption in CBC mode with PKCS7 padding.
/// </summary>
/// <param name="b">The data to decrypt.</param>
/// <param name="keyPair">The symmetric key pair containing the key and IV.</param>
/// <returns>The decrypted data as a byte array.</returns>
public static byte[] Aes_Decrypt(byte[] b, SymmetricKeyPair keyPair)
{
using (var algorithm = Aes.Create())
{
algorithm.Key = keyPair.Key;
algorithm.IV = keyPair.IV;
algorithm.Mode = CipherMode.CBC;
algorithm.Padding = PaddingMode.PKCS7;
var decrypted = algorithm.CreateDecryptor().TransformFinalBlock(b, 0, b.Length);
return decrypted;
}
}
}
}