// Computes a keyed hash for a source file, creates a target file with the keyed hash // prepended to the contents of the source file, then decrypts the file and compares // the source and the decrypted files. /// <summary> /// 文件加密 /// </summary> /// <param name="key"></param> /// <param name="sourceFile">待加密的文件</param> /// <param name="destFile">加密后的文件</param> public static void EncodeFile(byte[] key, String sourceFile, String destFile) { // Initialize the keyed hash object. HMACMD5 myhmacMD5 = new HMACMD5(key); FileStream inStream = new FileStream(sourceFile, FileMode.Open); FileStream outStream = new FileStream(destFile, FileMode.Create); // Compute the hash of the input file. byte[] hashValue = myhmacMD5.ComputeHash(inStream); // Reset inStream to the beginning of the file. inStream.Position = 0; // Write the computed hash value to the output file. outStream.Write(hashValue, 0, hashValue.Length); // Copy the contents of the sourceFile to the destFile. int bytesRead; // read 1K at a time byte[] buffer = new byte[1024]; do { // Read from the wrapping CryptoStream. bytesRead = inStream.Read(buffer, 0, 1024); outStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); myhmacMD5.Clear(); // Close the streams inStream.Close(); outStream.Close(); return; }
static byte[] Compute_NTLMv2 (Type2Message type2, string username, string password, string domain) { var ntlm_hash = Compute_NTLM_Password (password); var ubytes = Encoding.Unicode.GetBytes (username.ToUpperInvariant ()); var tbytes = Encoding.Unicode.GetBytes (domain); var bytes = new byte [ubytes.Length + tbytes.Length]; ubytes.CopyTo (bytes, 0); Array.Copy (tbytes, 0, bytes, ubytes.Length, tbytes.Length); var md5 = new HMACMD5 (ntlm_hash); var ntlm_v2_hash = md5.ComputeHash (bytes); Array.Clear (ntlm_hash, 0, ntlm_hash.Length); md5.Clear (); var ntlm_v2_md5 = new HMACMD5 (ntlm_v2_hash); var now = DateTime.Now; var timestamp = now.Ticks - 504911232000000000; var nonce = new byte [8]; var rng = RandomNumberGenerator.Create (); rng.GetBytes (nonce); byte[] blob = new byte [28 + type2.TargetInfo.Length]; blob[0] = 0x01; blob[1] = 0x01; Buffer.BlockCopy (BitConverterLE.GetBytes (timestamp), 0, blob, 8, 8); Buffer.BlockCopy (nonce, 0, blob, 16, 8); Buffer.BlockCopy (type2.TargetInfo, 0, blob, 28, type2.TargetInfo.Length); var challenge = type2.Nonce; var hashInput = new byte [challenge.Length + blob.Length]; challenge.CopyTo (hashInput, 0); blob.CopyTo (hashInput, challenge.Length); var blobHash = ntlm_v2_md5.ComputeHash (hashInput); var response = new byte [blob.Length + blobHash.Length]; blobHash.CopyTo (response, 0); blob.CopyTo (response, blobHash.Length); Array.Clear (ntlm_v2_hash, 0, ntlm_v2_hash.Length); ntlm_v2_md5.Clear (); Array.Clear (nonce, 0, nonce.Length); Array.Clear (blob, 0, blob.Length); Array.Clear (hashInput, 0, hashInput.Length); Array.Clear (blobHash, 0, blobHash.Length); return response; }
/// <summary> /// Computes the hash. /// </summary> /// <param name="version">The version.</param> /// <param name="header">The header.</param> /// <param name="parameters">The parameters.</param> /// <param name="data">The scope data.</param> /// <param name="privacy">The privacy provider.</param> /// <param name="length">The length bytes.</param> /// <returns></returns> public OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData data, IPrivacyProvider privacy, byte[] length) { if (header == null) { throw new ArgumentNullException("header"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } if (data == null) { throw new ArgumentNullException("data"); } if (privacy == null) { throw new ArgumentNullException("privacy"); } var key = PasswordToKey(_password, parameters.EngineId.GetRaw()); using (var md5 = new HMACMD5(key)) { var hash = md5.ComputeHash(ByteTool.PackMessage(length, version, header, parameters, data).ToBytes()); md5.Clear(); var result = new byte[DigestLength]; Buffer.BlockCopy(hash, 0, result, 0, result.Length); return new OctetString(result); } }
/// <summary> /// Computes a keyed hash for a source file, creates a target file with the keyed hash /// prepended to the contents of the source file, then decrypts the file and compares /// the source and the decrypted files. /// </summary> /// <param name="key">The key to use to encode the file</param> /// <param name="sourceFile">The file to encrypt complete path</param> /// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param> public void EncodeFile(string key, String sourceFile, String destFile) { if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile)) throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null"); if (destFile.IsNullOrWhiteSpace()) throw new ArgumentException("Please specify the path of the output path", nameof(destFile)); if (string.IsNullOrEmpty(key)) throw new ArgumentException("Please specify the key", nameof(key)); // Create a key using a random number generator. This would be the // secret key shared by sender and receiver. byte[] secretkey = key.ToByteArray(); // Initialize the keyed hash object. HMACMD5 myhmacMD5 = new HMACMD5(secretkey); FileStream inStream = new FileStream(sourceFile, FileMode.Open); FileStream outStream = new FileStream(destFile, FileMode.Create); // Compute the hash of the input file. byte[] hashValue = myhmacMD5.ComputeHash(inStream); // Reset inStream to the beginning of the file. inStream.Position = 0; // Write the computed hash value to the output file. outStream.Write(hashValue, 0, hashValue.Length); // Copy the contents of the sourceFile to the destFile. int bytesRead; // read 1K at a time byte[] buffer = new byte[1024]; do { // Read from the wrapping CryptoStream. bytesRead = inStream.Read(buffer, 0, 1024); outStream.Write(buffer, 0, bytesRead); } while (bytesRead > 0); myhmacMD5.Clear(); // Close the streams inStream.Close(); outStream.Close(); }
/// <summary> /// Computes the hash. /// </summary> /// <param name="version">The version.</param> /// <param name="header">The header.</param> /// <param name="parameters">The parameters.</param> /// <param name="scopeBytes">The scope bytes.</param> /// <param name="privacy">The privacy provider.</param> /// <returns></returns> private OctetString ComputeHash(VersionCode version, ISegment header, SecurityParameters parameters, ISnmpData scopeBytes, IPrivacyProvider privacy) { if (scopeBytes == null) { throw new ArgumentNullException("scopeBytes"); } byte[] key = PasswordToKey(_password, parameters.EngineId.GetRaw()); #if ! SILVERLIGHT //mc++ using (HMACMD5 md5 = new HMACMD5(key)) { byte[] hash = md5.ComputeHash(SnmpMessageExtension.PackMessage(version, header, parameters, scopeBytes, privacy).ToBytes()); md5.Clear(); byte[] result = new byte[DigestLength]; Array.Copy(hash, result, result.Length); return new OctetString(result); } #endif return null; }