internal 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.WriteLine(e); } return(key); }
public bool setPassphrase(String _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 = System.Text.Encoding.Default.GetBytes(_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); 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); } } 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 SshClientException) { throw (SshClientException)e; } throw new SshClientException(e.ToString()); } }