'Windows.Security.Cryptography.CryptographicBuffer.DecodeFromBase64String replacement in MAUI or .net standard

I am trying to convert UWP application to MAUI. Trying to convert following function. What is the equivalent of Windows.Security.Cryptography.CryptographicBuffer.DecodeFromBase64String(publicKey) in MAUI (.net6 or .net standard).

public static string EncryptUsingAsymmetricPublicKey(string data)
{
    string publicKey = "MIGJAoGBAJTxJ6xRdgcPXi4/0KK73FBDuI5VGZnIi7IblIY/wxcuGagBEvzlYEqJaWVO3l5aTbnnB63DU5eBlR0nnzUC+UM6b/PBmFGUyDaXYK6msThvvxAqj32UjLvYyq5S+Z57Y/cv/0vokOfTdEyTMw+WTbJh7c2npj9IAFnc32Z5PknVAgMBAAE=";
    
    //converting the public key into an IBuffer
    IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(publicKey);

    //load the public key and the algorithm provider
    var asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);

    var cryptoKey = asym.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);

    //converting the string into an IBuffer
    IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(data, BinaryStringEncoding.Utf8);

    //perform the encryption
    var encryptedBytesBuffer = CryptographicEngine.Encrypt(cryptoKey, buffer, null);

    //convert to base 64
    return CryptographicBuffer.EncodeToBase64String(encryptedBytesBuffer);
}


Solution 1:[1]

Found solution

public static string EncryptUsingAsymmetricPublicKey(string data)
{
    byte[] publicKeyBytes = Convert.FromBase64String(publicKey);
    using (var rsa = new RSACryptoServiceProvider(1024))
    {
        rsa.ImportRSAPublicKey(publicKeyBytes, out int bytesRead);
        var encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false);

        return Convert.ToBase64String(encryptedData);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vinod Shinde