Код: Выделить всё
public string EncryptData(string stringToBeEncrypted, string publicKey)
{
try
{
RsaKeyParameters parameters1 = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
RSAParameters parameters21 = new RSAParameters
{
Modulus = parameters1.Modulus.ToByteArrayUnsigned(),
Exponent = parameters1.Exponent.ToByteArrayUnsigned()
};
RSACng cng1 = new RSACng();
cng1.ImportParameters(parameters21);
return Convert.ToBase64String(cng1.Encrypt(Encoding.UTF8.GetBytes(stringToBeEncrypted), RSAEncryptionPadding.OaepSHA256));
}
catch (Exception ex)
{
return "error";
}
}
Решение 2. Я использовал ECDsa, как показано ниже, но снова получаю сообщение об ошибке.
Код: Выделить всё
public string EncryptData(string stringToBeEncrypted, string publicKey)
{
try
{
var publicKeyBytes = Convert.FromBase64String(publicKey);
ECParameters parameters2 = new ECParameters
{
Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
Q = new System.Security.Cryptography.ECPoint
{
X = publicKeyBytes.Skip(1).Take(32).ToArray(),
Y = publicKeyBytes.Skip(33).ToArray()
},
};
ECDsa cng2 = ECDsa.Create();
cng2.ImportParameters(parameters2);
return Convert.ToBase64String(cng2.SignData(Encoding.UTF8.GetBytes(stringToBeEncrypted), HashAlgorithmName.SHA256));
} catch (Exception ex) { return "error"; } }
Необработанное исключение: System.PlatformNotSupportedException: криптография Windows следующего поколения (CNG) не поддерживается на этой платформе.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ey-in-maui