Csharp/CSharp Tutorial/Security/Encryption

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

PublicKey Cryptography

using System;
using System.Security.Cryptography;
  class Class1
  {
    static void Main(string[] args)
    {
      string dataToSign = "this is a test.";
      byte[] data_Bytes = System.Text.Encoding.ASCII.GetBytes( dataToSign );
      
      SHA1Managed sha1 = new SHA1Managed();
      byte[] hash_Bytes = sha1.ruputeHash( data_Bytes );
    
      DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(); 
      DSASignatureFormatter sigFormatter = new DSASignatureFormatter(dsa);
      sigFormatter.SetHashAlgorithm("SHA1");
      byte[] signedHash = sigFormatter.CreateSignature( hash_Bytes );
      byte[] remote_SignedHash = signedHash;
      byte[] remote_HashedValue = hash_Bytes;
      DSASignatureDeformatter sigDeformatter = new DSASignatureDeformatter( dsa );
      sigDeformatter.SetHashAlgorithm( "SHA1" );
      Console.WriteLine(sigDeformatter.VerifySignature( remote_HashedValue, remote_SignedHash ));
      RSACryptoServiceProvider rsa=  new RSACryptoServiceProvider();
      byte[] dataToEncrypt = System.Text.Encoding.Unicode.GetBytes( "Encrypt THIS!" );
      byte[] encrypted_Bytes;
      byte[] decrypted_Bytes;
      encrypted_Bytes = rsa.Encrypt( dataToEncrypt, false );
      Console.WriteLine("RSA-encrypted data is {0} bytes long.", encrypted_Bytes.Length);
      decrypted_Bytes = rsa.Decrypt( encrypted_Bytes, false );
      Console.WriteLine("RSA-decrypted string is {0}.", System.Text.Encoding.Unicode.GetString( decrypted_Bytes ));
    }
  }

Secret Key Cryptography: RijndaelManaged

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
  class Class1
  {
    static void Main(string[] args)
    {
      byte[] bin = new byte[100]; // 100 byte buffer
      FileStream fsIn = new FileStream("input.txt", FileMode.Open, FileAccess.Read);
      long rdLen = 0;
      long totLen = fsIn.Length;
      int len;
      RijndaelManaged rm = new RijndaelManaged();
      rm.GenerateIV();
      rm.GenerateKey();
      FileStream fsOut_RM = new FileStream("outputRM.txt", FileMode.Create, FileAccess.Write);
      CryptoStream rmStream = new CryptoStream( fsOut_RM,
        rm.CreateEncryptor(), CryptoStreamMode.Write);
      while ( rdLen < totLen )
      {
        len = fsIn.Read(bin, 0, 100);
        System.Diagnostics.Debug.WriteLine("Read " + len.ToString() + " bytes.");
        rmStream.Write(bin, 0, len );
        rdLen += len;
        Console.WriteLine("{0} Bytes Read.", rdLen );
      }
      fsIn.Close();
    }
  }

Symmetric Encryption

using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
    class Program
    {
        static void Main(string[] args)
        {
            RijndaelManaged rmCrypto = new RijndaelManaged();
            byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            string clearMessage = "this is a test";
            FileStream fs = new FileStream("encrypted.dat", FileMode.Create);
            CryptoStream cs = new CryptoStream(fs, rmCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(clearMessage),0, clearMessage.Length);
            cs.Close();
            fs.Close();
            FileStream fs2 = new FileStream("encrypted.dat", FileMode.Open);
            CryptoStream cs2 = new CryptoStream(fs2, rmCrypto.CreateDecryptor(key, IV), CryptoStreamMode.Read);
            byte[] decryptedData = new byte[fs2.Length];
            cs2.Read(decryptedData, 0, (int)fs2.Length);
            cs2.Close();
            fs2.Close();
            Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(decryptedData));
        }
    }