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);
 }
示例#2
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);
 }