/// <exception cref="NSch.JSchException"></exception>
 public static NSch.KeyPair GenKeyPair(JSch jsch, int type, int key_size)
 {
     NSch.KeyPair kpair = null;
     if (type == DSA)
     {
         kpair = new KeyPairDSA(jsch);
     }
     else
     {
         if (type == RSA)
         {
             kpair = new KeyPairRSA(jsch);
         }
     }
     if (kpair != null)
     {
         kpair.Generate(key_size);
     }
     return(kpair);
 }
        /// <exception cref="NSch.JSchException"></exception>
        public static NSch.KeyPair Load(JSch jsch, string prvkey, string pubkey)
        {
            byte[] iv = new byte[8];
            // 8
            bool encrypted = true;

            byte[] data             = null;
            byte[] publickeyblob    = null;
            int    type             = ERROR;
            int    vendor           = VENDOR_OPENSSH;
            string publicKeyComment = string.Empty;

            NSch.Cipher cipher = null;
            try
            {
                FilePath        file = new FilePath(prvkey);
                FileInputStream fis  = new FileInputStream(prvkey);
                byte[]          buf  = new byte[(int)(file.Length())];
                int             len  = 0;
                while (true)
                {
                    int i = fis.Read(buf, len, buf.Length - len);
                    if (i <= 0)
                    {
                        break;
                    }
                    len += i;
                }
                fis.Close();
                int i_1 = 0;
                while (i_1 < len)
                {
                    if (buf[i_1] == '-' && i_1 + 4 < len && buf[i_1 + 1] == '-' && buf[i_1 + 2] == '-' &&
                        buf[i_1 + 3] == '-' && buf[i_1 + 4] == '-')
                    {
                        break;
                    }
                    i_1++;
                }
                while (i_1 < len)
                {
                    if (buf[i_1] == 'B' && i_1 + 3 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'G' &&
                        buf[i_1 + 3] == 'I')
                    {
                        i_1 += 6;
                        if (buf[i_1] == 'D' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A')
                        {
                            type = DSA;
                        }
                        else
                        {
                            if (buf[i_1] == 'R' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'A')
                            {
                                type = RSA;
                            }
                            else
                            {
                                if (buf[i_1] == 'S' && buf[i_1 + 1] == 'S' && buf[i_1 + 2] == 'H')
                                {
                                    // FSecure
                                    type   = UNKNOWN;
                                    vendor = VENDOR_FSECURE;
                                }
                                else
                                {
                                    throw new JSchException("invalid privatekey: " + prvkey);
                                }
                            }
                        }
                        i_1 += 3;
                        continue;
                    }
                    if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' &&
                        buf[i_1 + 3] == '-' && buf[i_1 + 4] == '2' && buf[i_1 + 5] == '5' && buf[i_1
                                                                                                 + 6] == '6' && buf[i_1 + 7] == '-')
                    {
                        i_1 += 8;
                        if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc")))
                        {
                            Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes256-cbc"));
                            cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
                            // key=new byte[cipher.getBlockSize()];
                            iv = new byte[cipher.GetIVSize()];
                        }
                        else
                        {
                            throw new JSchException("privatekey: aes256-cbc is not available " + prvkey);
                        }
                        continue;
                    }
                    if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' &&
                        buf[i_1 + 3] == '-' && buf[i_1 + 4] == '1' && buf[i_1 + 5] == '9' && buf[i_1
                                                                                                 + 6] == '2' && buf[i_1 + 7] == '-')
                    {
                        i_1 += 8;
                        if (Session.CheckCipher((string)JSch.GetConfig("aes192-cbc")))
                        {
                            Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes192-cbc"));
                            cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
                            // key=new byte[cipher.getBlockSize()];
                            iv = new byte[cipher.GetIVSize()];
                        }
                        else
                        {
                            throw new JSchException("privatekey: aes192-cbc is not available " + prvkey);
                        }
                        continue;
                    }
                    if (buf[i_1] == 'A' && i_1 + 7 < len && buf[i_1 + 1] == 'E' && buf[i_1 + 2] == 'S' &&
                        buf[i_1 + 3] == '-' && buf[i_1 + 4] == '1' && buf[i_1 + 5] == '2' && buf[i_1
                                                                                                 + 6] == '8' && buf[i_1 + 7] == '-')
                    {
                        i_1 += 8;
                        if (Session.CheckCipher((string)JSch.GetConfig("aes128-cbc")))
                        {
                            Type c = Sharpen.Runtime.GetType((string)JSch.GetConfig("aes128-cbc"));
                            cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
                            // key=new byte[cipher.getBlockSize()];
                            iv = new byte[cipher.GetIVSize()];
                        }
                        else
                        {
                            throw new JSchException("privatekey: aes128-cbc is not available " + prvkey);
                        }
                        continue;
                    }
                    if (buf[i_1] == 'C' && i_1 + 3 < len && buf[i_1 + 1] == 'B' && buf[i_1 + 2] == 'C' &&
                        buf[i_1 + 3] == ',')
                    {
                        i_1 += 4;
                        for (int ii = 0; ii < iv.Length; ii++)
                        {
                            iv[ii] = unchecked ((byte)(((A2b(buf[i_1++]) << 4) & unchecked ((int)(0xf0))) + (A2b
                                                                                                                 (buf[i_1++]) & unchecked ((int)(0xf)))));
                        }
                        continue;
                    }
                    if (buf[i_1] == unchecked ((int)(0x0d)) && i_1 + 1 < buf.Length && buf[i_1 + 1] ==
                        unchecked ((int)(0x0a)))
                    {
                        i_1++;
                        continue;
                    }
                    if (buf[i_1] == unchecked ((int)(0x0a)) && i_1 + 1 < buf.Length)
                    {
                        if (buf[i_1 + 1] == unchecked ((int)(0x0a)))
                        {
                            i_1 += 2;
                            break;
                        }
                        if (buf[i_1 + 1] == unchecked ((int)(0x0d)) && i_1 + 2 < buf.Length && buf[i_1 + 2
                            ] == unchecked ((int)(0x0a)))
                        {
                            i_1 += 3;
                            break;
                        }
                        bool inheader = false;
                        for (int j = i_1 + 1; j < buf.Length; j++)
                        {
                            if (buf[j] == unchecked ((int)(0x0a)))
                            {
                                break;
                            }
                            //if(buf[j]==0x0d) break;
                            if (buf[j] == ':')
                            {
                                inheader = true;
                                break;
                            }
                        }
                        if (!inheader)
                        {
                            i_1++;
                            encrypted = false;
                            // no passphrase
                            break;
                        }
                    }
                    i_1++;
                }
                if (type == ERROR)
                {
                    throw new JSchException("invalid privatekey: " + prvkey);
                }
                int start = i_1;
                while (i_1 < len)
                {
                    if (buf[i_1] == unchecked ((int)(0x0a)))
                    {
                        bool xd = (buf[i_1 - 1] == unchecked ((int)(0x0d)));
                        System.Array.Copy(buf, i_1 + 1, buf, i_1 - (xd ? 1 : 0), len - i_1 - 1 - (xd ? 1 :
                                                                                                  0));
                        if (xd)
                        {
                            len--;
                        }
                        len--;
                        continue;
                    }
                    if (buf[i_1] == '-')
                    {
                        break;
                    }
                    i_1++;
                }
                data = Util.FromBase64(buf, start, i_1 - start);
                if (data.Length > 4 && data[0] == unchecked ((byte)unchecked ((int)(0x3f))) && data
                    [1] == unchecked ((byte)unchecked ((int)(0x6f))) && data[2] == unchecked ((byte)unchecked (
                                                                                                  (int)(0xf9))) && data[3] == unchecked ((byte)unchecked ((int)(0xeb))))
                {
                    // FSecure
                    Buffer _buf = new Buffer(data);
                    _buf.GetInt();
                    // 0x3f6ff9be
                    _buf.GetInt();
                    byte[] _type = _buf.GetString();
                    //System.err.println("type: "+new String(_type));
                    string _cipher = Util.Byte2str(_buf.GetString());
                    //System.err.println("cipher: "+_cipher);
                    if (_cipher.Equals("3des-cbc"))
                    {
                        _buf.GetInt();
                        byte[] foo = new byte[data.Length - _buf.GetOffSet()];
                        _buf.GetByte(foo);
                        data      = foo;
                        encrypted = true;
                        throw new JSchException("unknown privatekey format: " + prvkey);
                    }
                    else
                    {
                        if (_cipher.Equals("none"))
                        {
                            _buf.GetInt();
                            _buf.GetInt();
                            encrypted = false;
                            byte[] foo = new byte[data.Length - _buf.GetOffSet()];
                            _buf.GetByte(foo);
                            data = foo;
                        }
                    }
                }
                if (pubkey != null)
                {
                    try
                    {
                        file = new FilePath(pubkey);
                        fis  = new FileInputStream(pubkey);
                        buf  = new byte[(int)(file.Length())];
                        len  = 0;
                        while (true)
                        {
                            i_1 = fis.Read(buf, len, buf.Length - len);
                            if (i_1 <= 0)
                            {
                                break;
                            }
                            len += i_1;
                        }
                        fis.Close();
                        if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] ==
                            '-')
                        {
                            // FSecure's public key
                            bool valid = true;
                            i_1 = 0;
                            do
                            {
                                i_1++;
                            }while (buf.Length > i_1 && buf[i_1] != unchecked ((int)(0x0a)));
                            if (buf.Length <= i_1)
                            {
                                valid = false;
                            }
                            while (valid)
                            {
                                if (buf[i_1] == unchecked ((int)(0x0a)))
                                {
                                    bool inheader = false;
                                    for (int j = i_1 + 1; j < buf.Length; j++)
                                    {
                                        if (buf[j] == unchecked ((int)(0x0a)))
                                        {
                                            break;
                                        }
                                        if (buf[j] == ':')
                                        {
                                            inheader = true;
                                            break;
                                        }
                                    }
                                    if (!inheader)
                                    {
                                        i_1++;
                                        break;
                                    }
                                }
                                i_1++;
                            }
                            if (buf.Length <= i_1)
                            {
                                valid = false;
                            }
                            start = i_1;
                            while (valid && i_1 < len)
                            {
                                if (buf[i_1] == unchecked ((int)(0x0a)))
                                {
                                    System.Array.Copy(buf, i_1 + 1, buf, i_1, len - i_1 - 1);
                                    len--;
                                    continue;
                                }
                                if (buf[i_1] == '-')
                                {
                                    break;
                                }
                                i_1++;
                            }
                            if (valid)
                            {
                                publickeyblob = Util.FromBase64(buf, start, i_1 - start);
                                if (type == UNKNOWN)
                                {
                                    if (publickeyblob[8] == 'd')
                                    {
                                        type = DSA;
                                    }
                                    else
                                    {
                                        if (publickeyblob[8] == 'r')
                                        {
                                            type = RSA;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (buf[0] == 's' && buf[1] == 's' && buf[2] == 'h' && buf[3] == '-')
                            {
                                i_1 = 0;
                                while (i_1 < len)
                                {
                                    if (buf[i_1] == ' ')
                                    {
                                        break;
                                    }
                                    i_1++;
                                }
                                i_1++;
                                if (i_1 < len)
                                {
                                    start = i_1;
                                    while (i_1 < len)
                                    {
                                        if (buf[i_1] == ' ')
                                        {
                                            break;
                                        }
                                        i_1++;
                                    }
                                    publickeyblob = Util.FromBase64(buf, start, i_1 - start);
                                }
                                if (i_1++ < len)
                                {
                                    int s = i_1;
                                    while (i_1 < len)
                                    {
                                        if (buf[i_1] == '\n')
                                        {
                                            break;
                                        }
                                        i_1++;
                                    }
                                    if (i_1 < len)
                                    {
                                        publicKeyComment = Sharpen.Runtime.GetStringForBytes(buf, s, i_1 - s);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                if (e is JSchException)
                {
                    throw (JSchException)e;
                }
                if (e is Exception)
                {
                    throw new JSchException(e.ToString(), (Exception)e);
                }
                throw new JSchException(e.ToString());
            }
            NSch.KeyPair kpair = null;
            if (type == DSA)
            {
                kpair = new KeyPairDSA(jsch);
            }
            else
            {
                if (type == RSA)
                {
                    kpair = new KeyPairRSA(jsch);
                }
            }
            if (kpair != null)
            {
                kpair.encrypted        = encrypted;
                kpair.publickeyblob    = publickeyblob;
                kpair.vendor           = vendor;
                kpair.publicKeyComment = publicKeyComment;
                kpair.cipher           = cipher;
                if (encrypted)
                {
                    kpair.iv   = iv;
                    kpair.data = data;
                }
                else
                {
                    if (kpair.Parse(data))
                    {
                        return(kpair);
                    }
                    else
                    {
                        throw new JSchException("invalid privatekey: " + prvkey);
                    }
                }
            }
            return(kpair);
        }