UWP [Universal windows platform] RSA AsymmetricKey

2020-04-17 08:38发布

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 ";        
}

2条回答
可以哭但决不认输i
2楼-- · 2020-04-17 09:40

I have found a solution to my problem the problem was that the public key i was trying to import was not in ASN1 form and therefore the uwp Cryptography Core couldn't do the conversion from DER to ASN1 the new code is :

byte[] bytes = Encoding.UTF8.GetBytes(publicKeyString);               

IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(publicKeyString);

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

CryptographicKey publicKey = provider.ImportPublicKey(keyBuffer,CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);`
查看更多
何必那么认真
3楼-- · 2020-04-17 09:42

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);
查看更多
登录 后发表回答