Retrieve ASN PEM file heading. - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:ASN

Description

Retrieve ASN PEM file heading.

Demo Code


using Microsoft.Win32;
using System.Text;
using System.IO;//  w  ww .  jav a  2  s . c  o m
using System;

public class Main{
        /// <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 = BytesToString(data);
                return GetPemHeader(dataStr);
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// Convert a ASCII byte array to string, also filter out the null characters.
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToString(byte[] bytes)
        {
            string retval = "";
            if (bytes == null || bytes.Length < 1) return retval;
            char[] cretval = new char[bytes.Length];
            for (int i=0, j=0; i<bytes.Length; i++)
            {
                if (bytes[i] != '\0')
                {
                    cretval[j++] = (char) bytes[i];
                }
            }
            retval = new string(cretval);
            retval = retval.TrimEnd('\0');
            return retval;
        }
}

Related Tutorials