首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#各种加密算法的加密进程以及怎样循环加密

2011-12-10 
C#各种加密算法的加密过程以及怎样循环加密C#各种加密算法的加密过程,最好举一个简单的例子来说明它以及怎

C#各种加密算法的加密过程以及怎样循环加密
C#各种加密算法的加密过程,最好举一个简单的例子来说明它
以及怎样循环加密,让程序变得更难破解!!

[解决办法]
一般常用的就是MD5加密

C# code
using System.Security.Cryptography;  //  //MD5加密函数  //  public string MD5(String str)  {   MD5 md5=new MD5CryptoServiceProvider();   byte[] data=System.Text.Encoding.Default.GetBytes(str);   byte[] result=md5.ComputeHash(data);   String ret="";   for(int i=0;i<result.Length;i++)    ret+=result[i].ToString("x").PadLeft(2,'0');   return ret;  }  MS的HELP using System;using System.Security.Cryptography;using System.Text;class Example{    // Hash an input string and return the hash as    // a 32 character hexadecimal string.    static string getMd5Hash(string input)    {        // Create a new instance of the MD5CryptoServiceProvider object.        MD5 md5Hasher = MD5.Create();        // Convert the input string to a byte array and compute the hash.        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));        // Create a new Stringbuilder to collect the bytes        // and create a string.        StringBuilder sBuilder = new StringBuilder();        // Loop through each byte of the hashed data         // and format each one as a hexadecimal string.        for (int i = 0; i < data.Length; i++)        {            sBuilder.Append(data[i].ToString("x2"));        }        // Return the hexadecimal string.        return sBuilder.ToString();    }    // Verify a hash against a string.    static bool verifyMd5Hash(string input, string hash)    {        // Hash the input.        string hashOfInput = getMd5Hash(input);        // Create a StringComparer an comare the hashes.        StringComparer comparer = StringComparer.OrdinalIgnoreCase;        if (0 == comparer.Compare(hashOfInput, hash))        {            return true;        }        else        {            return false;        }    }    static void Main()    {        string source = "Hello World!";                string hash = getMd5Hash(source);        Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".");        Console.WriteLine("Verifying the hash...");        if (verifyMd5Hash(source, hash))        {            Console.WriteLine("The hashes are the same.");        }        else        {            Console.WriteLine("The hashes are not same.");        }            }}
[解决办法]
没有必要用循环加密 
用一种不可逆得加密 就很难破解了
C# code
using System;using System.IO;using System.Security.Cryptography;using System.Text;class FileEncrypt {public static Byte[] ConvertStringToByteArray(String s){return (new UnicodeEncoding()).GetBytes(s);}public static void Main(){//创建文件流FileStream fs = new FileStream("EncryptedFile.txt",FileMode.Create,FileAccess.Write);Console.WriteLine("输入一些要存储在加密文件中的文本::");String strinput = Console.ReadLine();Byte[] bytearrayinput=ConvertStringToByteArray(strinput);//具有随机密钥的 DES 实例DESCryptoServiceProvider des = new DESCryptoServiceProvider();//从此实例创建 DES 加密器ICryptoTransform desencrypt = des.CreateEncryptor();//创建使用 des 加密转换文件流的加密流CryptoStream cryptostream = new CryptoStream(fs,desencrypt,CryptoStreamMode.Write);//写出 DES 加密文件cryptostream.Write(bytearrayinput,0,bytearrayinput.Length);cryptostream.Close();//创建文件流以读回加密文件FileStream fsread = new FileStream("EncryptedFile.txt",FileMode.Open,FileAccess.Read);//从此 des 实例创建 DES 解密器ICryptoTransform desdecrypt = des.CreateDecryptor();//创建加密流集合以便对传入的字节进行读取并执行 des 解密转换CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);//输出已解密文件的内容Console.WriteLine( (new StreamReader(cryptostreamDecr, new UnicodeEncoding())).ReadToEnd() );Console.WriteLine ();Console.WriteLine ("按 Enter 键继续...");Console.ReadLine();}} 

热点排行