日期:2014-05-18  浏览次数:21025 次

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 UnicodeEncodin