95 lines
3.6 KiB
C#
95 lines
3.6 KiB
C#
// ***********************************************************************
|
|
// <copyright file="RsaHelper.cs">
|
|
// Heath
|
|
// </copyright>
|
|
// ***********************************************************************
|
|
using Org.BouncyCastle.Crypto;
|
|
using Org.BouncyCastle.Crypto.Parameters;
|
|
using Org.BouncyCastle.OpenSsl;
|
|
using Org.BouncyCastle.Security;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Core.Cerberos.Adapters.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Handles all methods related to RSA encryption"/>.
|
|
/// </summary>
|
|
public class RsaHelper
|
|
{
|
|
private readonly RSACryptoServiceProvider _privateKey;
|
|
private readonly RSACryptoServiceProvider _publicKey;
|
|
private readonly string keysFolder = "Keys\\";
|
|
private readonly string exeDirectory = AppContext.BaseDirectory;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of <see cref="RsaHelper"/>.
|
|
/// </summary>
|
|
public RsaHelper()
|
|
{
|
|
exeDirectory = exeDirectory + keysFolder;
|
|
|
|
_publicKey = GetPublicKeyFromPemFile();
|
|
_privateKey = GetPrivateKeyFromPemFile();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Encrypts a text using RSA algorithm.
|
|
/// </summary>
|
|
/// <param name="text">The text to be encrypted.</param>
|
|
/// <returns>The encrypted text.</returns>
|
|
public string Encrypt(string text)
|
|
{
|
|
byte[] dataBytes = Encoding.UTF8.GetBytes(text);
|
|
var encryptedBytes = _publicKey.Encrypt(Encoding.UTF8.GetBytes(text), true);
|
|
return Convert.ToBase64String(encryptedBytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decrypts a text using RSA algorithm.
|
|
/// </summary>
|
|
/// <param name="text">The encrypted text to be decrypted.</param>
|
|
/// <returns>The decrypted text.</returns>
|
|
public string Decrypt(string encrypted)
|
|
{
|
|
var decryptedBytes = _privateKey.Decrypt(Convert.FromBase64String(encrypted), true);
|
|
return Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);
|
|
}
|
|
|
|
/// <summary>
|
|
///Obtains the private key from a file.
|
|
/// </summary>
|
|
/// <returns>The private key.</returns>
|
|
private RSACryptoServiceProvider GetPrivateKeyFromPemFile()
|
|
{
|
|
using (TextReader privateKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPrivateKey.pem"))))
|
|
{
|
|
AsymmetricCipherKeyPair readKeyPair = (AsymmetricCipherKeyPair)new PemReader(privateKeyTextReader).ReadObject();
|
|
|
|
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)readKeyPair.Private);
|
|
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
|
|
csp.ImportParameters(rsaParams);
|
|
return csp;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///Obtains the public key from a file.
|
|
/// </summary>
|
|
/// <returns>The public key.</returns>
|
|
public RSACryptoServiceProvider GetPublicKeyFromPemFile()
|
|
{
|
|
using (TextReader publicKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPublicKey.pem"))))
|
|
{
|
|
RsaKeyParameters publicKeyParam = (RsaKeyParameters)new PemReader(publicKeyTextReader).ReadObject();
|
|
|
|
RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKeyParam);
|
|
|
|
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
|
|
csp.ImportParameters(rsaParams);
|
|
return csp;
|
|
}
|
|
}
|
|
}
|
|
}
|