/// <summary> /// Export RSA private key /// </summary> /// <param name="rsa"></param> /// <param name="format"></param> /// <param name="usePemFormat"></param> /// <returns></returns> public static string ExportPrivateKey(this MsRSA rsa, RsaKeyFormat format, bool usePemFormat = false) { var key = format switch { RsaKeyFormat.XML => rsa.ExportKeyInLvccXml(true), RsaKeyFormat.JSON => rsa.ExportKeyInJson(true), #if NETCOREAPP3_1 || NETSTANDARD2_1 RsaKeyFormat.Pkcs1 => BaseConv.ToBase64(rsa.ExportRSAPrivateKey()), RsaKeyFormat.Pkcs8 => BaseConv.ToBase64(rsa.ExportPkcs8PrivateKey()), #else RsaKeyFormat.Pkcs1 => rsa.GetPrivateKeyInPkcs1(), RsaKeyFormat.Pkcs8 => rsa.GetPrivateKeyInPkcs8(), #endif _ => throw new NotSupportedException("Unknown RSA key type.") }; if (usePemFormat) { key = format switch { RsaKeyFormat.XML => key, RsaKeyFormat.JSON => key, RsaKeyFormat.Pkcs1 => key.RemovePkcs1PrivateKeyFormat(), RsaKeyFormat.Pkcs8 => key.RemovePkcs8PrivateKeyFormat(), _ => throw new NotSupportedException("Unknown RSA key type.") }; } return(key); }
/// <summary> /// Import RSA private key /// </summary> /// <param name="rsa"></param> /// <param name="format"></param> /// <param name="privateKey"></param> /// <param name="isPem"></param> public static void ImportPrivateKey(this MsRSA rsa, RsaKeyFormat format, string privateKey, bool isPem = false) { if (isPem) { privateKey = format switch { RsaKeyFormat.XML => privateKey, RsaKeyFormat.JSON => privateKey, RsaKeyFormat.Pkcs1 => privateKey.RemovePkcs1PrivateKeyFormat(), RsaKeyFormat.Pkcs8 => privateKey.RemovePkcs8PrivateKeyFormat(), _ => throw new NotSupportedException("Unknown RSA key type.") }; } switch (format) { case RsaKeyFormat.XML: rsa.ImportKeyInLvccXml(privateKey); break; case RsaKeyFormat.JSON: rsa.ImportKeyInJson(privateKey); break; case RsaKeyFormat.Pkcs1: #if NETCOREAPP3_1 || NETSTANDARD2_1 rsa.ImportRSAPrivateKey(BaseConv.FromBase64(privateKey), out _); #else rsa.TouchFromPrivateKeyInPkcs1(privateKey, out _); #endif break; case RsaKeyFormat.Pkcs8: #if NETCOREAPP3_1 || NETSTANDARD2_1 rsa.ImportPkcs8PrivateKey(BaseConv.FromBase64(privateKey), out _); #else rsa.TouchFromPrivateKeyInPkcs8(privateKey, out _); #endif break; } }