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);
 }
Esempio n. 2
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);
			}
		}
Esempio n. 3
0
 internal virtual bool Decrypt_rsa()
 {
     byte[] p_array;
     byte[] q_array;
     byte[] dmp1_array;
     byte[] dmq1_array;
     byte[] iqmp_array;
     try
     {
         byte[] plain;
         if (encrypted)
         {
             if (keytype == OPENSSH)
             {
                 cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv);
                 plain = new byte[encoded_data.Length];
                 cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0);
             }
             else
             {
                 if (keytype == FSECURE)
                 {
                     for (int i = 0; i < iv.Length; i++)
                     {
                         iv[i] = 0;
                     }
                     cipher.Init(NSch.Cipher.DECRYPT_MODE, key, iv);
                     plain = new byte[encoded_data.Length];
                     cipher.Update(encoded_data, 0, encoded_data.Length, plain, 0);
                 }
                 else
                 {
                     return(false);
                 }
             }
         }
         else
         {
             if (n_array != null)
             {
                 return(true);
             }
             plain = encoded_data;
         }
         if (keytype == FSECURE)
         {
             // FSecure
             Buffer buf = new Buffer(plain);
             int    foo = buf.GetInt();
             if (plain.Length != foo + 4)
             {
                 return(false);
             }
             e_array = buf.GetMPIntBits();
             d_array = buf.GetMPIntBits();
             n_array = buf.GetMPIntBits();
             byte[] u_array = buf.GetMPIntBits();
             p_array = buf.GetMPIntBits();
             q_array = buf.GetMPIntBits();
             return(true);
         }
         int index  = 0;
         int length = 0;
         if (plain[index] != unchecked ((int)(0x30)))
         {
             return(false);
         }
         index++;
         // SEQUENCE
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         if (plain[index] != unchecked ((int)(0x02)))
         {
             return(false);
         }
         index++;
         // INTEGER
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         index += length;
         //System.err.println("int: len="+length);
         //System.err.print(Integer.toHexString(plain[index-1]&0xff)+":");
         //System.err.println("");
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         n_array = new byte[length];
         System.Array.Copy(plain, index, n_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         e_array = new byte[length];
         System.Array.Copy(plain, index, e_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         d_array = new byte[length];
         System.Array.Copy(plain, index, d_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         p_array = new byte[length];
         System.Array.Copy(plain, index, p_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         q_array = new byte[length];
         System.Array.Copy(plain, index, q_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         dmp1_array = new byte[length];
         System.Array.Copy(plain, index, dmp1_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         dmq1_array = new byte[length];
         System.Array.Copy(plain, index, dmq1_array, 0, length);
         index += length;
         index++;
         length = plain[index++] & unchecked ((int)(0xff));
         if ((length & unchecked ((int)(0x80))) != 0)
         {
             int foo = length & unchecked ((int)(0x7f));
             length = 0;
             while (foo-- > 0)
             {
                 length = (length << 8) + (plain[index++] & unchecked ((int)(0xff)));
             }
         }
         iqmp_array = new byte[length];
         System.Array.Copy(plain, index, iqmp_array, 0, length);
         index += length;
     }
     catch (Exception)
     {
         //System.err.println(e);
         return(false);
     }
     return(true);
 }