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); } }
/// <exception cref="NSch.JSchException"></exception> public virtual bool SetPassphrase(byte[] _passphrase) { 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(); System.Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } System.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(); System.Array.Copy(tmp, 0, hn, index, tmp.Length); index += tmp.Length; } System.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; } if (e is Exception) { throw new JSchException(e.ToString(), (Exception)e); } throw new JSchException(e.ToString()); } }