JSFiddle - React, Tailwind, and code Playground
HTML
public static string EncryptData(String stringToBeEncrypted, string
publicKey)
{
try
{
//var pem = "-----BEGIN PUBLIC KEY-----\n" + publicKey + "\n-
----END PUBLIC KEY-----"; // Add header and foote
AsymmetricKeyParameter asymmetricKeyParameter =
PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
RsaKeyParameters rsaKeyParameters =
(RsaKeyParameters)asymmetricKeyParameter;
RSAParameters rsaParameters = new RSAParameters();
rsaParameters.Modulus =
rsaKeyParameters.Modulus.ToByteArrayUnsigned();
rsaParameters.Exponent =
rsaKeyParameters.Exponent.ToByteArrayUnsigned();
RSACng rsa = new RSACng();
rsa.ImportParameters(rsaParameters);
string base64 =
Convert.ToBase64String(rsa.Encrypt(Encoding.UTF8.GetBytes(stringToBeEncrypted
), RSAEncryptionPadding.OaepSHA256));
if (base64.Length % 4 == 3 )
{
base64 += "=";
} else if (base64.Length % 4 == 2)
{
base64 += "==";
}
return base64;
}
catch (Exception e)
{
return "error";
}
}