Пример #1
0
        /// <summary>
        /// Generate node text header. This method is used by GetNodeText to put heading.
        /// </summary>
        /// <param name="lineLen">Line length.</param>
        /// <returns>Header string.</returns>
        static public string GetNodeTextHeader(int lineLen)
        {
            string header = String.Format("Offset| Len  |LenByte|\r\n");

            header += "======+======+=======+" + Asn1Util.GenStr(lineLen + 10, '=') + "\r\n";
            return(header);
        }
Пример #2
0
 /// <summary>
 /// Encode single OID value.
 /// </summary>
 /// <param name="bt">output stream.</param>
 /// <param name="v">source value.</param>
 protected void EncodeValue(Stream bt, ulong v)
 {
     for (int i = (Asn1Util.BitPrecision(v) - 1) / 7; i > 0; i--)
     {
         bt.WriteByte((byte)(0x80 | ((v >> (i * 7)) & 0x7f)));
     }
     bt.WriteByte((byte)(v & 0x7f));
 }
Пример #3
0
 /// <summary>
 /// Retrieve PEM file heading.
 /// </summary>
 /// <param name="fileName">source file name.</param>
 /// <returns>heading string.</returns>
 public static string GetPemFileHeader(string fileName)
 {
     try
     {
         FileStream fs   = new FileStream(fileName, FileMode.Open);
         byte[]     data = new byte[fs.Length];
         fs.Read(data, 0, data.Length);
         fs.Close();
         string dataStr = Asn1Util.BytesToString(data);
         return(GetPemHeader(dataStr));
     }
     catch
     {
         return("");
     }
 }
Пример #4
0
        /// <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);
        }
Пример #5
0
        /// <summary>
        /// Load PEM formated file.
        /// </summary>
        /// <param name="fileName">PEM file name.</param>
        public void LoadPemData(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);

            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            string dataStr = Asn1Util.BytesToString(data);

            if (Asn1Util.IsPemFormated(dataStr))
            {
                Stream ms = Asn1Util.PemToStream(dataStr);
                ms.Position = 0;
                LoadData(ms);
            }
            else
            {
                throw new Exception("It is a invalid PEM file: " + fileName);
            }
        }