I want to generate a public key using RSA with given Modulus and Exponent values.
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;
}
Following is the Exception Message : "ASN1 bad tag value met. (Exception from HRESULT: 0x8009310B)"
StackTrace : " at Windows.Security.Cryptography.Core.AsymmetricKeyAlgorithmProvider.ImportPublicKey(IBuffer keyBlob, CryptographicPublicKeyBlobType BlobType) at MyProject.General.Utility.RSAPublic(String toEncrypt)"
I am not able to figure out the right way to generate the CryptographicKey necessary to create encrypted string. Any help would be appreciated.