I'm trying to work out a way to encrypt a string using a private key, and decrypt it using a public one. I generated the keys using OpenSSL as described in:
http://www.akadia.com/services/ssh_test_certificate.html
This is what I have currently
public static string Encrypt(string str, string key)
{
try
{
key = key.Replace(Environment.NewLine, "");
IBuffer keyBuffer = CryptographicBuffer.DecodeFromBase64String(key);
AsymmetricKeyAlgorithmProvider provider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptographicKey publicKey = provider.ImportPublicKey(keyBuffer, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);
IBuffer dataBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(str));
var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);
return CryptographicBuffer.EncodeToBase64String(encryptedData);
}
catch (Exception e)
{
throw;
return "Error in Encryption:With RSA ";
}
}
However on the ImportPublicKey
method I'm getting an exceptin ASN1 corrupted data
the string key
passed to that method has following format:
var privateKey =
@"MIICXwIBAAKBgQDUTqfSknFiQx3aepORHJycWck007cfU4fXluTIyf6U9ipDhyPD .... yDxwZVmexltyK5Bwc26lmb+5EtTEic+kZToYWcCucF8lsok=";
so the contents of the OpenSSL generated key file without this part:
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----