【发布时间】:2015-10-13 15:04:53
【问题描述】:
我想使用具有给定模数和指数值的 RSA 生成公钥。
public static string RSAPublic(string toEncrypt) {
var crypt = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
var buffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);
string publikKey = modulus + exponent;
publikKey.Replace("\r\n", "");
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(publikKey);
string pk = System.Convert.ToBase64String(plainTextBytes);
IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(pk);
CryptographicKey key = crypt.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo); // Throws exception here, have tried using all the 4 available BlobTypes
// var key = crypt.CreateKeyPair(512);
var sigBuffer = CryptographicEngine.Encrypt(key, buffer, null);
string signature = CryptographicBuffer.EncodeToBase64String(sigBuffer);
return signature;
}
以下是异常消息:“遇到 ASN1 错误标记值。(来自 HRESULT 的异常:0x8009310B)”
StackTrace:“在 Windows.Security.Cryptography.Core.AsymmetricKeyAlgorithmProvider.ImportPublicKey(IBuffer keyBlob, CryptographicPublicKeyBlobType BlobType) 在 MyProject.General.Utility.RSAPublic(String toEncrypt)"
我无法找出正确的方法来生成创建加密字符串所需的 CryptographicKey。任何帮助将不胜感激。
【问题讨论】:
-
你提到你得到了一个异常:你到底得到了什么异常?
-
@user1666620 :使用 CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey ,我得到了 ASN1 错误标签值。 (HRESULT 异常:0x8009310B)
-
编辑您的问题并添加实际的异常消息以及堆栈跟踪。
标签: c# encryption windows-phone-8.1 rsa win-universal-app