-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringRSA.cs
43 lines (38 loc) · 1.27 KB
/
StringRSA.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using MetalWorker.Cryptography;
using System.Threading.Tasks;
namespace MetalWorker.Cryptography
{
/// <summary>
/// Encrypts and decrypts strings with the block RSA
/// </summary>
public class StringRSA
{
private static readonly Encoding encoding = new UTF8Encoding();
public static string Encrypt(string text, BlockRSA.Key<BigInteger> encKey)
{
return ByteArrayToString(BlockRSA.Encrypt(encoding.GetBytes(text), encKey));
}
public static string Decrypt(string text, BlockRSA.Key<BigInteger> decKey)
{
return encoding.GetString(BlockRSA.Decrypt(StringToByteArray(text), decKey));
}
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
}
}