I have public key modulus and public key exponent and I need to generate a public key and encrypt data in metro style application. in c# we have RSAParameters class but I cannot find any thing of such sort for metro style applications.
when I use the base64encoded public key directly received from the certificate and try to import the key with the below code I get an exception thrown ASN1 bad tag value met.
which i think is due to the invalid format of the data.
//sample dummy key from certificate in base64encoded
string key = @"MIIB0zCCAX2gAwIBAgIJAMF/bHcA799IMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTIwMzI3MTEyNjQ5WhcNMTMwMzI3MTEyNjQ5WjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMEPeWjP4sdqxvBlDId4BtRRTeWPwjlZLSOFvOVgmoSyoPva8psFUF6tH9/vPXIJrL80tdCoBt8YFH6pwDN9a1sCAwEAAaNQME4wHQYDVR0OBBYEFGARqQfUhX7atVU4sS+aQAPt/jFxMB8GA1UdIwQYMBaAFGARqQfUhX7atVU4sS+aQAPt/jFxMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADQQALqOyjovRbWUZvziVaE8QYy83WEln1l+HJU9D6tFncUZTlwSd8aUwyQsd3zOVNZ41oCAVv5R3h1jtBtPbM+c1K";
symmetricKeyAlgorithmProvider asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSA_OAEP_SHA1");
CryptographicKey publicKey = asymmAlg.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(key));
string input64string ="encrypt this";
IBuffer dataToEncrypt = CryptographicBuffer.DecodeFromBase64String(input64string);
IBuffer encryptedData = CryptographicEngine.Encrypt(publicKey, dataToEncrypt, null);
Your
keyis the modulus part of the public key, whose length is 128. Consider the standard key exported from C# code in metro:you can see the length of the byte[] standardKey is 140,which has 7 bits prefix and 5 bits tail. I don’t know the reason, but I copied the extra 12 bits to the known key, it works. Hope this helps you: