使用非对称密钥在UWP中编码/解码

时间:2016-11-21 18:22:07

标签: c# uwp win-universal-app .net-core windows-10-universal

我正在尝试找出一种使用私钥加密字符串的方法,并使用公共密钥对其进行解密。我使用OpenSSL生成密钥,如下所述:

http://www.akadia.com/services/ssh_test_certificate.html

这就是我目前的

public static string Encrypt(string str, string key)
{
    try
    {
        key = key.Replace(Environment.NewLine, "");
        IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(key);

        AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);

        CryptographicKey publicKey = provider.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);
        IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(str));
        var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);

        return CryptographicBuffer.EncodeToBase64String(encryptedData);
    }
    catch (Exception e)
    {
        throw;
        return "Error in Encryption:With RSA ";
    }
}

然而,在ImportPublicKey方法中,我得到的是ASN1 corrupted data

传递给该方法的字符串key具有以下格式:

var privateKey =

@“MIICXwIBAAKBgQDUTqfSknFiQx3aepORHJycWck007cfU4fXluTIyf6U9ipDhyPD    ....    yDxwZVmexltyK5Bwc26lmb + 5EtTEic + kZToYWcCucF8lsok =“;

所以OpenSSL的内容生成了没有这部分的密钥文件:

-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----

1 个答案:

答案 0 :(得分:1)

好的,所以我的代码非常错误,因为我想用私钥加密,但是使用ImportPublicKey函数,应该是正确的方法;

public static string Encrypt(string str, string key)
{
    try
    {
        key = key.Replace(Environment.NewLine, "");
        IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(key);

        AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);

        var keyPar = provider.ImportKeyPair(keyBuffer, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
        //CryptographicKey publicKey = provider.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);
        IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(str));
        var encryptedData = CryptographicEngine.Encrypt(keyPar, dataBuffer, null);
        var encryptedStr = CryptographicBuffer.EncodeToBase64String(encryptedData);
        var signature = CryptographicEngine.Sign(keyPar, dataBuffer);
        var signatureStr = CryptographicBuffer.EncodeToBase64String(signature);
        return encryptedStr;
    }
    catch (Exception e)
    {
        throw;
        return "Error in Encryption:With RSA ";
    }
}

这适用于使用RSA私钥加密字符串。

但是,当我尝试使用公钥解密时,使用以下类似的方法;

public static string Decrypt(string str, string key)
{
    try
    {
        key = key.Replace(Environment.NewLine, "");
        IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(key);

        AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaSignPkcs1Sha256);

        CryptographicKey publicKey = provider.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
        IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(str));
        var encryptedData = CryptographicEngine.Decrypt(publicKey, dataBuffer, null);
        return CryptographicBuffer.EncodeToBase64String(encryptedData);
    }
    catch (Exception e)
    {
        throw;
        return "Error in Decryption:With RSA ";
    }
}

我收到了Method or operation not implemented异常,要么仍有问题,要么UWP中还没有private-encrypt / public-decrypt方法。

我最终要做的是获取nuget包Portable.BouncyCastle-Signed并按照此答案中的代码段进行操作:

C# BouncyCastle - RSA Encryption with Public/Private keys

像沙姆一样工作。