/// <summary>
        /// Load keys from keytab file.
        /// </summary>
        /// <param name="filename">Filename</param>
        public void LoadKeytab(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            byte[] buf = new byte[fs.Length];
            fs.Read(buf, 0, (int)fs.Length);
            Keytab kt = Keytab.Decode(buf);

            foreach (var entry in kt.entries)
            {
                string principal = entry.Components[0].ToString() +
                                   (entry.NumComponents > 1 ? ("/" + entry.Components[1].ToString()) : "");
                KeytabItem item = new KeytabItem()
                {
                    Principal = principal,
                    Realm     = entry.Realm.ToString(),
                    Kvno      = entry.Kvno,
                    KeyType   = (EncryptionType)entry.Key.type,
                    Key       = new EncryptionKey(new KerbInt32((long)entry.Key.type), new Asn1OctetString(entry.Key.Data.Data))
                };
                keytabItems.Add(item);

                // For Windows keytab compatibility
                // Computer name is used instead of service principal name in Windows AD database.
                // Add cifs SPN manually when encountering computer name.
                if (principal.EndsWith("$"))
                {
                    string cifsPrincipal = string.Format("cifs/{0}.{1}", principal.Remove(principal.Length - 1).ToLower(), entry.Realm.ToString().ToLower());
                    item = new KeytabItem()
                    {
                        Principal = cifsPrincipal,
                        Realm     = entry.Realm.ToString(),
                        Kvno      = entry.Kvno,
                        KeyType   = (EncryptionType)entry.Key.type,
                        Key       = new EncryptionKey(new KerbInt32((long)entry.Key.type), new Asn1OctetString(entry.Key.Data.Data))
                    };
                    keytabItems.Add(item);
                }
            }
            fs.Close();
        }
        public static Keytab Decode(byte[] buffer)
        {
            List <KeytabEntry> entryList    = new List <KeytabEntry>();
            KeytabDecodeBuffer decodeBuffer = new KeytabDecodeBuffer(buffer);
            Keytab             keytab       = new Keytab
            {
                FormatVersion = decodeBuffer.DecodeUInt16(),
                entries       = new KeytabEntry[1]
            };

            while (!decodeBuffer.EOS)
            {
                entryList.Add(KeytabEntry.Decode(decodeBuffer, keytab.FormatVersion));
            }
            keytab.entries = new KeytabEntry[entryList.Count];
            for (int i = 0; i < entryList.Count; i++)
            {
                keytab.entries[i] = entryList[i];
            }
            return(keytab);
        }
 public static Keytab Decode(byte[] buffer)
 {
     List<KeytabEntry> entryList = new List<KeytabEntry>();
     KeytabDecodeBuffer decodeBuffer = new KeytabDecodeBuffer(buffer);
     Keytab keytab = new Keytab
     {
         FormatVersion = decodeBuffer.DecodeUInt16(),
         entries = new KeytabEntry[1]
     };
     while (!decodeBuffer.EOS)
     {
         entryList.Add(KeytabEntry.Decode(decodeBuffer, keytab.FormatVersion));
     }
     keytab.entries = new KeytabEntry[entryList.Count];
     for (int i = 0; i < entryList.Count; i++)
     {
         keytab.entries[i] = entryList[i];
     }
     return keytab;
 }