DES加密的值大于128 [英] DES encryption for value larger than 128

查看:120
本文介绍了DES加密的值大于128的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当我想加密文本(使用C#中的DES算法)时遇到问题,该文本包含一些字符,其ASCII码大于127.

我已经通过一些在线站点(例如" http://www.tero.co.uk/des/test)检查了结果.php ",当我使用纯文本作为数据时,一切都OK.但是在某些情况下,当数据中包含一些特殊字符时,结果将变得不同.

这是我的代码:

Hi Guys,

I have a problem when I want to encrypt a text (with DES algorithm in C#) which has some characters with ASCII code larger than 127.

I have checked the result with some Online site like "http://www.tero.co.uk/des/test.php" and every thing is OK when I use plain text as data. But in some situations when there are some special characters in the data, the result will become different.

This is my Code:

// key and data are given in Hex
public string PerformDES(string Key,string Data)
      {
          string sRet = string.Empty;
          DES oDes = new DESCryptoServiceProvider();
          System.IO.MemoryStream oOutStream = new System.IO.MemoryStream();


          oDes.Key = HexToString(Key); // It returns byte[]
          oDes.IV = new byte[8];
          Data = HexToString1(Data); //It returns string

          CryptoStream encStream = new CryptoStream(oOutStream, oDes.CreateEncryptor(), CryptoStreamMode.Write);

          System.IO.StreamWriter sw = new System.IO.StreamWriter(encStream);

          sw.WriteLine(Data);

          sw.Flush();
          encStream.FlushFinalBlock();
          sw.Flush();

          sRet = StringToHex(oOutStream.ToArray()); // It convert the result to string
          sRet = sRet.Substring(0, 16);
          sw.Close();
          encStream.Close();

          oOutStream.Close();
          oOutStream.Dispose();
          oOutStream = null;
          sw.Dispose();
          sw = null;
          encStream.Dispose();
          encStream = null;
          oDes.Clear();
          oDes = null;

          return sRet;
      }



我该怎么办?

谢谢.



What should I do?

Thanks.

推荐答案

我发现了.我应该用BinaryWriter代替StreamWriter.我不知道为什么,但似乎StreamWriter会使用大于127的ascii代码更改字符!
I found that. I should replace StreamWriter by BinaryWriter. I do not know why but it seems that StreamWriter changes characters with ascii code bigger than 127!


这篇关于DES加密的值大于128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆