????? ?????? ??? ????? ??? ???????? Visual Basic .NET ?? Visual Basic 2005

?????? ????????? ?????? ?????????
???? ???????: 301070 - ??? ???????? ???? ????? ????? ??? ???????.
?????? ??? ????? Microsoft Visual C# .NET ?? ??? ???????? ???? 307010.
????? ???? | ?? ????

?? ??? ??????

??????

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



???????

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

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

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

?????? ??? ???? ??????? ???????:
  1. ?????? ????? Visual Studio .NET ?? Visual Studio 2005.
  2. ????? ????? ???? ???? ???? ?? Visual Basic .NET ?? ?? Visual Basic 2005. ??? ????? ???? ????? ??? ?? ????? Main() ????.
  3. ?????? ??????? ?????? ????????? ??? ?? ????? ????? ?????? ????? ????? System.Security ????? ????? System.Security.Cryptography ????? ????? System.Text ????? ????? System.IO. ??? ????? ??? ???? ?? ???? ????? ????????? ?? ?????? ??????? ??? ???? ??? ?? ????????? ???????? ?????? ??. ??? ??????? ??? ???????? ??? ?? ????????? ??????.
    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Runtime.InteropServices
    Imports System.Text
    					
  4. ????? ????? ??? ????? ?? ????? ????????. ??? DESCryptoServiceProvider ????? ??? ???????? ????? ????????. ????? ????? ?????? ???????? ?? ???? ????? (IV) ?????? ????????. ??? ????? ???????? ? ??? ?? ???? ???? ??? ????? ? IV ???. ??? ???? ????? ??????? ???????? ????? ???. ????? ????? ???????? ???????? ?? ?? ????? ???????:
    • ??????? 1 ????? ?????? ???????? ????? ????. ??? ?????? ???? ?????? ??????? ? IV.
    • ??????? 2 ??? ????? ???? ???? ?? ???? ??????? ???????? ??? ???????? ????? ????? ???? ? IV ????? ?? ??? ???? ?????. ???? ??????? ??????? IV ???? ?? ??????? ?????? ??????? ???????? ??????? ?????? ?????? ? ??? ????? ?????.

      ?????? ??? ???? ?? ????????? ??? ????? ????? ????? ?????? ?????? ???? Microsoft ?????? ??? ????? ?? ???? ????? ?????? ????? (SDK) ????? ?????? .NET Framework:

      http://msdn.microsoft.com/en-us/library/5e9ft273(VS.71).aspx
  5. ????? ???? ??????? ????? ????? ???? ????? ??? ?? ???? ?? "??????? ???????":
       ' Call this function to remove the key from memory after it is used for security.
       Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" _
                         (ByVal Destination As String, ByVal Length As Integer)
       
       ' Function to generate a key.
       Function GenerateKey() As String
          ' Create an instance of Symmetric Algorithm. The key and the IV are generated automatically.
          Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
    
          ' Use the automatically generated key for encryption. 
          Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
       End Function 
  6. ????? ????? ?? ????? ??????? ?????? EncryptFile. ??? ?? ????? ????? EncryptFile ???????? ??????:
    • sInputFile
    • sOutputFile
    • sKey (??? ?? ??????? ????? ???? ??? ???????? ?????? ??? ????? ?????.)
        Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
        End Sub
    					
  7. ?? ??????? EncryptFile ????? ???? ???? FileStream ????? ???????? ???? ???? FileStream. ???? ??????? ?? ??? ???????? ?????? ??????? ??? ??????? ?????.
            Dim fsInput As New FileStream(sInputFilename, _
                                        FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sOutputFilename, _
                                        FileMode.Create, FileAccess.Write)
    					
  8. ?? ?????? ???? ??? DESCryptoServiceProvider. ???? ??? ??????? ?????? ?????? ?? ??????? ?????? ???????? ??? ???????. ??? ??? ??????? ????? ????? ???? ????? ??? ??? ???? ??????? ?????? RSA ?? ??? ????? ???????.
    Dim DES As New DESCryptoServiceProvider()
    					
  9. ??? ????? ???? ????? ??????? ?? ??????? ????? ?? ???? ?? ??????. ???? ????? ????? System.Text ???? ???? GetBytes(). ???? ?? ?????? ??????? ?????? GetBytes() ???? ????? ??? ?? ????? ???? ?? ??????. ??? ??????? ?????? ??????? ??? ????? ???????. ??? ???? ??????? ???? ????? ????? ???????? "(DES) ????? 64 ?? ????? 8 ???? ?? 8 ????.

    ??? ?? ??? ?????? ????? ?????? ???? ?????? ?????? ?????. ??? ?????? ????? ????? ???? ?? ???? ??? ????? ??? ????? ?????. ???? ??? ??? ????? ????? IV. ??? ??????? ??? ?????? ???? ?? ???????. ??? ???????, ??? ????? IV ???? ?????? ??? ?? ???? ??????. ???? ??? ?? ???? ????? ????? ?? ??? ??????? ??? ? ??? ?? ?? ???? ????? ?????? ?? ??? ?????.
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    					
  10. ????? ???? ???? CryptoStream. ??????? ???? ????? ??????? ??? ?????? ??? ???? ????? (CreateEncryptor) ? ??????? ?????? ?????? ???? FileStream ???? ?? ???????.
    Dim cryptostream As New CryptoStream(fsEncrypted, _
                                         desencrypt, _
                                         CryptoStreamMode.Write)
    					
  11. ????? ?? ??? ??????? ??? ?? ????? ??? ??? ???????. ?????? ?? ???? ?????? CryptoStream ??? ????? ????? ???????? ??????? ???? ??? ???????.
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    					
?? ????? ??? "? ???? ??????? ???????:
  1. ????? ????? ???? DecryptFile. ???? ????? ?? ??????? ????? ???????. ??? ???? ??? DecryptFile ???????? ?????? ????? ?? ??????? EncryptFile.
    • ??? ??????? CreateDecryptor ????? ?? CreateEncryptor ?????? ???? CryptoStream ???? ???? ??? ???? ??????? ??????.
    • ??? ????? ???? ???? ?? ?? ?????? ??? ????? ?????? CryptoStream ?? ???? ???? ????? ?? ??? ??????.
    Sub DecryptFile(ByVal sInputFilename As String, _
        ByVal sOutputFilename As String, _
        ByVal sKey As String)
    
        Dim DES As New DESCryptoServiceProvider()
        'A 64-bit key and an IV are 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.
        Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
        'Create a DES Decryptor from your DES instance.
        Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
        'Create a crypto stream set to read and to do a DES decryption transform on incoming bytes.
        Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
        'Print out the contents of the decrypted file.
        Dim fsDecrypted As New StreamWriter(sOutputFilename)
        fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
        fsDecrypted.Flush()
        fsDecrypted.Close()
    End Sub
    					
  2. ??? ?????? ??????? ??? ????? Main() ??????? ?? EncryptFile ? DecryptFile.
        Public Sub Main()      'Must be 64 bits, 8 bytes.
          Dim sSecretKey As String
    
          ' Get the key for the file to encrypt.
          ' You can distribute this key to the user who will decrypt the file.
          sSecretKey = GenerateKey()
    
          ' For additional security, pin the key.
          Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
    
    
          ' Encrypt the file.        
          EncryptFile("%USERPROFILE%\MyData.txt", _
                          "%USERPROFILE%\Encrypted.txt", _
                          sSecretKey)
    
          ' Decrypt the file.
          DecryptFile("%USERPROFILE%\Encrypted.txt", _
                      "%USERPROFILE%\Decrypted.txt", _
                      sSecretKey)
    
          ' Remove the key from memory.
          ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
          gch.Free()
       End Sub
  3. ??? ?????. ????? ??????? ????? ??. ???? ?? ?? ?????? ???? ??? ??????? ??? ??? ??????? ???? ??? ????? ??? ???? ?? ??????.

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

?????? ??? ???????? ???????? ???????? ??? ??? (.txt) ?????? ?? ?? ????? ??? ???? ???? ????? ?? ?? ???????. ???? ?? ????? ????? ??? ??? ???? (??? ?? ???? ?? ??????? Main() Sub ?? ??? ???????) ????? ?? ????? ??????. ??? ??? ?? ?? ?????? ??????? ?? ????? ????? ??????.

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

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text


Module Module1

   ' Call this function to remove the key from memory after it is used for security.
   <DllImport("kernel32.dll")> _
   Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
   End Sub

   ' Function to generate a 64-bit key.
   Function GenerateKey() As String
      ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
      Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()

      ' Use the automatically generated key for encryption. 
      Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)

   End Function

   Sub EncryptFile(ByVal sInputFilename As String, _
                  ByVal sOutputFilename As String, _
                  ByVal sKey As String)

      Dim fsInput As New FileStream(sInputFilename, _
                                  FileMode.Open, FileAccess.Read)
      Dim fsEncrypted As New FileStream(sOutputFilename, _
                                  FileMode.Create, FileAccess.Write)

      Dim DES As New DESCryptoServiceProvider()

      'Set secret key for DES algorithm.
      'A 64-bit key and an IV are required for this provider.
      DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

      'Create the DES encryptor from this instance.
      Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
      'Create the crypto stream that transforms the file stream by using DES encryption.
      Dim cryptostream As New CryptoStream(fsEncrypted, _
                                          desencrypt, _
                                          CryptoStreamMode.Write)

      'Read the file text to the byte array.
      Dim bytearrayinput(fsInput.Length - 1) As Byte
      fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
      'Write out the DES encrypted file.
      cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
      cryptostream.Close()
   End Sub

   Sub DecryptFile(ByVal sInputFilename As String, _
       ByVal sOutputFilename As String, _
       ByVal sKey As String)

      Dim DES As New DESCryptoServiceProvider()
      'A 64-bit key and an IV are required for this provider.
      'Set the secret key for the DES algorithm.
      DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
      'Set the initialization vector.
      DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

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

   Public Sub Main()
      'Must be 64 bits, 8 bytes.
      Dim sSecretKey As String

      ' Get the key for the file to encrypt.
      ' You can distribute this key to the user who will decrypt the file.
      sSecretKey = GenerateKey()

      ' For additional security, pin the key.
      Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)


      ' Encrypt the file.        
      EncryptFile("%USERPROFILE%\MyData.txt", _
                      "%USERPROFILE%\Encrypted.txt", _
                      sSecretKey)

      ' Decrypt the file.
      DecryptFile("%USERPROFILE%\Encrypted.txt", _
                  "%USERPROFILE%\Decrypted.txt", _
                  sSecretKey)

      ' Remove the key from memory. 
      ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
      gch.Free()
   End Sub

End Module

?????

?????? ??? ???? ?? ????????? ??? ????? ??????? ????? ??????? .NET ???? ??????? ?????? ????? Microsoft ??????? ??? ?????:

http://msdn.microsoft.com/net

http://code.msdn.microsoft.com/

???????

???? ???????: 301070 - ????? ??? ??????: 15/?? ??????/1427 - ??????: 5.6
????? ???
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
????? ??????: 
kbmt kbvs2005swept kbvs2005applies kbsecurity kbio kbcrypt kbhowtomaster KB301070 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????301070

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

 

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