C#常用的加密解密方法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
p.s.内容摘自百度百科
测试代码:
usingSystem;
usingSystem.Text;
namespace Base64Test {
class Program {
staticvoidMain(string[] args) {
stringsource ="Happy Birthday!";
stringencryStr = EncryptBase64(source);
returnConvert.ToBase64String(inputBytes);
}
/// <summary>
/// base64解密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
staticstringDecryptBase64(stringinput) {
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] result = sha.ComputeHash(inputBytes);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
/// <param name="hash">原字符串的md5码</param>
/// <returns></returns>
static bool VerifyMd5Hash(string input, string hash)
{
string hashOfInput = GetMd5Hash(input);
string hash = GetMd5Hash(source);
Console.WriteLine("The MD5 hash of " + source + " is: " + hash);
Console.WriteLine("Verifying the hash ...");
if (VerifyMd5Hash(source, hash))
/// </summary>
/// <param name="input">原字符串</param>
/// <returns></returns>
static string GetMd5Hash(string input)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
}
}
SHA1
安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,这时候就会产生不同的消息摘要。SHA1有如下特性:
DES
对称加密算法
DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),
并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。
在某些文献中,作为算法的DES称为数据加密算法(Data Encryption Algorithm,DSA),已与作为标准的DES区分开来。
C#常用的加密解密方法
开篇
C#
数据校验型
MD5
Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)。
①不可以从消息摘要中复原信息
②两个不同的消息不会产生同样的消息摘要,(但会有1x10 ^ 48分之一的机率出现相同的消息摘要,一般使用时忽略)。
p.s.内容摘自百度百科
sha1和md5的用途类似,用来验证数据。
测试代码:
演示通过sha1获取一个字符串的hash并做验证:
using System;
using System.Security.Cryptography;
P.s.内容摘自百度百科
测试代码:
usingSystem;
usingSystem.IO;
usingSystem.Security.Cryptography;
usingSystem.Text;
namespace DESTest {
class Program {
staticvoidMain(string[] args) {
byte[] inputBytes = Encoding.Default.GetBytes(input);
byte[] data = md5Hasher.ComputeHash(inputBytes);
StringBuilder sBuilder = new StringBuilder();
//将data中的每个字符都转换为16进制的
byte[] inputBytes = Convert.FromBase64String(input);
returnEncoding.UTF8.GetString(inputBytes);
}
}
}
base64的加密解密规则比较固定,所以破解也容易,在网上找个在线base64解密就可以破解加密后的内容,所以敏感数据不推荐使用base64。
/// <summary>
/// base64加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
StaticstringEncryptBase64(stringinput) {
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
Console.WriteLine("原数据:"+ source);
Console.WriteLine("加密后的数据:"+ encryStr);
stringdecryStr = DecryptBase64(encryStr);
Console.WriteLine("解密后的数据:"+ decryStr); Console.ReadKey(); }
}
else
{
Console.WriteLine("The Hashes are not same...");
}
Console.ReadKey();
}
static string GetSha1Hash(string input)
{
byte[] inputBytes = Encoding.Default.GetBytes(input);
stringkey ="The_key!";
stringsource ="Happy Birthday!";
Console.WriteLine("Source string: "+ source);
stringencryptStr = EncryptDES(source, key);
Console.WriteLine("Encrypt string: "+ encryptStr);
{
sBuilder.Append(result[i].ToString("x2"));
}
Байду номын сангаасreturn sBuilder.ToString();
}
static bool VerifySha1Hash(string input, string hash)
{
string hashOfInput = GetSha1Hash(input);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
}
}
加密数据传输
这个类型的加密可以解密,主要用来把加密后的数据进行传输,对方收到数据后进行解密。
using System.Security.Cryptography;
using System.Text;
namespace MD5Test
{
class Program
{
static void Main(string[] args)
{
string source = "Happy Birthday!";
{
Console.WriteLine("The hashes are the same.");
}
else
{
Console.WriteLine("The hashes are not same.");
}
Console.ReadKey();
}
/// <summary>
///获取一个字符串的32位16进制字符串格式MD5码
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法),主流编程语言普遍已有MD5实现。将数据(如汉字)运算为另一固定长度值,是杂凑算法的基础原理,MD5的前身有MD2、MD3和MD4。
p.s.内容来自百度百科。
using System.Text;
namespace SHA1Test
{
class Program
{
static void Main(string[] args)
{
string source = "Happy Birthday!";
string hash = GetSha1Hash(source);
Console.WriteLine("The SHA1 Hash of " + source + " is: " + hash);
Console.WriteLine("Verifying the hash....");
if (VerifySha1Hash(source, hash))
{
Console.WriteLine("The hashes are the same...");
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/// <summary>
///验证Md5 hash
/// </summary>
/// <param name="input">原字符串</param>
base64
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。
Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。采用Base64编码具有不可读性,需要解码后才能阅读。
MD5主要是用来做数据校验的。拿网上下载软件来说,有很多软件在下载的时候都会提供一个MD5校验码,就是用来校验软件是否被他人修改过。还有就是用户账号系统,用户注册后,数据库存储的不是明文密码,而是MD5码。
测试代码:
演示如何获取一个字符串的MD5 hash以及校验MD5 hash:
using System;
相关文档
最新文档