0

I need to use RSA Cryptography in c# project to send ecrypted data to service. Service gave me a public key like this:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjU/bq6YZD2H0DUhbtEBg
JIyiurM8eX3aH02/ZWr6VZ27WF93ylWC4cGAe50sSgiA8NCW0G/UL77kAkebJQrJ
jVdt7SvDypSPk1mXNK0i9cI9DrdmAHLGLlYJx7eeY6h4JShLhOBnKRghi0S4uL5N
L7W4OUgCeUlGWcmz8ssNEQ5w17rfUF9TxYEFVKFMGN/SSaYNUr4znGt2r97YPsPy
0Sk4dGHhMXr1QGR05UQeVuU43OuRAFxA71YbuCRUYg5ENwKM/1RnNcu8v7kXFA4L
qGV9AncHLIZEOqWgY+4balVXlKIcMVN6W+PXKJpowOyB9QIq1Ec3OMaJ3sGpOppx
KQIDAQAB
-----END PUBLIC KEY-----

How can i convert it to base64 string to use with this code?

public static string EncryptData(string publicKey, string clearText)
{
    IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(publicKey);
    AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
    CryptographicKey key = asym.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.Capi1PublicKey);
    IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(clearText, BinaryStringEncoding.Utf8);
    IBuffer encryptedBuffer = CryptographicEngine.Encrypt(key, plainBuffer, null);
    byte[] encryptedBytes;
    CryptographicBuffer.CopyToByteArray(encryptedBuffer, out encryptedBytes);
    return Convert.ToBase64String(encryptedBytes);
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Yan
  • 1
  • 3
  • Possible duplicate of [C# / .NET crypto: Using Base64 encoded Public Key to verify RSA signature](http://stackoverflow.com/questions/9283716/c-sharp-net-crypto-using-base64-encoded-public-key-to-verify-rsa-signature) – santosh singh Sep 28 '16 at 14:59

1 Answers1

0

I found solution, it was easy: I need to use:

 CryptographicKey key = asym.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
Yan
  • 1
  • 3