AsymmetricKeyAlgorithmProvider

AsymmetricKeyAlgorithmProvider

本文介绍了UWP [通用Windows平台] RSA AsymmetricKeyAlgorithmProvider导入公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在UWP中使用 Windows.Security.Cryptography.Core 时,总是从导入公钥时出现错误AsymmetricKeyAlgorithmProvider 。我尝试了每一个组合没有成功。我总是检查输入字符串为UTF8模式。

When I am trying to use Windows.Security.Cryptography.Core in UWP, I always get an error on importing the public key from AsymmetricKeyAlgorithmProvider. I tried every combination without success. I always checked the input string to be in UTF8 mode.

try {
  var bytes = Encoding.UTF8.GetBytes(publicKeyString);
  publicKeyString = Encoding.UTF8.GetString(bytes);

  Debug.WriteLine(publicKeyString);
  IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(publicKeyString);

  byte[] bytes = Convert.FromBase64String(publicKeyString);
  string hex = "2D2D2D2D2D424547494E205055424C4943204B45592D2D2D2D2D0A4D494942496A414E42676B71686B6947397730424151454641414F43415138414D49494243674B434151454172336767514744726E5645562B786A4F484F2B390A4B72595541547166756935666F364D65736E53466C6B797937482F4775327135667273724B305246383933507A584F664371414A6753534D58673330793463720A594E3742617838467979314C58744E2B5A35576B43436D644F597438704F636D7A75494D6A636E4E733063486B767859576B336658516D513255535A685063700A51595761382F4469392B344462745464587A643263346F717A4E4A4A4A4B66437A647731796E72776F4A755A4245563547747363396F48782F6D7231725434420A386F377635473553706D543368687661396762617034436C4231745677434B584F41636A2F71782F49416A505A35566E4B7A363669325534706A6F43764A79590A564E53497A3557346276726A6E622B76775848626B506D62316E6655503864526D33683767644F314D7330766B72715A4567714746416953343475686974576B0A65514944415141420A2D2D2D2D2D454E44205055424C4943204B45592D2D2D2D2D0A";

  Debug.WriteLine(hex);
  AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
  CryptographicKey publicKey = provider.ImportPublicKey(CryptographicBuffer.DecodeFromHexString(hex), CryptographicPublicKeyBlobType.BCryptPublicKey);
  IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(plainText));
  var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);

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


推荐答案

我遇到同样的问题: ASN1坏标签值遇到。
我发现,ImportKeyPair(IBuffer)对我的情况是正确的。
示例代码:

I meet the same problem:ASN1 bad tag value met.I found that the ImportKeyPair(IBuffer) is the correct for my situation.Sample Code:

var loginPBK = "";//your public key,such as "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg......."
var provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
var publicKey = provider.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(loginPBK));
var encryptData = CryptographicEngine.Encrypt(publicKey, CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8), null);
var pwd2 = CryptographicBuffer.EncodeToBase64String(encryptData);

这篇关于UWP [通用Windows平台] RSA AsymmetricKeyAlgorithmProvider导入公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 09:02