Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# .NET加密问题_C#_.net_Encryption_Encryption Symmetric - Fatal编程技术网

C# .NET加密问题

C# .NET加密问题,c#,.net,encryption,encryption-symmetric,C#,.net,Encryption,Encryption Symmetric,我参考了这篇文章,我正在尝试编写一个加密字符串,而不是纯文本。下面是我正在使用的代码: TextWriter tw = new StreamWriter("c:\\temp\\test.txt"); string plainString = "String to be encrypted"; PasswordDeriveBytes pdb = new PasswordDeriveBytes("Test",new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d

我参考了这篇文章,我正在尝试编写一个加密字符串,而不是纯文本。下面是我正在使用的代码:

TextWriter tw = new StreamWriter("c:\\temp\\test.txt");
string plainString = "String to be encrypted";
PasswordDeriveBytes pdb = new PasswordDeriveBytes("Test",new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
tw.WriteLine(alg.IV.ToString());
MemoryStream ms = new MemoryStream(); 
CryptoStream cs = new CryptoStream(ms,alg.CreateEncryptor(), CryptoStreamMode.Write);
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(plainString);
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
tw.WriteLine(ms.ToString());
ms.Close();
tw.Flush();

但是,当我打开文件时,我得到的是System.IO.MemoryStream,而不是一些加密字符。我错过了什么?

MemoryStream.ToString依赖于Object.ToString的默认实现,它会写出类名。创建一个文件流并将其传递给CryptoStream构造函数


MemoryStream.ToString依赖于Object.ToString的默认实现,它会写出类名。创建一个文件流并将其传递给CryptoStream构造函数


我认为.net非常支持使用MD5算法加密字符串。如果要使用MD5,请参阅以下代码

private void encrypt(ref string password)
    {
        Int32 counter;
        Char[] passwordArr;
        String encryptedPassword;
        Byte[] hashedPassword;
        MD5CryptoServiceProvider obj = new MD5CryptoServiceProvider();

        passwordArr = password.ToCharArray();
        Byte[] passwordBytes = new byte[passwordArr.Length - 1];
        for (counter = 0; counter < passwordBytes.Length; counter++)
        {
            passwordBytes[counter] = Convert.ToByte(passwordArr[counter]);
        }
        hashedPassword = obj.ComputeHash(passwordBytes);
        encryptedPassword = BitConverter.ToString(hashedPassword);
        password =  encryptedPassword;
        obj = null;
    }

我认为.net非常支持使用MD5算法加密字符串。如果要使用MD5,请参阅以下代码

private void encrypt(ref string password)
    {
        Int32 counter;
        Char[] passwordArr;
        String encryptedPassword;
        Byte[] hashedPassword;
        MD5CryptoServiceProvider obj = new MD5CryptoServiceProvider();

        passwordArr = password.ToCharArray();
        Byte[] passwordBytes = new byte[passwordArr.Length - 1];
        for (counter = 0; counter < passwordBytes.Length; counter++)
        {
            passwordBytes[counter] = Convert.ToByte(passwordArr[counter]);
        }
        hashedPassword = obj.ComputeHash(passwordBytes);
        encryptedPassword = BitConverter.ToString(hashedPassword);
        password =  encryptedPassword;
        obj = null;
    }

问题是托斯特林女士。您应该从memorystream读取字节,将其更改为适当的编码,然后写入文本流。

问题在于ms.ToString。您应该从memorystream读取字节,将其更改为适当的编码,然后写入文本流。

愚蠢的问题,如果我使用MD5,我可以解密我的字符串吗?如何执行?您的代码假定char==byte,这是错误的。改为使用Encoding.GetBytes.-1 MD5是一种散列算法,而不是加密算法,即它只能单向运行。您无法从MD5哈希值中检索原始明文。直到今天,我仍然认为这两个值是相似的。感谢您对散列和加密之间的澄清。愚蠢的问题,如果我使用MD5,我可以解密我的字符串吗?如何执行?您的代码假定char==byte,这是错误的。改为使用Encoding.GetBytes.-1 MD5是一种散列算法,而不是加密算法,即它只能单向运行。您无法从MD5哈希值中检索原始明文。直到今天,我仍然认为这两个值是相似的。感谢您对哈希和加密之间的澄清。byte[]bytes=ms.ToArray;字符串s=Encoding.GetEncodingwhatever.GetStringbytes,0,bytes.Length;tw.WriteLines;字节[]字节=ms.ToArray;字符串s=Encoding.GetEncodingwhatever.GetStringbytes,0,bytes.Length;tw.WriteLines;您忘记阅读和理解您复制和粘贴的任何代码。您忘记阅读和理解您复制和粘贴的任何代码。