-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.Crypt.cs
More file actions
90 lines (73 loc) · 3.88 KB
/
Core.Crypt.cs
File metadata and controls
90 lines (73 loc) · 3.88 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
using System.Security.Cryptography;
using System.Text;
namespace M9Studio.ShadowTalk.Client
{
public partial class Core
{
public static string DecryptWithRSA(string base64EncryptedText, string privateKeyXml)
{
using var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
rsa.FromXmlString(privateKeyXml);
byte[] encryptedBytes = Convert.FromBase64String(base64EncryptedText);
byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, false);
return System.Text.Encoding.UTF8.GetString(decryptedBytes);
}
public static string DecryptAesBase64(string base64CipherText, string base64Key)
{
byte[] cipherBytes = Convert.FromBase64String(base64CipherText);
byte[] keyBytes = Convert.FromBase64String(base64Key);
// IV берём из начала сообщения (если он туда добавлен), либо задаём явно:
byte[] iv = new byte[16]; // Можно использовать фиксированный IV или получать отдельно
Array.Copy(cipherBytes, 0, iv, 0, 16);
byte[] actualCipher = new byte[cipherBytes.Length - 16];
Array.Copy(cipherBytes, 16, actualCipher, 0, actualCipher.Length);
using var aes = Aes.Create();
aes.Key = keyBytes;
aes.IV = iv;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using var decryptor = aes.CreateDecryptor();
byte[] decryptedBytes = decryptor.TransformFinalBlock(actualCipher, 0, actualCipher.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
public static string EncryptWithRSA(string plainText, string publicKeyXml)
{
using var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
rsa.FromXmlString(publicKeyXml);
byte[] dataBytes = Encoding.UTF8.GetBytes(plainText);
byte[] encryptedBytes = rsa.Encrypt(dataBytes, false); // false = PKCS#1 v1.5 padding
return Convert.ToBase64String(encryptedBytes);
}
public static string EncryptAesBase64(string plainText, string base64Key)
{
byte[] keyBytes = Convert.FromBase64String(base64Key);
byte[] iv = RandomNumberGenerator.GetBytes(16); // 16 байт IV
using var aes = Aes.Create();
aes.Key = keyBytes;
aes.IV = iv;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using var encryptor = aes.CreateEncryptor();
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] cipherBytes = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
// Префикс IV к шифртексту (как ты предполагаешь в DecryptAesBase64)
byte[] result = new byte[iv.Length + cipherBytes.Length];
Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
Buffer.BlockCopy(cipherBytes, 0, result, iv.Length, cipherBytes.Length);
return Convert.ToBase64String(result);
}
public static (string publicKey, string privateKey) GenRSA()
{
using var rsa = new System.Security.Cryptography.RSACryptoServiceProvider(2048);
string publicKey = rsa.ToXmlString(false); // только публичный ключ
string privateKey = rsa.ToXmlString(true); // полный ключ (приватный + публичный)
return (publicKey, privateKey);
}
public static string GenAesKey(int keySizeBits = 256)
{
int keySizeBytes = keySizeBits / 8;
byte[] key = RandomNumberGenerator.GetBytes(keySizeBytes);
return Convert.ToBase64String(key);
}
}
}