????? ????? ??? ????? ??? ???????? Visual C#

?????? ????????? ?????? ?????????
???? ???????: 307010 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual Basic.NET ?? ??? ???????? ???? 301070.
???? ??? ??????? ??? Microsoft.NET ??????? ????? ?????? ???? ?????? ???????:
  • System.IO
  • System.Security
  • System.Security.Cryptography
?????? ?? ????? ??? ??????? ??? Microsoft.NET Framework 2.0.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ????? ??????? ???? ??????? ???? ??? ??????? ?? ??? Microsoft.NET Framework ?????? ??? ?? ??? ???? ??? ???? ???????? ??? ?? ??? ????? ??? ????? ????? ??? ???? ??? ?????? ?????.

???????

???? ??????? ??????? ??????? ?????? ???? ???????? ?????? ???????? ??????? ???? ??????? ???? ??? ?? ???? ????:
  • Microsoft Windows 2000 Professional? Windows 2000 Server ????? Windows 2000 Server ?? Windows NT 4.0 Server ?? ???? ??????? Microsoft Windows XP Professional
  • Microsoft Visual Studio 2005 ?? Microsoft Visual Studio .NET

??????? ??? ???????

???? ????? ????? System.Security.Cryptographic ?? Microsoft.NET Framework ?????? ?????? ?? ????? ???????? ?? ??????? ??? ???????. ????? CryptoStream ?? ????? ?? ?????? ?? ?????? ???? ??? ???????. ?? ????? ????? CryptoStream ????? ?? ?? ????? ????? ??? ???? ?????? ??? ???.

????? ???

?????? ???? ???? ??????? ???????:
  1. ??? ????? Visual Studio 2005 ?? Visual Studio.NET.
  2. ???? ??? Visual C# ???????????? ?? ???? ??? ????? ???? ????????? ?????. ???? visual C#.NET ??? ????? ? ?? ????? main () ????.
  3. ??????? ????? using (??? ?? ???? ?? ????? ????????? ???????? ???? ????) ??? ?????? ??????? ???????:
    • ??????
    • System.Security
    • System.Security.Cryptography
    • System.Text
    • System.IO
    ??? ??? ??? ???? ??? ????? ????????? ?? ??? ?????? ??????? ?????? ?? ????????? ???????? ?????? ??. ??? ??????? ??? ???????? ??? ?? ???? ???? ?????????.
    using System;
    using System.IO;
    using System.Security;
    using System.Security.Cryptography;
    using System.Runtime.InteropServices;
    using System.Text;
    					
  4. ????? ????? ??? ?????? ??? ????? ????????. ???????????????????????? ????? ??? ???????? ????? ??????. ?????? ????? ????? ????? ????? ????? (IV) ?????? ??????. ??? ????? ????????? ??? ?? ???? ???? ??? ????? ? IV ???. ??? ???? ???? ?????? ??? ???????? ???????. ????? ????? ???????? ???????? ?? ?? ????? ???????:
    • ??????? ?????? ????? ?????? ???????? ????? ????. ??? ?????? ???? ?????? ? ??????? ? IV.
    • ??????? ??????? ????? ???? ?????? ???? ???? ?? ???????? ??????? ??? ???????? ????? ????? ????? ? IV ????? ?????. ??????? ??????? ? IV ???? ?? ??????? ?????? ???? ??????? ??????? ????????? ??? ????? ??? ????? ?????.

      ????? ?? ????????? ??? ????? ????? ?????? ????????? ???? Microsoft.NET Framework SDK ?????? ?? ???? ??? ???? ????? Microsoft (MSDN) ?????? ??????:
      ????? ?????? ??????? ??? ???????
  5. ????? ???? ??????? ????? ????? ???? ???? ????? (??? ?? ???? ?? "??????? ???????" ?? ?????? 4):
    //  Call this function to remove the key from memory after use for security.
    [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
    public static extern bool ZeroMemory(ref string Destination, int Length);
    		
    // Function to Generate a 64 bits Key.
    static string GenerateKey() 
    {
    	// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
    	DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
    
    	// Use the Automatically generated key for Encryption. 
    	return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
    }
  6. ????? ????? ?? ????? ?????? ?? ???? EncryptFile. ??? ?? ???? ??? EncryptFile ???????? ?????? ???????:
    • ??????????????
    • ??????????????
    • ??? (??????? ????? ???? ??? ???????? ?????? ??? ????? ???.)
    static void EncryptFile(string sInputFilename,
    		string sOutputFilename,
    		string sKey)
    					
  7. ?? ??????? EncryptFile ? ????? ???? FileStream ????? ?????? ???? FileStream . ???? ??????? ?? ??? ???????? ?????? ????? ?????.
    FileStream fsInput = new FileStream(sInputFilename, 
    				FileMode.Open, 
    				FileAccess.Read);
    
    FileStream fsEncrypted = new FileStream(sOutputFilename, 
    				FileMode.Create, 
    				FileAccess.Write);
    					
  8. ????? ???? ?? ????? ???????????????????????? . ???? ??? ??????? ?????? ???????? ????? ?? ??????? ???? ??? ???????? ??? ???????. ??? ??? ??????? ????? ????? ???? ????? ??? ??? ???? ??????? RSAsecutiry ?? ??? ??????? ?????.
    DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    					
  9. ??? ????? ???? ??????? ????? ?? ??????? ????? ????? ????. ???? ????? ????? System.Text ???? ???? GetBytes(). ???? ?? ?????? ???????? ?????? GetBytes() ???? ????? ?????? ???? ?? ??????. ????? ????? ??? ??????? ??? ????? ???????. ??? ???? ??????? ????? ????? ???????? (DES) ???? ????? 64-?? ???? ????? 8 ???? ?? ??? 8 ????.

    ??? ?? ??? ?????? ????? ?????? ???????? ???? ?????? ????. ???? ???? ?????? ????? ?????? ???? ?? ???? ??? ????? ?? ????? ?????. ???? ??? ??? ???? ????? ???? ??????? (IV). ??? ??????? ??? ?????? ???? ?? ???????. ??? ???????? ?????? ????? ??? ?? ??? ?????? ?????? ???? ??????. ???? ??? ?? ???? ????? ???? ??????? ??? ???????? ???? ??? ?? ?? ???? ???????? ????? ??? ?????.
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    					
  10. ????? ???? ???? CryptoStream ???????? ???? ??????? ?????? ??? ????? ???? (?????????????) ???????? ?????? ?????? FileStream ???? ?? ?????? ?????????.
    ICryptoTransform desencrypt = DES.CreateEncryptor();
    CryptoStream cryptostream = new CryptoStream(fsEncrypted, 
    					desencrypt, 
    					CryptoStreamMode.Write);
    					
  11. ????? ?? ??? ??????? ??? ?? ????? ??????? ???. ????? ?? ???? ?????? CryptoStream ??? ????? ????? ???????? ??????? ???? ??? ?????.
    byte[] bytearrayinput = new byte[fsInput.Length - 1];
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
    					

?? ????? ???

??? ????? ???? ???? ??????? ???????:
  1. ????? ?????? ??? ?? ?? ??????????????????. ???? ????? ?? ??????? ????? ???????? ??? ???? ??????? DecryptFile ????? ??? ????? ?? ?????????? ???????? ?? ??????? EncryptFile .
    • ??? ??????? ?????????????? ????? ?? ????????????? ?????? ?????? CryptoStream ? ???? ???? ??? ???? ??????? ??????.
    • ??? ????? ???? ???? ?? ?? ?????? ??? ?????? ?????? ?????? CryptoStream ????? ?????? ????? ?? ?????? ???.
    static void DecryptFile(string sInputFilename, 
    	                string sOutputFilename,
    	                string sKey)
    {
    	DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
    	//A 64 bit key and IV is required for this provider.
    	//Set secret key For DES algorithm.
    	DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    	//Set initialization vector.
    	DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    
    	//Create a file stream to read the encrypted file back.
    	FileStream fsread = new FileStream(sInputFilename, 
    		                           FileMode.Open, 
    		                           FileAccess.Read);
    	//Create a DES decryptor from the DES instance.
    	ICryptoTransform desdecrypt = DES.CreateDecryptor();
    	//Create crypto stream set to read and do a 
    	//DES decryption transform on incoming bytes.
    	CryptoStream cryptostreamDecr = new CryptoStream(fsread, 
    		                                         desdecrypt,
    		                                         CryptoStreamMode.Read);
    	//Print the contents of the decrypted file.
    	StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
    	fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
    	fsDecrypted.Flush();
    	fsDecrypted.Close();
    }
    					
  2. ??? ?????? ??????? ??? ??????? main () ??????? ??? ?? EncryptFile ? ???????????:
    static void Main()
    {
          // Must be 64 bits, 8 bytes.
          // Distribute this key to the user who will decrypt this file.
          string sSecretKey;
             
          // Get the key for the file to encrypt.
          sSecretKey = GenerateKey();
    
          // For additional security pin the key.
          GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
             
          // Encrypt the file.        
          EncryptFile(@"C:\MyData.txt", 
             @"C:\Encrypted.txt", 
             sSecretKey);
    
          // Decrypt the file.
          DecryptFile(@"C:\Encrypted.txt", 
             @"C:\Decrypted.txt", 
             sSecretKey);
    
          // Remove the key from memory. 
          ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
          gch.Free();
    }
  3. ??? ?????. ?? ?????? ??????? ????? ??. ???? ?? ?? ?????? ???? ??? ???????? ??? ???? ??? ??? ??????? ??? ????? ???.

?????? ???????

?????? ??? ???????? ???????? ???????? ??? ??? (.txt) ?????? ?? ?? ????????? ???????? ????? ??? ????? ????? ???? ????. ???? ?? ?? ????? ????? ??? ??? ???? (??? ?? ???? ?? ??????? main () ?? ??? ???????) ????? ?? ????? ??????. ??? ????? ???? ?? ?? ?????? ??? ?? ????? ?????? ??????.

????? ??? ????????? ????????

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

namespace CSEncryptDecrypt
{
   class Class1
   {
      //  Call this function to remove the key from memory after use for security
      [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
      public static extern bool ZeroMemory(IntPtr Destination, int Length);
		
      // Function to Generate a 64 bits Key.
      static string GenerateKey() 
      {
         // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
         DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();

         // Use the Automatically generated key for Encryption. 
         return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
      }

      static void EncryptFile(string sInputFilename,
         string sOutputFilename, 
         string sKey) 
      {
         FileStream fsInput = new FileStream(sInputFilename, 
            FileMode.Open, 
            FileAccess.Read);

         FileStream fsEncrypted = new FileStream(sOutputFilename, 
            FileMode.Create, 
            FileAccess.Write);
         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
         ICryptoTransform desencrypt = DES.CreateEncryptor();
         CryptoStream cryptostream = new CryptoStream(fsEncrypted, 
            desencrypt, 
            CryptoStreamMode.Write); 

         byte[] bytearrayinput = new byte[fsInput.Length];
         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
         cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
         cryptostream.Close();
         fsInput.Close();
         fsEncrypted.Close();
      }

      static void DecryptFile(string sInputFilename, 
         string sOutputFilename,
         string sKey)
      {
         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
         //A 64 bit key and IV is required for this provider.
         //Set secret key For DES algorithm.
         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
         //Set initialization vector.
         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

         //Create a file stream to read the encrypted file back.
         FileStream fsread = new FileStream(sInputFilename, 
            FileMode.Open, 
            FileAccess.Read);
         //Create a DES decryptor from the DES instance.
         ICryptoTransform desdecrypt = DES.CreateDecryptor();
         //Create crypto stream set to read and do a 
         //DES decryption transform on incoming bytes.
         CryptoStream cryptostreamDecr = new CryptoStream(fsread, 
            desdecrypt,
            CryptoStreamMode.Read);
         //Print the contents of the decrypted file.
         StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
         fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
         fsDecrypted.Flush();
         fsDecrypted.Close();
      } 

      static void Main()
      {
         // Must be 64 bits, 8 bytes.
         // Distribute this key to the user who will decrypt this file.
         string sSecretKey;
         
         // Get the Key for the file to Encrypt.
         sSecretKey = GenerateKey();

         // For additional security Pin the key.
         GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );
         
         // Encrypt the file.        
         EncryptFile(@"C:\MyData.txt", 
            @"C:\Encrypted.txt", 
            sSecretKey);

         // Decrypt the file.
         DecryptFile(@"C:\Encrypted.txt", 
            @"C:\Decrypted.txt", 
            sSecretKey);

         // Remove the Key from memory. 
         ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
         gch.Free();
      }
   }
}

?????

????? ?? ????????? ??? ???????? ???????? ????? ??????? ??.NET? ???? ????? ??? MSDN ??????:
????? ????? System.Security.Cryptography
???? ???????? ????? ? Microsoft.NET Framework

???????

???? ???????: 307010 - ????? ??? ??????: 11/????? ???????/1434 - ??????: 4.0
????? ???
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
????? ??????: 
kbsecurity kbio kbcrypt kbhowtomaster kbmt KB307010 KbMtar
????? ????
???: ??? ????? ??? ??????? ???????? ????? ?????????? ??????? ?????? ??? ??????? ?? ??? ??????? ?? ??????? ??? ??? ?? ???? ????????? ???? ??????? ???????(CTF) ?? ?? ???? ????? ????. ???? ?? ???? ?????????? ??? ???????? ???????? ???? ??? ?? ???????? ??????? ?? ??? ??????? ???????? ?? ??? ??????? ??????? ??? ?????? ?? ?????? ??? ???? ???????? ???????? ?? ????? ??????? ????? ????? ??????. ??? ?? ???????? ???????? ?? ????? ??? ????? ?? ???????? ?? ???? ????? ?? ?????. ?????? ??? ???? ?????????? ???? ?????? ?? ??? ????? ?? ??? ??? ?? ??????? ?? ??? ????? ?? ???? ????? ????? ?? ????? ??????? ???? ?? ???????? ?? ??? ???????.
???? ??? ????? ??????? ?????? ??????????307010

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com