Last active
November 7, 2024 01:08
-
-
Save misaxi/4642030 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace RsaKeyConverter.Converter | |
{ | |
public static class BytesExtensions | |
{ | |
public static string ToBase64(this byte[] bytes) | |
{ | |
return Convert.ToBase64String(bytes); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.Security; | |
namespace RsaKeyConverter.Converter | |
{ | |
public static class RsaExtensions | |
{ | |
public static AsymmetricCipherKeyPair GetKeyPair(this RSA rsa) | |
{ | |
try | |
{ | |
return DotNetUtilities.GetRsaKeyPair(rsa); | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
public static RsaKeyParameters GetPublicKey(this RSA rsa) | |
{ | |
try | |
{ | |
return DotNetUtilities.GetRsaPublicKey(rsa); | |
} | |
catch | |
{ | |
return null; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Org.BouncyCastle.Asn1.Pkcs; | |
using Org.BouncyCastle.Asn1.X509; | |
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.OpenSsl; | |
using Org.BouncyCastle.Pkcs; | |
using Org.BouncyCastle.Security; | |
using Org.BouncyCastle.X509; | |
namespace RsaKeyConverter.Converter | |
{ | |
public static class RsaKeyConverter | |
{ | |
public static string XmlToPem(string xml) | |
{ | |
using (RSA rsa = RSA.Create()) | |
{ | |
rsa.FromXmlString(xml); | |
AsymmetricCipherKeyPair keyPair = rsa.GetKeyPair(); // try get private and public key pair | |
if (keyPair != null) // if XML RSA key contains private key | |
{ | |
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private); | |
return FormatPem(privateKeyInfo.GetEncoded().ToBase64(), "RSA PRIVATE KEY"); | |
} | |
RsaKeyParameters publicKey = rsa.GetPublicKey(); // try get public key | |
if (publicKey != null) // if XML RSA key contains public key | |
{ | |
SubjectPublicKeyInfo publicKeyInfo = | |
SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey); | |
return FormatPem(publicKeyInfo.GetEncoded().ToBase64(), "PUBLIC KEY"); | |
} | |
} | |
throw new InvalidKeyException("Invalid RSA Xml Key"); | |
} | |
public static async Task<string> XmlToPemAsync(string xml) | |
{ | |
return await Task.Run(() => XmlToPem(xml)); | |
} | |
private static string FormatPem(string pem, string keyType) | |
{ | |
var sb = new StringBuilder(); | |
sb.AppendFormat("-----BEGIN {0}-----\n", keyType); | |
int line = 1, width = 64; | |
while ((line - 1) * width < pem.Length) | |
{ | |
int startIndex = (line - 1) * width; | |
int len = line * width > pem.Length | |
? pem.Length - startIndex | |
: width; | |
sb.AppendFormat("{0}\n", pem.Substring(startIndex, len)); | |
line++; | |
} | |
sb.AppendFormat("-----END {0}-----\n", keyType); | |
return sb.ToString(); | |
} | |
public static string PemToXml(string pem) | |
{ | |
if (pem.StartsWith("-----BEGIN RSA PRIVATE KEY-----") | |
|| pem.StartsWith("-----BEGIN PRIVATE KEY-----")) | |
{ | |
return GetXmlRsaKey(pem, obj => | |
{ | |
if ((obj as RsaPrivateCrtKeyParameters) != null) | |
return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)obj); | |
var keyPair = (AsymmetricCipherKeyPair)obj; | |
return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)keyPair.Private); | |
}, rsa => rsa.ToXmlString(true)); | |
} | |
if (pem.StartsWith("-----BEGIN PUBLIC KEY-----")) | |
{ | |
return GetXmlRsaKey(pem, obj => | |
{ | |
var publicKey = (RsaKeyParameters)obj; | |
return DotNetUtilities.ToRSA(publicKey); | |
}, rsa => rsa.ToXmlString(false)); | |
} | |
throw new InvalidKeyException("Unsupported PEM format..."); | |
} | |
public static async Task<string> PemToXmlAsync(string pem) | |
{ | |
return await Task.Run(() => PemToXml(pem)); | |
} | |
private static string GetXmlRsaKey(string pem, Func<object, RSA> getRsa, Func<RSA, string> getKey) | |
{ | |
using (var ms = new MemoryStream()) | |
using (var sw = new StreamWriter(ms)) | |
using (var sr = new StreamReader(ms)) | |
{ | |
sw.Write(pem); | |
sw.Flush(); | |
ms.Position = 0; | |
var pr = new PemReader(sr); | |
object keyPair = pr.ReadObject(); | |
using (RSA rsa = getRsa(keyPair)) | |
{ | |
var xml = getKey(rsa); | |
return xml; | |
} | |
} | |
} | |
} | |
} |
Hi @misaxi @gumma91 @aakashsethi20
If I have a key of xml format, how can I run this c# script to convert the xml file?
How to run those script?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @gumma91, in case you are still looking into it, here's an example format for the pem string - https://github.com/aakashsethi20/XML-PEM-Converter/blob/81c61a9fcb171f067c11ccdb2c88678ff9f6e7e7/pemFile.pem