Metro Style RSA Encryption

I have a public key module and a public key metric, and I need to create a public key and encrypt the data in a subway style application. in C # we have the RSAParameters class, but I cannot find anything like this for metro-style applications.

when I use the base64encoded public key obtained directly from the certificate and try to import the key with the code below, I get an exception caused by the error value of the ASN1 tag. which, in my opinion, is due to an invalid data format.

//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); 
+4
source share
2 answers

Your key is part of the public key module, whose length is 128. Consider a standard key exported from C # code to the metro:

 using System.Runtime.InteropServices.WindowsRuntime; CryptographicKey standardKeyPair = provider.CreateKeyPair(1024); byte[] standardKey = standardKeyPair.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey).ToArray(); 

you can see the byte length [] of standardKey is 140, which has a prefix of 7 bits and 5 bits of tail. I do not know the reason, but I copied an additional 12 bits to the known key, it works. Hope this helps you:

 public static IBuffer RsaEncrypt(this IBuffer dataToEncrypt, string publicKeyN) { AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1); CryptographicKey standardKeyPair = provider.CreateKeyPair(1024); byte[] standardKey = standardKeyPair.ExportPublicKey(CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey).ToArray(); var data_n = CryptographicBuffer.DecodeFromBase64String(publicKeyN).ToArray(); Array.Copy(data_n, 0, standardKey, 7, data_n.Length); var key = provider.ImportPublicKey(standardKey.AsBuffer(), CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); var result = CryptographicEngine.Encrypt(key, dataToEncrypt, null); return result; } 
+2
source

This should do what you ask for:

 public static IBuffer RsaEncrypt(byte[] modulus, byte[] exponent, IBuffer data) { var rsa = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaOaepSha1); var keyBlob = modulus.Concat(exponent).ToArray().AsBuffer(); var publicKey = rsa.ImportPublicKey(keyBlob, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey); return CryptographicEngine.Encrypt(publicKey, data, null); } 

Of course, you can still use base64 strings as input / output if you want, but you already know how to do it. :)

If you go to page 61 of RFC3447 , you will find that the public key format structure is just a module, followed by an exponent, so I concatenated them.

0
source

All Articles