RSA加密在Metro风格中的应用 [英] RSA Encryption in metro style Application

查看:74
本文介绍了RSA加密在Metro风格中的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有公共密钥模数和公共密钥指数,我需要生成一个公共密钥并在Metro风格的应用程序中加密数据.在c#中,我们具有RSAParameters类,但是对于Metro风格的应用程序,我找不到任何此类的东西.

I have public key modulus and public key exponent and I need to generate a public key and encrypt data in metro style application. in c# we have RSAParameters class but I cannot find any thing of such sort for metro style applications.

当我使用直接从证书中接收到的base64编码公共密钥,并尝试使用以下代码导入密钥时,遇到了抛出ASN1错误标记值的异常. 我认为这是由于数据格式无效造成的.

when I use the base64encoded public key directly received from the certificate and try to import the key with the below code I get an exception thrown ASN1 bad tag value met. which i think is due to the invalid format of the data.

     //sample dummy key from certificate in base64encoded
     string key =   @"MIIB0zCCAX2gAwIBAgIJAMF/bHcA799IMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTIwMzI3MTEyNjQ5WhcNMTMwMzI3MTEyNjQ5WjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMEPeWjP4sdqxvBlDId4BtRRTeWPwjlZLSOFvOVgmoSyoPva8psFUF6tH9/vPXIJrL80tdCoBt8YFH6pwDN9a1sCAwEAAaNQME4wHQYDVR0OBBYEFGARqQfUhX7atVU4sS+aQAPt/jFxMB8GA1UdIwQYMBaAFGARqQfUhX7atVU4sS+aQAPt/jFxMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADQQALqOyjovRbWUZvziVaE8QYy83WEln1l+HJU9D6tFncUZTlwSd8aUwyQsd3zOVNZ41oCAVv5R3h1jtBtPbM+c1K";
       symmetricKeyAlgorithmProvider asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSA_OAEP_SHA1");
       CryptographicKey publicKey = asymmAlg.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(key));
        string input64string ="encrypt this";
        IBuffer dataToEncrypt = CryptographicBuffer.DecodeFromBase64String(input64string);
        IBuffer encryptedData = CryptographicEngine.Encrypt(publicKey, dataToEncrypt, null);

推荐答案

您的key是公用密钥的模数部分,其长度为128.考虑从Metro中的C#代码导出的标准密钥:

Your key is the modulus part of the public key, whose length is 128. Consider the standard key exported from C# code in metro:

using System.Runtime.InteropServices.WindowsRuntime;
CryptographicKey standardKeyPair = provider.CreateKeyPair(1024);
byte[] standardKey = standardKeyPair.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey).ToArray();

您可以看到byte [] standardKey的长度为140,它的前缀是7位,尾部是5位.我不知道原因,但是我将多余的12位复制到了已知的密钥上,它可以正常工作.希望这对您有所帮助:

you can see the length of the byte[] standardKey is 140,which has 7 bits prefix and 5 bits tail. I don't know the reason, but I copied the extra 12 bits to the known key, it works. Hope this helps you:

public static IBuffer RsaEncrypt(this IBuffer dataToEncrypt, string publicKeyN)
{
    AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);

    CryptographicKey standardKeyPair = provider.CreateKeyPair(1024);
    byte[] standardKey = standardKeyPair.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey).ToArray();
    var data_n = CryptographicBuffer.DecodeFromBase64String(publicKeyN).ToArray();
    Array.Copy(data_n, 0, standardKey, 7, data_n.Length);
    var key = provider.ImportPublicKey(standardKey.AsBuffer(), CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);

    var result = CryptographicEngine.Encrypt(key, dataToEncrypt, null);
    return result;          
}

这篇关于RSA加密在Metro风格中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆