数据加密总结进阶(4)

 在这篇中我们将学习如何在传输过程中不窜改数据而达到安全目的.我们用的技术是哈希算法.哈希值会验证数据的完整性.接收到的数据的哈希值可以和被传出去的数据的哈希值进行比较,看它是否被窜改!

Net框架中提供了下面这几个主要的运用哈希的类:

  • SHA1Managed
  • MD5CryptoServiceProvider
  • MACTripleDES

因为SHA1现在已经被破译,所以我们将使用MD5CryptoServiceProvider 类生成哈希值.

 

我们将创建一个Helper类来帮助我们用MD5算法创建和验证哈希值.这个类包含两个方法:GetHash()和VerifyHash();GetHash()方法接收一个要被生成哈希值的字符串,并以字节数组类型返回这个窜的哈希值.VerifyHash()方法接收GetHash()方法返回的哈希值和要进行比较的字符串,如果在传输过程中没有被窜改这个方法将返加True,否则返回False;

public class MD5HashHelper
{
public byte[] GetHash(string message)
{
byte[] data;
data=System.Text.UTF8Encoding.ASCII.GetBytes(message);
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
return md5.ComputeHash(data,0,data.Length);
}
public bool VerifyHash(string message, byte[] hash)
{
byte[] data;
data=System.Text.UTF8Encoding.ASCII.GetBytes(message);
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
byte[] hashtemp=md5.ComputeHash(data,0,data.Length);
for(int x = 0; x < hash.Length;x++)
{
if (hash[x] != hashtemp[x])
{
return false;
}
}
return true;
}
}

Let's dissect the code step by step:让我们一步步解释:

  • We first need to import System.Security.Cryptography namespace in your class

        导入命名空间:System.Security.Cryptography

  • The GetHash() accepts string whose hash value is to be generated and returns the computed hash as a byte array.

        GetHash()方法接收一个要被生成哈希值的字符串,并以字节数组类型返回这个窜的哈希值.

  • Inside the function we used UTF8Encoding class and get aa byte representation of the string to be transfered.

        在方法内部我们用UTF8Encoding类得到要被传输字符串的字节类型.

  • We then create an instance of MD5CryptoServiceProvider class and call its ComputeHash by passing the byte created above to it.

        然后,我们创建MD5CryptoServiceProvider类的实例,并且通过传递上一步创建的字节调用这个实例的ComputeHash()方法.

  • The ComputeHash() function generates the hash for the given data and returns another byte array that represents the hash value of the data.

        ComputeHash()方法生成数据的哈希值,并返回这个数据的哈希值的数组.

  • The VerifyHash() function accepts the message as it was received and the hash generated previously and returns true if the message is not altered during transmit otherwise returns false.

       VerifyHash()方法接收GetHash()方法返回的哈希值和要进行比较的字符串,如果在传输过程中没有被窜改这个方法将返加True,否则返回False;

  • Inside this function we again use UTF8Encoding class and generate byte representation of the received message.

       在这个方法内我们再次用到了UTF8Encoding 类生成接收数据的字数组.

  • We then compute hash for this data using the same ComputeHash() method of MD5CryptoServiceProvider class.

       然后我们用MD5CryptoServiceProvider类的 ComputeHash() 方法来运算哈希值.

  • Finally, we run a for loop and check each and every byte of original hash value and the hash we generated above. If both the hash values are matching we can conclude that the data is not tampered.

        最后,我们用一个循环检查原字符串的哈希值和我们生成的有什么不同.如果哈希值匹配我们就能断定数据没被窜改过.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值