private NSch.Cipher GenCipher()
 {
     try
     {
         Type c;
         c      = Sharpen.Runtime.GetType(JSch.GetConfig("3des-cbc"));
         cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
     }
     catch (Exception)
     {
     }
     return(cipher);
 }
 private byte[] Encrypt(byte[] plain, byte[][] _iv)
 {
     if (passphrase == null)
     {
         return(plain);
     }
     if (cipher == null)
     {
         cipher = GenCipher();
     }
     byte[] iv = _iv[0] = new byte[cipher.GetIVSize()];
     if (random == null)
     {
         random = GenRandom();
     }
     random.Fill(iv, 0, iv.Length);
     byte[] key     = GenKey(passphrase, iv);
     byte[] encoded = plain;
     {
         // PKCS#5Padding
         //int bsize=cipher.getBlockSize();
         int    bsize = cipher.GetIVSize();
         byte[] foo   = new byte[(encoded.Length / bsize + 1) * bsize];
         System.Array.Copy(encoded, 0, foo, 0, encoded.Length);
         int padding = bsize - encoded.Length % bsize;
         for (int i = foo.Length - 1; (foo.Length - padding) <= i; i--)
         {
             foo[i] = unchecked ((byte)padding);
         }
         encoded = foo;
     }
     try
     {
         cipher.Init(NSch.Cipher.ENCRYPT_MODE, key, iv);
         cipher.Update(encoded, 0, encoded.Length, encoded, 0);
     }
     catch (Exception)
     {
     }
     //System.err.println(e);
     Util.Bzero(key);
     return(encoded);
 }
예제 #3
0
		/// <exception cref="System.Exception"></exception>
		private void UpdateKeys(KeyExchange kex)
		{
			byte[] K = kex.GetK();
			byte[] H = kex.GetH();
			HASH hash = kex.GetHash();
			//    String[] guess=kex.guess;
			if (session_id == null)
			{
				session_id = new byte[H.Length];
				System.Array.Copy(H, 0, session_id, 0, H.Length);
			}
			buf.Reset();
			buf.PutMPInt(K);
			buf.PutByte(H);
			buf.PutByte(unchecked((byte)unchecked((int)(0x41))));
			buf.PutByte(session_id);
			hash.Update(buf.buffer, 0, buf.index);
			IVc2s = hash.Digest();
			int j = buf.index - session_id.Length - 1;
			buf.buffer[j]++;
			hash.Update(buf.buffer, 0, buf.index);
			IVs2c = hash.Digest();
			buf.buffer[j]++;
			hash.Update(buf.buffer, 0, buf.index);
			Ec2s = hash.Digest();
			buf.buffer[j]++;
			hash.Update(buf.buffer, 0, buf.index);
			Es2c = hash.Digest();
			buf.buffer[j]++;
			hash.Update(buf.buffer, 0, buf.index);
			MACc2s = hash.Digest();
			buf.buffer[j]++;
			hash.Update(buf.buffer, 0, buf.index);
			MACs2c = hash.Digest();
			try
			{
				Type c;
				string method;
				method = guess[KeyExchange.PROPOSAL_ENC_ALGS_STOC];
				c = Sharpen.Runtime.GetType(GetConfig(method));
				s2ccipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
				while (s2ccipher.GetBlockSize() > Es2c.Length)
				{
					buf.Reset();
					buf.PutMPInt(K);
					buf.PutByte(H);
					buf.PutByte(Es2c);
					hash.Update(buf.buffer, 0, buf.index);
					byte[] foo = hash.Digest();
					byte[] bar = new byte[Es2c.Length + foo.Length];
					System.Array.Copy(Es2c, 0, bar, 0, Es2c.Length);
					System.Array.Copy(foo, 0, bar, Es2c.Length, foo.Length);
					Es2c = bar;
				}
				s2ccipher.Init(NSch.Cipher.DECRYPT_MODE, Es2c, IVs2c);
				s2ccipher_size = s2ccipher.GetIVSize();
				method = guess[KeyExchange.PROPOSAL_MAC_ALGS_STOC];
				c = Sharpen.Runtime.GetType(GetConfig(method));
				s2cmac = (MAC)(System.Activator.CreateInstance(c));
				s2cmac.Init(MACs2c);
				//mac_buf=new byte[s2cmac.getBlockSize()];
				s2cmac_result1 = new byte[s2cmac.GetBlockSize()];
				s2cmac_result2 = new byte[s2cmac.GetBlockSize()];
				method = guess[KeyExchange.PROPOSAL_ENC_ALGS_CTOS];
				c = Sharpen.Runtime.GetType(GetConfig(method));
				c2scipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
				while (c2scipher.GetBlockSize() > Ec2s.Length)
				{
					buf.Reset();
					buf.PutMPInt(K);
					buf.PutByte(H);
					buf.PutByte(Ec2s);
					hash.Update(buf.buffer, 0, buf.index);
					byte[] foo = hash.Digest();
					byte[] bar = new byte[Ec2s.Length + foo.Length];
					System.Array.Copy(Ec2s, 0, bar, 0, Ec2s.Length);
					System.Array.Copy(foo, 0, bar, Ec2s.Length, foo.Length);
					Ec2s = bar;
				}
				c2scipher.Init(NSch.Cipher.ENCRYPT_MODE, Ec2s, IVc2s);
				c2scipher_size = c2scipher.GetIVSize();
				method = guess[KeyExchange.PROPOSAL_MAC_ALGS_CTOS];
				c = Sharpen.Runtime.GetType(GetConfig(method));
				c2smac = (MAC)(System.Activator.CreateInstance(c));
				c2smac.Init(MACc2s);
				method = guess[KeyExchange.PROPOSAL_COMP_ALGS_CTOS];
				InitDeflater(method);
				method = guess[KeyExchange.PROPOSAL_COMP_ALGS_STOC];
				InitInflater(method);
			}
			catch (Exception e)
			{
				if (e is JSchException)
				{
					throw;
				}
				throw new JSchException(e.ToString(), e);
			}
		}
        /// <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);
        }
 internal virtual byte[] GenKey(byte[] passphrase, byte[] iv)
 {
     lock (this)
     {
         if (cipher == null)
         {
             cipher = GenCipher();
         }
         if (hash == null)
         {
             hash = GenHash();
         }
         byte[] key   = new byte[cipher.GetBlockSize()];
         int    hsize = hash.GetBlockSize();
         byte[] hn    = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 :
                                                               hsize)];
         try
         {
             byte[] tmp = null;
             if (vendor == VENDOR_OPENSSH)
             {
                 for (int index = 0; index + hsize <= hn.Length;)
                 {
                     if (tmp != null)
                     {
                         hash.Update(tmp, 0, tmp.Length);
                     }
                     hash.Update(passphrase, 0, passphrase.Length);
                     hash.Update(iv, 0, iv.Length > 8 ? 8 : iv.Length);
                     tmp = hash.Digest();
                     System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                     index += tmp.Length;
                 }
                 System.Array.Copy(hn, 0, key, 0, key.Length);
             }
             else
             {
                 if (vendor == VENDOR_FSECURE)
                 {
                     for (int index = 0; index + hsize <= hn.Length;)
                     {
                         if (tmp != null)
                         {
                             hash.Update(tmp, 0, tmp.Length);
                         }
                         hash.Update(passphrase, 0, passphrase.Length);
                         tmp = hash.Digest();
                         System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                         index += tmp.Length;
                     }
                     System.Array.Copy(hn, 0, key, 0, key.Length);
                 }
             }
         }
         catch (Exception e)
         {
             System.Console.Error.WriteLine(e);
         }
         return(key);
     }
 }
예제 #6
0
		internal virtual byte[] GenKey(byte[] passphrase, byte[] iv)
		{
			lock (this)
			{
				if (cipher == null)
				{
					cipher = GenCipher();
				}
				if (hash == null)
				{
					hash = GenHash();
				}
				byte[] key = new byte[cipher.GetBlockSize()];
				int hsize = hash.GetBlockSize();
				byte[] hn = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 : 
					hsize)];
				try
				{
					byte[] tmp = null;
					if (vendor == VENDOR_OPENSSH)
					{
						for (int index = 0; index + hsize <= hn.Length; )
						{
							if (tmp != null)
							{
								hash.Update(tmp, 0, tmp.Length);
							}
							hash.Update(passphrase, 0, passphrase.Length);
							hash.Update(iv, 0, iv.Length);
							tmp = hash.Digest();
							System.Array.Copy(tmp, 0, hn, index, tmp.Length);
							index += tmp.Length;
						}
						System.Array.Copy(hn, 0, key, 0, key.Length);
					}
					else
					{
						if (vendor == VENDOR_FSECURE)
						{
							for (int index = 0; index + hsize <= hn.Length; )
							{
								if (tmp != null)
								{
									hash.Update(tmp, 0, tmp.Length);
								}
								hash.Update(passphrase, 0, passphrase.Length);
								tmp = hash.Digest();
								System.Array.Copy(tmp, 0, hn, index, tmp.Length);
								index += tmp.Length;
							}
							System.Array.Copy(hn, 0, key, 0, key.Length);
						}
					}
				}
				catch (Exception e)
				{
					System.Console.Error.WriteLine(e);
				}
				return key;
			}
		}
예제 #7
0
		private NSch.Cipher GenCipher()
		{
			try
			{
				Type c;
				c = Sharpen.Runtime.GetType(JSch.GetConfig("3des-cbc"));
				cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
			}
			catch (Exception)
			{
			}
			return cipher;
		}
예제 #8
0
		private byte[] Encrypt(byte[] plain, byte[][] _iv)
		{
			if (passphrase == null)
			{
				return plain;
			}
			if (cipher == null)
			{
				cipher = GenCipher();
			}
			byte[] iv = _iv[0] = new byte[cipher.GetIVSize()];
			if (random == null)
			{
				random = GenRandom();
			}
			random.Fill(iv, 0, iv.Length);
			byte[] key = GenKey(passphrase, iv);
			byte[] encoded = plain;
			{
				// PKCS#5Padding
				//int bsize=cipher.getBlockSize();
				int bsize = cipher.GetIVSize();
				byte[] foo = new byte[(encoded.Length / bsize + 1) * bsize];
				System.Array.Copy(encoded, 0, foo, 0, encoded.Length);
				int padding = bsize - encoded.Length % bsize;
				for (int i = foo.Length - 1; (foo.Length - padding) <= i; i--)
				{
					foo[i] = unchecked((byte)padding);
				}
				encoded = foo;
			}
			try
			{
				cipher.Init(NSch.Cipher.ENCRYPT_MODE, key, iv);
				cipher.Update(encoded, 0, encoded.Length, encoded, 0);
			}
			catch (Exception)
			{
			}
			//System.err.println(e);
			Util.Bzero(key);
			return encoded;
		}
예제 #9
0
 /// <exception cref="NSch.JSchException"></exception>
 private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch)
 {
     this.identity = name;
     this.jsch     = jsch;
     // prvkey from "ssh-add" command on the remote.
     if (pubkey == null && prvkey != null && (prvkey.Length > 11 && prvkey[0] == 0 &&
                                              prvkey[1] == 0 && prvkey[2] == 0 && prvkey[3] == 7))
     {
         Buffer buf   = new Buffer(prvkey);
         string _type = Sharpen.Runtime.GetStringForBytes(buf.GetString());
         // ssh-rsa
         if (_type.Equals("ssh-rsa"))
         {
             type    = RSA;
             n_array = buf.GetString();
             e_array = buf.GetString();
             d_array = buf.GetString();
             buf.GetString();
             buf.GetString();
             buf.GetString();
             this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString());
         }
         else
         {
             if (_type.Equals("ssh-dss"))
             {
                 type           = DSS;
                 P_array        = buf.GetString();
                 Q_array        = buf.GetString();
                 G_array        = buf.GetString();
                 pub_array      = buf.GetString();
                 prv_array      = buf.GetString();
                 this.identity += Sharpen.Runtime.GetStringForBytes(buf.GetString());
             }
             else
             {
                 throw new JSchException("privatekey: invalid key " + Sharpen.Runtime.GetStringForBytes
                                             (prvkey, 4, 7));
             }
         }
         encoded_data = prvkey;
         encrypted    = false;
         keytype      = OPENSSH;
         return;
     }
     try
     {
         Type c;
         c      = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc"));
         cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
         key    = new byte[cipher.GetBlockSize()];
         // 24
         iv = new byte[cipher.GetIVSize()];
         // 8
         c    = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5"));
         hash = (HASH)(System.Activator.CreateInstance(c));
         hash.Init();
         byte[] buf = prvkey;
         int    len = buf.Length;
         int    i   = 0;
         while (i < len)
         {
             if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf
                 [i + 3] == '-' && buf[i + 4] == '-')
             {
                 break;
             }
             i++;
         }
         while (i < len)
         {
             if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf
                 [i + 3] == 'I')
             {
                 i += 6;
                 if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                 {
                     type = DSS;
                 }
                 else
                 {
                     if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
                     {
                         type = RSA;
                     }
                     else
                     {
                         if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
                         {
                             // FSecure
                             type    = UNKNOWN;
                             keytype = FSECURE;
                         }
                         else
                         {
                             //System.err.println("invalid format: "+identity);
                             throw new JSchException("invalid privatekey: " + identity);
                         }
                     }
                 }
                 i += 3;
                 continue;
             }
             if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf
                 [i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' &&
                 buf[i + 7] == '-')
             {
                 i += 8;
                 if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc")))
                 {
                     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 " + identity);
                 }
                 continue;
             }
             if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf
                 [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '9' && buf[i + 6] == '2' &&
                 buf[i + 7] == '-')
             {
                 i += 8;
                 if (Session.CheckCipher((string)JSch.GetConfig("aes192-cbc")))
                 {
                     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 " + identity);
                 }
                 continue;
             }
             if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf
                 [i + 3] == '-' && buf[i + 4] == '1' && buf[i + 5] == '2' && buf[i + 6] == '8' &&
                 buf[i + 7] == '-')
             {
                 i += 8;
                 if (Session.CheckCipher((string)JSch.GetConfig("aes128-cbc")))
                 {
                     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 " + identity);
                 }
                 continue;
             }
             if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf
                 [i + 3] == ',')
             {
                 i += 4;
                 for (int ii = 0; ii < iv.Length; ii++)
                 {
                     iv[ii] = unchecked ((byte)(((A2b(buf[i++]) << 4) & unchecked ((int)(0xf0))) + (A2b(
                                                                                                        buf[i++]) & unchecked ((int)(0xf)))));
                 }
                 continue;
             }
             if (buf[i] == unchecked ((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked ((int
                                                                                               )(0x0a)))
             {
                 i++;
                 continue;
             }
             if (buf[i] == unchecked ((int)(0x0a)) && i + 1 < len)
             {
                 if (buf[i + 1] == unchecked ((int)(0x0a)))
                 {
                     i += 2;
                     break;
                 }
                 if (buf[i + 1] == unchecked ((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked (
                         (int)(0x0a)))
                 {
                     i += 3;
                     break;
                 }
                 bool inheader = false;
                 for (int j = i + 1; j < len; j++)
                 {
                     if (buf[j] == unchecked ((int)(0x0a)))
                     {
                         break;
                     }
                     //if(buf[j]==0x0d) break;
                     if (buf[j] == ':')
                     {
                         inheader = true;
                         break;
                     }
                 }
                 if (!inheader)
                 {
                     i++;
                     encrypted = false;
                     // no passphrase
                     break;
                 }
             }
             i++;
         }
         if (type == ERROR)
         {
             throw new JSchException("invalid privatekey: " + identity);
         }
         int start = i;
         while (i < len)
         {
             if (buf[i] == unchecked ((int)(0x0a)))
             {
                 bool xd = (buf[i - 1] == unchecked ((int)(0x0d)));
                 System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0));
                 if (xd)
                 {
                     len--;
                 }
                 len--;
                 continue;
             }
             if (buf[i] == '-')
             {
                 break;
             }
             i++;
         }
         encoded_data = Util.FromBase64(buf, start, i - start);
         if (encoded_data.Length > 4 && encoded_data[0] == unchecked ((byte)unchecked ((int)
                                                                                       (0x3f))) && encoded_data[1] == unchecked ((byte)unchecked ((int)(0x6f))) && encoded_data
             [2] == unchecked ((byte)unchecked ((int)(0xf9))) && encoded_data[3] == unchecked ((
                                                                                                   byte)unchecked ((int)(0xeb))))
         {
             // FSecure
             Buffer _buf = new Buffer(encoded_data);
             _buf.GetInt();
             // 0x3f6ff9be
             _buf.GetInt();
             byte[] _type = _buf.GetString();
             //System.err.println("type: "+new String(_type));
             byte[] _cipher   = _buf.GetString();
             string cipherStr = Util.Byte2str(_cipher);
             //System.err.println("cipher: "+cipher);
             if (cipherStr.Equals("3des-cbc"))
             {
                 _buf.GetInt();
                 byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()];
                 _buf.GetByte(foo);
                 encoded_data = foo;
                 encrypted    = true;
                 throw new JSchException("unknown privatekey format: " + identity);
             }
             else
             {
                 if (cipherStr.Equals("none"))
                 {
                     _buf.GetInt();
                     //_buf.getInt();
                     encrypted = false;
                     byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()];
                     _buf.GetByte(foo);
                     encoded_data = foo;
                 }
             }
         }
         if (pubkey == null)
         {
             return;
         }
         buf = pubkey;
         len = buf.Length;
         if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] ==
             '-')
         {
             // FSecure's public key
             i = 0;
             do
             {
                 i++;
             }while (len > i && buf[i] != unchecked ((int)(0x0a)));
             if (len <= i)
             {
                 return;
             }
             while (i < len)
             {
                 if (buf[i] == unchecked ((int)(0x0a)))
                 {
                     bool inheader = false;
                     for (int j = i + 1; j < len; j++)
                     {
                         if (buf[j] == unchecked ((int)(0x0a)))
                         {
                             break;
                         }
                         if (buf[j] == ':')
                         {
                             inheader = true;
                             break;
                         }
                     }
                     if (!inheader)
                     {
                         i++;
                         break;
                     }
                 }
                 i++;
             }
             if (len <= i)
             {
                 return;
             }
             start = i;
             while (i < len)
             {
                 if (buf[i] == unchecked ((int)(0x0a)))
                 {
                     System.Array.Copy(buf, i + 1, buf, i, len - i - 1);
                     len--;
                     continue;
                 }
                 if (buf[i] == '-')
                 {
                     break;
                 }
                 i++;
             }
             publickeyblob = Util.FromBase64(buf, start, i - start);
             if (type == UNKNOWN && publickeyblob.Length > 8)
             {
                 if (publickeyblob[8] == 'd')
                 {
                     type = DSS;
                 }
                 else
                 {
                     if (publickeyblob[8] == 'r')
                     {
                         type = RSA;
                     }
                 }
             }
         }
         else
         {
             if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-')
             {
                 return;
             }
             i = 0;
             while (i < len)
             {
                 if (buf[i] == ' ')
                 {
                     break;
                 }
                 i++;
             }
             i++;
             if (i >= len)
             {
                 return;
             }
             start = i;
             while (i < len)
             {
                 if (buf[i] == ' ' || buf[i] == '\n')
                 {
                     break;
                 }
                 i++;
             }
             publickeyblob = Util.FromBase64(buf, start, i - start);
             if (publickeyblob.Length < 4 + 7)
             {
                 // It must start with "ssh-XXX".
                 if (JSch.GetLogger().IsEnabled(Logger.WARN))
                 {
                     JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key");
                 }
                 publickeyblob = null;
             }
         }
     }
     catch (Exception e)
     {
         //System.err.println("IdentityFile: "+e);
         if (e is JSchException)
         {
             throw (JSchException)e;
         }
         if (e is Exception)
         {
             throw new JSchException(e.ToString(), (Exception)e);
         }
         throw new JSchException(e.ToString());
     }
 }
예제 #10
0
		/// <exception cref="NSch.JSchException"></exception>
		private IdentityFile(string name, byte[] prvkey, byte[] pubkey, JSch jsch)
		{
			this.identity = name;
			this.jsch = jsch;
			try
			{
				Type c;
				c = Sharpen.Runtime.GetType((string)JSch.GetConfig("3des-cbc"));
				cipher = (NSch.Cipher)(System.Activator.CreateInstance(c));
				key = new byte[cipher.GetBlockSize()];
				// 24
				iv = new byte[cipher.GetIVSize()];
				// 8
				c = Sharpen.Runtime.GetType((string)JSch.GetConfig("md5"));
				hash = (HASH)(System.Activator.CreateInstance(c));
				hash.Init();
				byte[] buf = prvkey;
				int len = buf.Length;
				int i = 0;
				while (i < len)
				{
					if (buf[i] == '-' && i + 4 < len && buf[i + 1] == '-' && buf[i + 2] == '-' && buf
						[i + 3] == '-' && buf[i + 4] == '-')
					{
						break;
					}
					i++;
				}
				while (i < len)
				{
					if (buf[i] == 'B' && i + 3 < len && buf[i + 1] == 'E' && buf[i + 2] == 'G' && buf
						[i + 3] == 'I')
					{
						i += 6;
						if (buf[i] == 'D' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
						{
							type = DSS;
						}
						else
						{
							if (buf[i] == 'R' && buf[i + 1] == 'S' && buf[i + 2] == 'A')
							{
								type = RSA;
							}
							else
							{
								if (buf[i] == 'S' && buf[i + 1] == 'S' && buf[i + 2] == 'H')
								{
									// FSecure
									type = UNKNOWN;
									keytype = FSECURE;
								}
								else
								{
									//System.err.println("invalid format: "+identity);
									throw new JSchException("invalid privatekey: " + identity);
								}
							}
						}
						i += 3;
						continue;
					}
					if (buf[i] == 'A' && i + 7 < len && buf[i + 1] == 'E' && buf[i + 2] == 'S' && buf
						[i + 3] == '-' && buf[i + 4] == '2' && buf[i + 5] == '5' && buf[i + 6] == '6' &&
						 buf[i + 7] == '-')
					{
						i += 8;
						if (Session.CheckCipher((string)JSch.GetConfig("aes256-cbc")))
						{
							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 " + identity);
						}
						continue;
					}
					if (buf[i] == 'C' && i + 3 < len && buf[i + 1] == 'B' && buf[i + 2] == 'C' && buf
						[i + 3] == ',')
					{
						i += 4;
						for (int ii = 0; ii < iv.Length; ii++)
						{
							iv[ii] = unchecked((byte)(((A2b(buf[i++]) << 4) & unchecked((int)(0xf0))) + (A2b(
								buf[i++]) & unchecked((int)(0xf)))));
						}
						continue;
					}
					if (buf[i] == unchecked((int)(0x0d)) && i + 1 < len && buf[i + 1] == unchecked((int
						)(0x0a)))
					{
						i++;
						continue;
					}
					if (buf[i] == unchecked((int)(0x0a)) && i + 1 < len)
					{
						if (buf[i + 1] == unchecked((int)(0x0a)))
						{
							i += 2;
							break;
						}
						if (buf[i + 1] == unchecked((int)(0x0d)) && i + 2 < len && buf[i + 2] == unchecked(
							(int)(0x0a)))
						{
							i += 3;
							break;
						}
						bool inheader = false;
						for (int j = i + 1; j < len; j++)
						{
							if (buf[j] == unchecked((int)(0x0a)))
							{
								break;
							}
							//if(buf[j]==0x0d) break;
							if (buf[j] == ':')
							{
								inheader = true;
								break;
							}
						}
						if (!inheader)
						{
							i++;
							encrypted = false;
							// no passphrase
							break;
						}
					}
					i++;
				}
				if (type == ERROR)
				{
					throw new JSchException("invalid privatekey: " + identity);
				}
				int start = i;
				while (i < len)
				{
					if (buf[i] == unchecked((int)(0x0a)))
					{
						bool xd = (buf[i - 1] == unchecked((int)(0x0d)));
						System.Array.Copy(buf, i + 1, buf, i - (xd ? 1 : 0), len - i - 1 - (xd ? 1 : 0));
						if (xd)
						{
							len--;
						}
						len--;
						continue;
					}
					if (buf[i] == '-')
					{
						break;
					}
					i++;
				}
				encoded_data = Util.FromBase64(buf, start, i - start);
				if (encoded_data.Length > 4 && encoded_data[0] == unchecked((byte)unchecked((int)
					(0x3f))) && encoded_data[1] == unchecked((byte)unchecked((int)(0x6f))) && encoded_data
					[2] == unchecked((byte)unchecked((int)(0xf9))) && encoded_data[3] == unchecked((
					byte)unchecked((int)(0xeb))))
				{
					// FSecure
					Buffer _buf = new Buffer(encoded_data);
					_buf.GetInt();
					// 0x3f6ff9be
					_buf.GetInt();
					byte[] _type = _buf.GetString();
					//System.err.println("type: "+new String(_type)); 
					byte[] _cipher = _buf.GetString();
					string cipher2 = Util.Byte2str(_cipher);
					//System.err.println("cipher: "+cipher); 
					if (cipher2.Equals("3des-cbc"))
					{
						_buf.GetInt();
						byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()];
						_buf.GetByte(foo);
						encoded_data = foo;
						encrypted = true;
						throw new JSchException("unknown privatekey format: " + identity);
					}
					else
					{
						if (cipher2.Equals("none"))
						{
							_buf.GetInt();
							//_buf.getInt();
							encrypted = false;
							byte[] foo = new byte[encoded_data.Length - _buf.GetOffSet()];
							_buf.GetByte(foo);
							encoded_data = foo;
						}
					}
				}
				if (pubkey == null)
				{
					return;
				}
				buf = pubkey;
				len = buf.Length;
				if (buf.Length > 4 && buf[0] == '-' && buf[1] == '-' && buf[2] == '-' && buf[3] ==
					 '-')
				{
					// FSecure's public key
					i = 0;
					do
					{
						i++;
					}
					while (len > i && buf[i] != unchecked((int)(0x0a)));
					if (len <= i)
					{
						return;
					}
					while (i < len)
					{
						if (buf[i] == unchecked((int)(0x0a)))
						{
							bool inheader = false;
							for (int j = i + 1; j < len; j++)
							{
								if (buf[j] == unchecked((int)(0x0a)))
								{
									break;
								}
								if (buf[j] == ':')
								{
									inheader = true;
									break;
								}
							}
							if (!inheader)
							{
								i++;
								break;
							}
						}
						i++;
					}
					if (len <= i)
					{
						return;
					}
					start = i;
					while (i < len)
					{
						if (buf[i] == unchecked((int)(0x0a)))
						{
							System.Array.Copy(buf, i + 1, buf, i, len - i - 1);
							len--;
							continue;
						}
						if (buf[i] == '-')
						{
							break;
						}
						i++;
					}
					publickeyblob = Util.FromBase64(buf, start, i - start);
					if (type == UNKNOWN && publickeyblob.Length > 8)
					{
						if (publickeyblob[8] == 'd')
						{
							type = DSS;
						}
						else
						{
							if (publickeyblob[8] == 'r')
							{
								type = RSA;
							}
						}
					}
				}
				else
				{
					if (buf[0] != 's' || buf[1] != 's' || buf[2] != 'h' || buf[3] != '-')
					{
						return;
					}
					i = 0;
					while (i < len)
					{
						if (buf[i] == ' ')
						{
							break;
						}
						i++;
					}
					i++;
					if (i >= len)
					{
						return;
					}
					start = i;
					while (i < len)
					{
						if (buf[i] == ' ' || buf[i] == '\n')
						{
							break;
						}
						i++;
					}
					publickeyblob = Util.FromBase64(buf, start, i - start);
					if (publickeyblob.Length < 4 + 7)
					{
						// It must start with "ssh-XXX".
						if (JSch.GetLogger().IsEnabled(Logger.WARN))
						{
							JSch.GetLogger().Log(Logger.WARN, "failed to parse the public key");
						}
						publickeyblob = null;
					}
				}
			}
			catch (Exception e)
			{
				//System.err.println("IdentityFile: "+e);
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
				if (e is Exception)
				{
					throw new JSchException(e.ToString(), (Exception)e);
				}
				throw new JSchException(e.ToString());
			}
		}
예제 #11
0
		/// <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;
		}