C# DES加密后存储,解密后显示问题。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# DES加密后存储,解密后显示问题。相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Text;
using DIS_IDAL;
using System.Security.Cryptography;
using System.IO;
using DIS_Models;

namespace DIS_DAL

public class DESCryptograhy : ISymmetricAlgorithm

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
CryptoStream encStream;

public object Read()

//根据密钥来对对象进行解密则返回
try

FileStream fs = new FileStream("d:\\1.txt", FileMode.Open, FileAccess.Read);
encStream = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(encStream);
object o = sr.ReadLine();
key.Clear();
sr.Close();
return o;

catch (Exception e)

Console.WriteLine(e.Message);
return "error";



public void Write(string obj)

//加密后写入到存储介质上
CryptograhyInfo cInfo = new CryptograhyInfo();
FileStream fs = new FileStream("d:\\1.txt", FileMode.OpenOrCreate, FileAccess.Write);
encStream = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(encStream);
sw.WriteLine(obj);
sw.Close();



在Read方法里的object o = sr.ReadLine();为什么老是出错,变量obj总取不到值。
谢谢下面的朋友的建议,但提到用ReadToEnd还是出错的,之前也我在ConsoleApplication下用Console.WriteLine(sr.ReadLine())是可以的。。我就是想做加密类把分别做写和读,但是总是不能把读的结果解密后返回来。头疼……希望有心朋友帮帮忙

参考技术A 在我这里测试你的代码是可以的,不过我把object o = sr.ReadLine();改成了object o = sr.ReadToEnd(); 参考技术B 刚刚看了下,好像是乱码。我这里改为ReadToEnd() 也会报错。

会报这个错误:Length of the data to decrypt is invalid
我现在一时解决不了...

您好,我现在解决了,但是你的那种读取方式,我还是修改不了,这个是我的修改方案,我把aspx页面 和 aspx.cs页面完整贴出来,你可以试下:
aspx页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DES加密</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtWrite" runat="server" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="btnWrite" runat="server" Text="写入txt并用DES加密"
onclick="btnWrite_Click" />
<br />
<br />
<asp:Button ID="btnRead" runat="server" Text="读取Des加密的txt内容:"
onclick="btnRead_Click" />
<asp:Label ID="lblReadContent" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
*****************************************************
aspx.cs页面
/// <summary>
/// 读取文件并解密
/// </summary>
/// <returns></returns>
public object Read()

//根据密钥来对对象进行解密则返回
try

using (FileStream fs = new FileStream("d:\\1.txt", FileMode.Open, FileAccess.Read))

using (StreamReader reader = new StreamReader(fs,System.Text.Encoding.UTF8))

string obj = reader.ReadToEnd();
return Decrypt(obj, "12345678");




catch (Exception e)

Console.WriteLine(e.Message);
return "error";



/// <summary>
/// 写入文件(加密)
/// </summary>
/// <param name="obj">要加密的字符串</param>
public void Write(string obj)

string txtValue = Encrypt(obj, "12345678");

using (FileStream fs = new FileStream("d:\\1.txt", FileMode.OpenOrCreate, FileAccess.Write))

using (StreamWriter sw = new StreamWriter(fs,Encoding.UTF8))

sw.Write(txtValue);





protected void btnWrite_Click(object sender, EventArgs e)

Write(txtWrite.Text);

protected void btnRead_Click(object sender, EventArgs e)

object objRead = Read();
this.lblReadContent.Text = objRead.ToString();


/// <summary>
/// 加密
/// </summary>
/// <param name="pToEncrypt"></param>
/// <param name="sKey">注意,这个sKey长度必须为8,可以为任意字母或数字,但是解密和加密时sKey必须一致</param>
/// <returns></returns>
public string Encrypt(string pToEncrypt, string sKey)

DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
using (MemoryStream ms = new MemoryStream())

CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())

ret.AppendFormat("0:X2", b);

ret.ToString();
return ret.ToString();



/// <summary>
/// 解密
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public string Decrypt(string pToDecrypt, string sKey)

using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())


byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)

int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;


des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
using (MemoryStream ms = new MemoryStream())

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilder ret = new StringBuilder();

return System.Text.Encoding.UTF8.GetString(ms.ToArray());


本回答被提问者和网友采纳

以上是关于C# DES加密后存储,解密后显示问题。的主要内容,如果未能解决你的问题,请参考以下文章

DES加密解密问题,java 和 C#

java的 DES 加密解密方法 求对应C#的加密解密方法,急切

JavaScript与C#互通的DES加解密算法

使用des对文件加密后怎么解密

使用C# DES解密java DES加密的字符串

c# DES加密解密