/// <summary> /// Create PKCS 8 from hex string. /// </summary> /// <param name="data">Hex string.</param> /// <returns>PKCS 8</returns> public static GXPkcs8 FromHexString(string data) { GXPkcs8 cert = new GXPkcs8(); cert.Init(GXCommon.HexToBytes(data)); return(cert); }
/// <summary> /// Create PKCS #8 from DER Base64 encoded string. /// </summary> /// <param name="der">Base64 DER string.</param> /// <returns></returns> public static GXPkcs8 FromDer(string der) { der = der.Replace("\r\n", ""); der = der.Replace("\n", ""); GXPkcs8 cert = new GXPkcs8(); cert.Init(GXCommon.FromBase64(der)); return(cert); }
/// <summary> /// Import certificate from string. /// </summary> /// <param name="value"></param> /// <returns></returns> public static GXPkcs8 Import(string value) { GXPrivateKey pk; GXPkcs8 ret; try { ret = GXPkcs8.FromPem(value); } catch (Exception) { try { ret = GXPkcs8.FromDer(value); } catch (Exception) { try { //If PEM. pk = GXPrivateKey.FromPem(value); ret = new GXPkcs8(pk); } catch (Exception) { try { //If DER. pk = GXPrivateKey.FromDer(value); ret = new GXPkcs8(pk); } catch (Exception) { try { //If Raw. pk = GXPrivateKey.FromRawBytes(GXDLMSTranslator.HexToBytes(value)); ret = new GXPkcs8(pk); } catch (Exception) { throw new Exception("Invalid private key format."); } } } } } return(ret); }
/// <summary>Load private key from the PEM file. /// </summary> /// <param name="path">File path. </param> /// <returns> Created GXPkcs8 object. </returns> public static GXPkcs8 Load(string path) { return(GXPkcs8.FromPem(File.ReadAllText(path))); }