You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
using RabbitMQ.Client;
|
|
using System.Reflection;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.Xml;
|
|
using System.Text;
|
|
|
|
namespace Expedience.Api.Encryption
|
|
{
|
|
public interface IDecryptor
|
|
{
|
|
string Decrypt(string data);
|
|
}
|
|
|
|
public class Decryptor : IDecryptor
|
|
{
|
|
private RSACryptoServiceProvider _csp;
|
|
|
|
public Decryptor()
|
|
{
|
|
_csp = new RSACryptoServiceProvider();
|
|
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Expedience.Api.key.bin")
|
|
?? throw new NullReferenceException("Could not get key.bin resource");
|
|
|
|
using var memorySteam = new MemoryStream();
|
|
stream.CopyTo(memorySteam);
|
|
var bytes = memorySteam.ToArray();
|
|
_csp.ImportCspBlob(bytes);
|
|
|
|
}
|
|
|
|
public string Decrypt(string data)
|
|
{
|
|
var encryptedData = Convert.FromBase64String(data);
|
|
|
|
// Split the encrypted data into chunks
|
|
byte[] decryptedData = Array.Empty<byte>();
|
|
for (int i = 0; i < encryptedData.Length; i += _csp.KeySize / 8)
|
|
{
|
|
int dataSize = _csp.KeySize / 8;
|
|
byte[] chunk = new byte[dataSize];
|
|
Array.Copy(encryptedData, i, chunk, 0, dataSize);
|
|
|
|
// Decrypt the chunk of data
|
|
byte[] decryptedChunk = _csp.Decrypt(chunk, false);
|
|
decryptedData = decryptedData.Concat(decryptedChunk).ToArray();
|
|
}
|
|
|
|
return Encoding.UTF8.GetString(decryptedData);
|
|
}
|
|
}
|
|
}
|