MAUI 或 .net 标准中的 Windows.Security.Cryptography.CryptographicBuffer.DecodeFromBase64String 替换

发布于 2025-01-11 05:30:06 字数 1258 浏览 0 评论 0原文

我正在尝试将 UWP 应用程序转换为 MAUI。 尝试转换以下函数。 MAUI(.net6 或 .net 标准)中的 Windows.Security.Cryptography.CryptographicBuffer.DecodeFromBase64String(publicKey) 等效项是什么。

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-01-18 05:30:06

找到解决方案

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文