Convert RSA Public Key format to X509 Subject Key Info format

 
 
The example represents two simple functions, which convert the RSA Public Key format to the X509 Subject Key Info format in base64 and PEM representation.
 
//converts from RSA Public Key format to X509 Subject Key Info format
private string Rsa2X509Convert(string rsaPublicKey)
{
   using (RsaKey rsaKey = new CryptRsaKey())
   {
        rsaKey.Init();

	var key = Encoder.DecodeToBytes(rsaPublicKey, EncodeMethod.Base64);
	rsaKey.SetRsaPublicKey(key);
	key = rsaKey.GetPublicKeyInfo();

	var result = Encoder.Encode(key, EncodeMethod.Base64);
	return result;
   }			
}
 
//converts from RSA Public Key PEM format to X509 Subject Key Info PEM format
private string Rsa2X509PemConvert(string rsaPublicKey)
{
	using (RsaKey rsaKey = new CryptRsaKey())
	using (CryptEncoder encoder = new CryptEncoder())
	{
		rsaKey.Init();

		var key = encoder.Decode(rsaPublicKey);
		rsaKey.SetRsaPublicKey(key);
		key = rsaKey.GetPublicKeyInfo();

		var result = encoder.Encode(key);
		return result;
	}
}
 
private void btnConvert1_Click(object sender, EventArgs e)
{
	string RsaKey, x509KeyInfo;
	RsaKey = textBox1.Text;
	x509KeyInfo = Rsa2X509Convert(RsaKey);

	textBox3.Text = x509KeyInfo;
}

private void btnConvert2_Click(object sender, EventArgs e)
{
	string RsaKeyPem, x509KeyInfo;
	RsaKeyPem = textBox2.Text;
	x509KeyInfo = Rsa2X509PemConvert(RsaKeyPem);

	textBox3.Text = x509KeyInfo;
}

Add Feedback