Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wcf Windows 8 Metro RSA加密:AsymmetricKeyAlgorithmProvider ImportPublicKey失败_Wcf_Microsoft Metro_Rsa_Public Key Encryption - Fatal编程技术网

Wcf Windows 8 Metro RSA加密:AsymmetricKeyAlgorithmProvider ImportPublicKey失败

Wcf Windows 8 Metro RSA加密:AsymmetricKeyAlgorithmProvider ImportPublicKey失败,wcf,microsoft-metro,rsa,public-key-encryption,Wcf,Microsoft Metro,Rsa,Public Key Encryption,我试图在Win 8 Metro应用程序和RESTful WCF服务之间传递一些加密数据。最初,Metro应用程序请求公钥,WCF服务将其作为原始流返回,以避免任何讨厌的格式问题。基本64编码公钥在metro应用程序中解码为字节数组。这就是问题所在。当我试图调用AsymmetricKeyAlgorithmProvider.ImportPublicKey时,我得到了错误“ASN1 bad tag value met” 我使用RSA PKCS1进行加密。以下是相关代码: WCF服务 stri

我试图在Win 8 Metro应用程序和RESTful WCF服务之间传递一些加密数据。最初,Metro应用程序请求公钥,WCF服务将其作为原始流返回,以避免任何讨厌的格式问题。基本64编码公钥在metro应用程序中解码为字节数组。这就是问题所在。当我试图调用AsymmetricKeyAlgorithmProvider.ImportPublicKey时,我得到了错误“ASN1 bad tag value met”

我使用RSA PKCS1进行加密。以下是相关代码:

WCF服务

     string keyName = "This is passed in via a parameter";
     var key = !CngKey.Exists(keyName) ? CngKey.Create(CngAlgorithm2.Rsa, keyName) : CngKey.Open(keyName);

     // Create the RSA container to get keys and then dispose
     using (var rsaCng = new RSACng(key) { EncryptionPaddingMode = AsymmetricPaddingMode.Pkcs1, KeySize = 2048 })
     {
        byte[] publicBlob = rsaCng.Key.Export(CngKeyBlobFormat.GenericPublicBlob);
        publicKey = Convert.ToBase64String(publicBlob);
     }
地铁应用程序

  public static string Encrypt(IBuffer dataBuffer, string publicKeyString)
  {
     var asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
     // The next line fails with ASN1 bad tag value met
     var publicKey = asymmAlg.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(publicKeyString), CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);

     var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);
     return CryptographicBuffer.EncodeToBase64String(encryptedData);
  }
编辑1:以下详细信息

从WCF服务导出2048bit密钥对的公钥会产生283位长的密钥blob,而从Metro应用导出相同类型的公钥仅为270位。当我导入Metro生成的公钥时,它成功了。你知道为什么WCF服务的公钥上有13个额外的比特吗?我认为是这些额外的13位导致了故障

以下是生成较短公钥blob的Metro代码:

var provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptographicKey standardKeyPair = provider.CreateKeyPair(2048);
byte[] standardKey = standardKeyPair.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey).ToArray();

已经很晚了,但也许它会帮助你或节省别人的时间

在导入期间更改blob类型的类型。这真的很奇怪,但经过试验,我成功地使用了它

您在WCF中的代码可能保持原样

仅更改地铁代码:

public static string Encrypt(IBuffer dataBuffer, string publicKeyString)
{
    var asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
    // The next line fails with ASN1 bad tag value met
    var publicKey = asymmAlg.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(publicKeyString), CryptographicPublicKeyBlobType.BCryptPublicKey);

    var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);
    return CryptographicBuffer.EncodeToBase64String(encryptedData);
}

因此,这里唯一的更改是导入过程中的
BCryptPublicKey
。然后它就起作用了。但不要问我为什么:-)。

也许值得在这里问一下: