Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CryptoServiceProvider哈希输出与联机哈希生成器不匹配?_C#_.net - Fatal编程技术网

C# CryptoServiceProvider哈希输出与联机哈希生成器不匹配?

C# CryptoServiceProvider哈希输出与联机哈希生成器不匹配?,c#,.net,C#,.net,我是一名学生,对编程一无所知。这是我尝试编写的第一个C#程序。程序应该读入一个明文文本文件,然后输出一个带有MD5和SHA-1散列的文本文件。据我所知,该程序工作正常,但是我的程序创建的哈希与在线哈希生成器生成的哈希不匹配。据我所知,如果输入的单词相同,哈希应该匹配。我的代码是否有任何明显的错误会导致这种情况 using System; using System.Text; using System.IO; using System.Security.Cryptography; namespa

我是一名学生,对编程一无所知。这是我尝试编写的第一个C#程序。程序应该读入一个明文文本文件,然后输出一个带有MD5和SHA-1散列的文本文件。据我所知,该程序工作正常,但是我的程序创建的哈希与在线哈希生成器生成的哈希不匹配。据我所知,如果输入的单词相同,哈希应该匹配。我的代码是否有任何明显的错误会导致这种情况

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace CreateHash
{
class Program
{
    const string INPUTFILE = "C:\\ClearTextPasswords.txt";
    const string OUTPUTFILEMD5 = "C:\\EncryptedMD5Passwords.txt";
    const string OUTPUTFILESHA1 = "C:\\EncryptedSHA1Passwords.txt";

    static void Main(string[] args)
    {
        string input;
        int counter = 0;

        DeleteWriteFile();

        StreamReader readFile = new StreamReader(INPUTFILE);

        while ((input = readFile.ReadLine()) != null)
        {
            if(AppendFile(input))
                counter++;
        }

        readFile.Close();

        Console.WriteLine("\nSuccessfully wrote {0} encrypted password(s) to: \n{1} and\n{2} files.\n\nPress any key to exit...", counter.ToString(), OUTPUTFILEMD5, OUTPUTFILESHA1);
        Console.ReadKey();
    }

    static void DeleteWriteFile()
    {
        //Example of try/catch block
        try
        {
            System.IO.File.Delete(OUTPUTFILEMD5);
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error deleting the MD5 Output file: " + ex.Message);
        }

        try
        {
            System.IO.File.Delete(OUTPUTFILESHA1);
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error deleting the MD5 Output file: " + ex.Message);
        }
    }

    static bool AppendFile(string str)
    {
        int x = 0;
        int y = 0;
        bool result = false;

        StreamWriter writeFile1;
        StreamWriter writeFile2;

        HashToolkit hashToolkit = new HashToolkit();

        //Example of try/catch/finally block
        try
        {
            writeFile1 = new StreamWriter(OUTPUTFILEMD5, true);
            writeFile1.WriteLine(hashToolkit.GetMd5(str));
            writeFile1.Close();
            x = 1;
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error writing to the MD5 Output file: " + ex.Message);
        }
        finally
        {
            writeFile1 = null;
        }

        try
        {
            writeFile2 = new StreamWriter(OUTPUTFILESHA1, true);
            writeFile2.WriteLine(hashToolkit.GetSha1(str));
            writeFile2.Close();
            y = 1;
        }
        catch (Exception ex)
        {
            Console.WriteLine("There was an error writing to the SHA1 Output file: " + ex.Message);
        }
        finally
        {
            writeFile2 = null;
        }

        if (x * y != 0)
            result = true;

        return result;
    }
}

public class HashToolkit
{
    public HashToolkit()
    {
    }

    public string GetMd5(string str)
    {
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        string input = string.Empty;

        byte[] hashedData = md5.ComputeHash(Encoding.Unicode.GetBytes(str));

        foreach (byte b in hashedData)
        {
            input += String.Format("{0,2:X2}", b);
        }
        return input;
    }

    public string GetSha1(string str)
    {
        SHA1 sha = new SHA1CryptoServiceProvider();
        string input = string.Empty;

        byte[] hashedData = sha.ComputeHash(Encoding.Unicode.GetBytes(str));

        foreach (byte b in hashedData)
        {
            input += String.Format("{0,2:X2}", b);
        }
        return input;
    }
}
}

任何帮助都将不胜感激。谢谢你抽出时间

这是因为你不能散列字符串,你只能散列字节。因此,您需要首先转换为字节。您正在为此使用UTF16。在线工具可能使用UTF8或某种ASCII样式的编码。找出它使用的是什么,然后也使用它。

在散列之前,您正在将文件转换为UTF-16-LE。为什么首先要将is解码为文本,而不是直接使用字节?或者,您可以使用
Encoding.UTF8
而不是
Encoding.Unicode
。只需使用