程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#裡的一些加密解密標准函數示例——DES,SHA1,RSA

C#裡的一些加密解密標准函數示例——DES,SHA1,RSA

編輯:關於C#

最近收到了很多朋友的來信說希望提供DES的C#代碼,但是我個人認為,.NET 提供了很多標准函數,沒有必要自己寫,所以我也只發布了C++的代碼,如果大家一定要熟悉加密過程的話,也可以自己動手實現整個過程,這個可以參考我博客裡的DES 算法介紹,和yxyDES2 Class的代碼,代碼注釋相當的清楚。

.NET 提供了很多標准加密、解密函數,我簡要介紹一下DES,SHA1,RSA的標准函數的使用。如果你想做一個網絡安全模塊,只需將三種算法結合起來設計一個模型,我相信可以實現很多復雜的功能。

示例本身並不復雜,我也不做過多解釋,我也學Linus Torvalds一樣吼一句:"Read the f**ing code”,哈哈,開個玩笑,我相信大家肯定能看懂。

注:以下示例需引用命名空間 : using System.Security.Cryptography;

一. DES 加密、解密

我相信一下注釋相當清楚了,加上我博客裡關於DES的文章確實不少,所以DES不做任何解釋,怎麼調用就更不用解釋了吧,呵呵:

//默認密鑰向量
private byte[] Keys = { 0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12 };
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密鑰,要求為8位</param>
/// <returns>加密成功返回加密後的字符串,失敗返回源串</returns>
public string EncryptDES(string encryptString, string encryptKey)
{
 try
 {
  byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  byte[] rgbIV = Keys;
  byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  MemoryStream mStream = new MemoryStream();
  CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  cStream.Write(inputByteArray, 0, inputByteArray.Length);
  cStream.FlushFinalBlock();
  return Convert.ToBase64String(mStream.ToArray());
 }
 catch
 {
  return encryptString;
 }
}

/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密鑰,要求為8位,和加密密鑰相同</param>
/// <returns>解密成功返回解密後的字符串,失敗返源串</returns>
public string DecryptDES(string decryptString, string decryptKey)
{
 try
 {
  byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
  byte[] rgbIV = Keys;
  byte[] inputByteArray = Convert.FromBase64String(decryptString);
  DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  MemoryStream mStream = new MemoryStream();
  CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  cStream.Write(inputByteArray, 0, inputByteArray.Length);
  cStream.FlushFinalBlock();
  return Encoding.UTF8.GetString(mStream.ToArray());
 }
 catch
 {
  return decryptString;
 }
}

二. SHA1 加密 (HASH算法沒有解密)

安全哈希算法(Secure Hash Algorithm)主要適用於數字簽名標准(Digital Signature Standard DSS)裡面定義的數字簽名算法(Digital Signature Algorithm DSA)。對於長度小於2^64位的消息,SHA1會產生一個160位的消息摘要。當接收到消息的時候,這個消息摘要可以用來驗證數據的完整性。在傳輸的過程中,數據很可能會發生變化,那麼這時候就會產生不同的消息摘要。

SHA1有如下特性:不可以從消息摘要中復原信息;兩個不同的消息不會產生同樣的消息摘要。

代碼如下:

/// <summary>
/// use sha1 to encrypt string
/// </summary>
public string SHA1_Encrypt(string Source_String)
{
 byte[] StrRes = Encoding.Default.GetBytes(Source_String);
 HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
 StrRes = iSHA.ComputeHash(StrRes);
 StringBuilder EnText = new StringBuilder();
 foreach (byte iByte in StrRes)
 {
  EnText.AppendFormat("{0:x2}", iByte);
 }
 return EnText.ToString();
}

三.RSA 加密、解密 (本例來自 MSDN)

RSA加密算法是一種非對稱加密算法。在公鑰加密標准和電子商業中RSA被廣泛使用。RSA是1977年由羅納德·李維斯特(Ron Rivest)、阿迪·薩莫爾(Adi Shamir)和倫納德·阿德曼(Leonard Adleman)一起提出的。當時他們三人都在麻省理工學院工作。RSA就是他們三人姓氏開頭字母拼在一起組成的。

RSA算法的可靠性基於分解極大的整數是很困難的。假如有人找到一種很快的分解因子的算法的話,那麼用RSA加密的信息的可靠性就肯定會極度下降。但找到這樣的算法的可能性是非常小的。今天只有短的RSA鑰匙才可能被強力方式解破。到2008年為止,世界上還沒有任何可靠的攻擊RSA算法的方式。只要其鑰匙的長度足夠長,用RSA加密的信息實際上是不能被解破的。

具體算法過程請參考http://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF%86%E6%BC%94%E7%AE%97%E6%B3%95

代碼示例如下(來自MSDN):

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Microsoft.Samples.Security.PublicKey
{
  class App
  {
    // Main entry point
    static void Main(string[] args)
    {
      // Instantiate 3 People for example. See the Person class below
      Person alice = new Person("Alice");
      Person bob = new Person("Bob");
      Person steve = new Person("Steve");

      // Messages that will exchanged. See CipherMessage class below
      CipherMessage aliceMessage;
      CipherMessage bobMessage;
      CipherMessage steveMessage;

      // Example of encrypting/decrypting your own message
      Console.WriteLine("Encrypting/Decrypting Your Own Message");
      Console.WriteLine("-----------------------------------------");

      // Alice encrypts a message using her own public key
      aliceMessage = alice.EncryptMessage("Alice wrote this message");
      // then using her private key can decrypt the message
      alice.DecryptMessage(aliceMessage);
      // Example of Exchanging Keys and Messages
      Console.WriteLine();
      Console.WriteLine("Exchanging Keys and Messages");
      Console.WriteLine("-----------------------------------------");

      // Alice Sends a copy of her public key to Bob and Steve
      bob.GetPublicKey(alice);
      steve.GetPublicKey(alice);

      // Bob and Steve both encrypt messages to send to Alice
      bobMessage = bob.EncryptMessage("Hi Alice! - Bob.");
      steveMessage = steve.EncryptMessage("How are you? - Steve");

      // Alice can decrypt and read both messages
      alice.DecryptMessage(bobMessage);
      alice.DecryptMessage(steveMessage);

      Console.WriteLine();
      Console.WriteLine("Private Key required to read the messages");
      Console.WriteLine("-----------------------------------------");

      // Steve cannot read the message that Bob encrypted
      steve.DecryptMessage(bobMessage);
      // Not even Bob can use the Message he encrypted for Alice.
      // The RSA private key is required to decrypt the RS2 key used
      // in the decryption.
      bob.DecryptMessage(bobMessage);

    } // method Main
  } // class App

  class CipherMessage
  {
    public byte[] cipherBytes;  // RC2 encrypted message text
    public byte[] rc2Key;       // RSA encrypted rc2 key
    public byte[] rc2IV;        // RC2 initialization vector
  }

  class Person
  {
    private RSACryptoServiceProvider rsa;
    private RC2CryptoServiceProvider rc2;
    private string name;

    // Maximum key size for the RC2 algorithm
    const int keySize = 128;

    // Person constructor
    public Person(string p_Name)
    {
      rsa = new RSACryptoServiceProvider();
      rc2 = new RC2CryptoServiceProvider();
      rc2.KeySize = keySize;
      name = p_Name;
    }

    // Used to send the rsa public key parameters
    public RSAParameters SendPublicKey()
    {
      RSAParameters result = new RSAParameters();
      try
      {
        result = rsa.ExportParameters(false);
      }
      catch (CryptographicException e)
      {
        Console.WriteLine(e.Message);
      }
      return result;
    }

    // Used to import the rsa public key parameters
    public void GetPublicKey(Person receiver)
    {
      try
      {
        rsa.ImportParameters(receiver.SendPublicKey());
      }
      catch (CryptographicException e)
      {
        Console.WriteLine(e.Message);
      }
    }

    public CipherMessage EncryptMessage(string text)
    {
      // Convert string to a byte array
      CipherMessage message = new CipherMessage();
      byte[] plainBytes = Encoding.Unicode.GetBytes(text.ToCharArray());

      // A new key and iv are generated for every message
      rc2.GenerateKey();
      rc2.GenerateIV();

      // The rc2 initialization doesnt need to be encrypted, but will
      // be used in conjunction with the key to decrypt the message.
      message.rc2IV = rc2.IV;
      try
      {
        // Encrypt the RC2 key using RSA encryption
        message.rc2Key = rsa.Encrypt(rc2.Key, false);
      }
      catch (CryptographicException e)
      {
        // The High Encryption Pack is required to run this  sample
        // because we are using a 128-bit key. See the readme for
        // additional information.
        Console.WriteLine("Encryption Failed. Ensure that the" +
          " High Encryption Pack is installed.");
        Console.WriteLine("Error Message: " + e.Message);
        Environment.Exit(0);
      }
      // Encrypt the Text Message using RC2 (Symmetric algorithm)
      ICryptoTransform sse = rc2.CreateEncryptor();
      MemoryStream ms = new MemoryStream();
      CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
      try
      {
          cs.Write(plainBytes, 0, plainBytes.Length);
          cs.FlushFinalBlock();
          message.cipherBytes = ms.ToArray();
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
      finally
      {
        ms.Close();
        cs.Close();
      }
      return message;
    } // method EncryptMessage


    public void DecryptMessage(CipherMessage message)
    {
      // Get the RC2 Key and Initialization Vector
      rc2.IV = message.rc2IV;
      try
      {
        // Try decrypting the rc2 key
        rc2.Key = rsa.Decrypt(message.rc2Key, false);
      }
      catch (CryptographicException e)
      {
        Console.WriteLine("Decryption Failed: " + e.Message);
        return;
      }
      
      ICryptoTransform ssd = rc2.CreateDecryptor();
      // Put the encrypted message in a memorystream
      MemoryStream ms = new MemoryStream(message.cipherBytes);
      // the CryptoStream will read cipher text from the MemoryStream
      CryptoStream cs = new CryptoStream(ms, ssd, CryptoStreamMode.Read);
      byte[] initialText = new Byte[message.cipherBytes.Length];

      try
      {
          // Decrypt the message and store in byte array
          cs.Read(initialText, 0, initialText.Length);
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
      finally
      {
        ms.Close();
        cs.Close();
      }

      // Display the message received
      Console.WriteLine(name + " received the following message:");
      Console.WriteLine("  " + Encoding.Unicode.GetString(initialText));
    } // method DecryptMessage
  } // class Person
} // namespace PublicKey

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved