Now the most popular RSA encryption algorithm, only the public key and private key have to crack the encryption information, RSA encryption algorithm appears in favor of data security transmission
The 1.c# in the RSACryptoServiceProvider class allows you to generate XML-formatted public and private keys in a very good way, with two lines of code.
2. However, the generated XML format front-end is not very good to use and read, so in the generated XML format needs to be converted to PEM format, so as to copy directly to the verification tool encryption and decryption, very convenient
First, we first import a third-party library, because the conversion code involved in the following is required to rely on this library to implement, the import operation is as follows
Input in console
PM > Install-package bouncycastle
Import into a reference
3. OK, here is the pure Code implementation, the purpose is to convert the XML format to PEM format, regardless of the public key or private key, no wordy direct code walk
Static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
String xmlPrivateKey = rsa.ToXmlString(true);//XML key
String pemPrivateKey = Xml2PemPrivate(xmlPrivateKey, "F:/privatePEM.txt");//PEM key
String xmlPublicKey = rsa.ToXmlString(false);//XML public key
String pemPublicKey = Xml2PemPublic(xmlPublicKey, "F:/publicPEM.txt");//PEM public key
}
/// <summary>
/// XML format public key to PEM format public key
/// </summary>
/// <param name="xml">public key in XML format</param>
/// <param name="saveFile">Save the physical path of the file</param>
Public static string Xml2PemPublic(string xml, string saveFile)
{
Var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
Var p = rsa.ExportParameters(false);
RsaKeyParameters key = new RsaKeyParameters(false, new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent));
Using (var sw = new StreamWriter(saveFile))
{
Var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(key);
}
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(key);
Byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
String publicKey = Convert.ToBase64String(serializedPublicBytes);
Return Format(publicKey, 1);
}
/// <summary>
/// XML format private key to PEM
/// </summary>
/// <param name="xml">XML format private key</param>
/// <param name="saveFile">Save the physical path of the file</param>
Public static string Xml2PemPrivate(string xml, string saveFile)
{
Var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
Var p = rsa.ExportParameters(true);
Var key = new RsaPrivateCrtKeyParameters(
New BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D),
New BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ),
New BigInteger(1, p.InverseQ));
Using (var sw = new StreamWriter(saveFile))
{
Var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
pemWriter.WriteObject(key);
}
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key);
Byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
String privateKey = Convert.ToBase64String(serializedPrivateBytes);
Return Format(privateKey, 2);
}
/// <summary>
/// Format the public/private key
/// </summary>
/// <param name="key">generated public/private key</param>
/// <param name="type">1: public key 2: private key</param>
/// <returns>PEM format public/private key</returns>
Public static string Format(string key, int type)
{
String result = string.Empty;
Int length = key.Length / 64;
For (int i = 0; i < length; i++)
{
Int start = i * 64;
Result = result + key.Substring(start, 64) + "\r\n";
}
Result = result + key.Substring(length * 64);
If (type == 1)
{
Result = result.Insert(0, "-----BEGIN PUBLIC KEY-----\r\n");
Result += "\r\n-----END PUBLIC KEY-----";
}
If (type == 2)
{
Result = result.Insert(0, "-----BEGIN PRIVATE KEY-----\r\n");
Result += "\r\n-----END PRIVATE KEY-----";
}
Return result;
}
4. The final output in the TXT file on the F-drive can be directly used to copy the data directly into the verification tool for encryption/decryption