-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathCypherRSA.cs
135 lines (126 loc) · 4.17 KB
/
CypherRSA.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
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
namespace CypherRSA;
// Max Lengh Support 379 Characters
public static class CypherRSA
{
public static void GenerateKeys(out byte[] publicKey, out byte[] privateKey)
{
try
{
using RSA rsaKey = RSA.Create(4096);
publicKey = rsaKey.ExportRSAPublicKey();
privateKey = rsaKey.ExportRSAPrivateKey();
rsaKey.Clear();
}
catch (Exception ex)
{
publicKey = Array.Empty<byte>();
privateKey = Array.Empty<byte>();
Debug.WriteLine("CypherTool GenerateKeys: " + ex.Message);
}
}
public static bool TryEncrypt(byte[] input, byte[] publicKey, out byte[] encryptedBytes, out string encryptedHex)
{
try
{
using RSA rsaKey = RSA.Create(4096);
rsaKey.ImportRSAPublicKey(publicKey, out _);
encryptedBytes = rsaKey.Encrypt(input, RSAEncryptionPadding.OaepSHA512);
encryptedHex = Convert.ToHexString(encryptedBytes);
rsaKey.Clear();
return true;
}
catch (Exception ex)
{
encryptedBytes = Array.Empty<byte>();
encryptedHex = string.Empty;
Debug.WriteLine("CypherTool TryEncrypt 1: " + ex.Message);
return false;
}
}
public static bool TryEncrypt(string inputText, byte[] publicKey, out byte[] encryptedBytes, out string encryptedHex)
{
try
{
byte[] inputBytes = Encoding.UTF8.GetBytes(inputText);
return TryEncrypt(inputBytes, publicKey, out encryptedBytes, out encryptedHex);
}
catch (Exception ex)
{
encryptedBytes = Array.Empty<byte>();
encryptedHex = string.Empty;
Debug.WriteLine("CypherTool TryEncrypt 2: " + ex.Message);
return false;
}
}
public static bool TryDecrypt(byte[] encryptedBytes, byte[] privateKey, out byte[] decryptedBytes)
{
try
{
using RSA rsaKey = RSA.Create(4096);
rsaKey.ImportRSAPrivateKey(privateKey, out _);
decryptedBytes = rsaKey.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA512);
rsaKey.Clear();
return true;
}
catch (Exception ex)
{
decryptedBytes = Array.Empty<byte>();
Debug.WriteLine("CypherTool TryDecrypt 1: " + ex.Message);
return false;
}
}
public static bool TryDecrypt(string encryptedHex, byte[] privateKey, out byte[] decryptedBytes)
{
try
{
byte[] encryptedBytes = Convert.FromHexString(encryptedHex);
return TryDecrypt(encryptedBytes, privateKey, out decryptedBytes);
}
catch (Exception ex)
{
decryptedBytes = Array.Empty<byte>();
Debug.WriteLine("CypherTool TryDecrypt 2: " + ex.Message);
return false;
}
}
public static bool TryDecrypt(byte[] encryptedBytes, byte[] privateKey, out string decryptedText)
{
bool isDecryptionSuccess = TryDecrypt(encryptedBytes, privateKey, out byte[] decryptedBytes);
if (isDecryptionSuccess)
{
try
{
decryptedText = Encoding.UTF8.GetString(decryptedBytes);
return true;
}
catch (Exception ex)
{
decryptedText = string.Empty;
Debug.WriteLine("CypherTool TryDecrypt 3: " + ex.Message);
return false;
}
}
else
{
decryptedText = string.Empty;
return false;
}
}
public static bool TryDecrypt(string encryptedHex, byte[] privateKey, out string decryptedText)
{
try
{
byte[] encryptedBytes = Convert.FromHexString(encryptedHex);
return TryDecrypt(encryptedBytes, privateKey, out decryptedText);
}
catch (Exception ex)
{
decryptedText = string.Empty;
Debug.WriteLine("CypherTool TryDecrypt 4: " + ex.Message);
return false;
}
}
}