/// <summary> /// Decrypts encrypted private key file data. /// </summary> /// <param name="cipherInfo">The cipher info.</param> /// <param name="cipherData">Encrypted data.</param> /// <param name="passPhrase">Decryption pass phrase.</param> /// <param name="binarySalt">Decryption binary salt.</param> /// <returns>Decrypted byte array.</returns> /// <exception cref="System.ArgumentNullException">cipherInfo</exception> /// <exception cref="ArgumentNullException"><paramref name="cipherInfo" />, <paramref name="cipherData" />, <paramref name="passPhrase" /> or <paramref name="binarySalt" /> is null.</exception> private static byte[] DecryptKey(CipherInfo cipherInfo, byte[] cipherData, string passPhrase, byte[] binarySalt) { if (cipherInfo == null) { throw new ArgumentNullException("cipherInfo"); } if (cipherData == null) { throw new ArgumentNullException("cipherData"); } if (binarySalt == null) { throw new ArgumentNullException("binarySalt"); } List <byte> cipherKey = new List <byte>(); using (var md5 = new MD5Hash()) { var passwordBytes = Encoding.UTF8.GetBytes(passPhrase); // Use 8 bytes binary salkt var initVector = passwordBytes.Concat(binarySalt.Take(8)); var hash = md5.ComputeHash(initVector.ToArray()).AsEnumerable(); cipherKey.AddRange(hash); while (cipherKey.Count < cipherInfo.KeySize / 8) { hash = hash.Concat(initVector); hash = md5.ComputeHash(hash.ToArray()); cipherKey.AddRange(hash); } } var cipher = cipherInfo.Cipher(cipherKey.ToArray(), binarySalt); return(cipher.Decrypt(cipherData)); }
public void TestMD5() { HashAdapter hash = new MD5Hash(); string src1 = @"ABC"; string result1 = hash.ComputeHash(src1); Assert.AreEqual(32, result1.Length); string hashed1 = @"902fbdd2b1df0c4f70b4a5d23525e932"; Assert.AreEqual(hashed1, result1); // 日本語を含む文字列は? string src2 = @"あいう"; string result2 = hash.ComputeHash(src2); Assert.AreEqual(32, result2.Length); string hashed2 = @"df5c588826b00952db2ff6c8829cb086"; Assert.AreEqual(hashed2, result2); }
private static byte[] GetCipherKey(string passphrase, int length) { var cipherKey = new List <byte>(); using (var md5 = new MD5Hash()) { var passwordBytes = Encoding.UTF8.GetBytes(passphrase); var hash = md5.ComputeHash(passwordBytes); cipherKey.AddRange(hash); while (cipherKey.Count < length) { hash = passwordBytes.Concat(hash).ToArray(); hash = md5.ComputeHash(hash); cipherKey.AddRange(hash); } } return(cipherKey.Take(length).ToArray()); }
/// <summary> /// Initializes a new instance of the <see cref="HostKeyEventArgs"/> class. /// </summary> /// <param name="host">The host.</param> public HostKeyEventArgs(KeyHostAlgorithm host) { this.CanTrust = true; // Set default value this.HostKey = host.Data; this.KeyLength = host.Key.KeyLength; using (var md5 = new MD5Hash()) { this.FingerPrint = md5.ComputeHash(host.Data); } }