/// <summary> /// Check if a file is PEM formated. /// </summary> /// <param name="fileName">source file name.</param> /// <returns>true:Yes, false:No.</returns> public static bool IsPemFormatedFile(string fileName) { bool retval = false; try { FileStream fs = new FileStream(fileName, System.IO.FileMode.Open); byte[] data = new byte[fs.Length]; fs.Read(data, 0, data.Length); fs.Close(); string dataStr = Asn1Util.BytesToString(data); retval = IsPemFormated(dataStr); } catch { retval = false; } return(retval); }
/// <summary> /// Retrieve the node description. /// </summary> /// <param name="pureHexMode">true:Return hex string only; /// false:Convert to more readable string depending on the node tag.</param> /// <returns>string</returns> public string GetDataStr(bool pureHexMode) { const int lineLen = 32; string dataStr = ""; if (pureHexMode) { dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); } else { switch (tag) { case Asn1Tag.BIT_STRING: dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); break; case Asn1Tag.OBJECT_IDENTIFIER: Oid xoid = new Oid(); dataStr = xoid.Decode(new MemoryStream(data)); break; case Asn1Tag.RELATIVE_OID: RelativeOid roid = new RelativeOid(); dataStr = roid.Decode(new MemoryStream(data)); break; case Asn1Tag.PRINTABLE_STRING: case Asn1Tag.IA5_STRING: case Asn1Tag.UNIVERSAL_STRING: case Asn1Tag.VISIBLE_STRING: case Asn1Tag.NUMERIC_STRING: case Asn1Tag.UTC_TIME: case Asn1Tag.BMPSTRING: case Asn1Tag.GENERAL_STRING: case Asn1Tag.GENERALIZED_TIME: dataStr = Asn1Util.BytesToString(data); break; case Asn1Tag.UTF8_STRING: UTF8Encoding utf8 = new UTF8Encoding(); dataStr = utf8.GetString(data); break; case Asn1Tag.INTEGER: dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); break; default: if ((tag & Asn1Tag.TAG_MASK) == 6 || // Visible string for certificate Asn1Util.IsAsciiString(Data)) { dataStr = Asn1Util.BytesToString(data); } else { dataStr = Asn1Util.FormatString(Asn1Util.ToHexString(data), lineLen, 2); } break; } ; } return(dataStr); }