Exemplo n.º 1
0
        byte[] genKey(byte[] passphrase, byte[] iv)
        {
            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();
                        Array.Copy(tmp, 0, hn, index, tmp.Length);
                        index += tmp.Length;
                    }
                    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();
                        Array.Copy(tmp, 0, hn, index, tmp.Length);
                        index += tmp.Length;
                    }
                    Array.Copy(hn, 0, key, 0, key.Length);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
            }
            return(key);
        }
Exemplo n.º 2
0
 public bool setPassphrase(byte[] _passphrase)
 {
     /*
      * hash is MD5
      * h(0) <- hash(passphrase, iv);
      * h(n) <- hash(h(n-1), passphrase, iv);
      * key <- (h(0),...,h(n))[0,..,key.Length];
      */
     try
     {
         if (encrypted)
         {
             if (_passphrase == null)
             {
                 return(false);
             }
             byte[] passphrase = _passphrase;
             int    hsize      = hash.getBlockSize();
             byte[] hn         = new byte[key.Length / hsize * hsize +
                                          (key.Length % hsize == 0 ? 0 : hsize)];
             byte[] tmp = null;
             if (keytype == 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();
                     Array.Copy(tmp, 0, hn, index, tmp.Length);
                     index += tmp.Length;
                 }
                 Array.Copy(hn, 0, key, 0, key.Length);
             }
             else if (keytype == 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();
                     Array.Copy(tmp, 0, hn, index, tmp.Length);
                     index += tmp.Length;
                 }
                 Array.Copy(hn, 0, key, 0, key.Length);
             }
             Util.bzero(passphrase);
         }
         if (decrypt())
         {
             encrypted = false;
             return(true);
         }
         P_array = Q_array = G_array = pub_array = prv_array = null;
         return(false);
     }
     catch (Exception e)
     {
         if (e is JSchException)
         {
             throw (JSchException)e;
         }
         throw new JSchException(e.Message, e);
     }
 }