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

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

?? ????? ??

??????

?? ???? ????? ???? ?? ?? Microsoft .NET Framework ?????? ?????? ??? ?? ??? ?? ?????????????? ???? ?? ????? ???? ????? ?? ???? ??? ???? ?? ???? unreadable ?????? ?? ?????????? ???? ?? ??? ?????????????? ???? ?? ????? ?? ???? ??? Then, you can decrypt that text file back to its original format.



??????????

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you must have:
  • Microsoft Windows XP, Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
  • Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005

Use Encryption and Decryption

The System.Security.Cryptographic namespace in the .NET Framework provides a variety of tools to aid in encryption and in decryption. TheCryptoStreamclass is one of the many classes that is provided. TheCryptoStream???? ?? ??? ??? ?? ??????? ?? ??????? ?????? ???? ?? ??? ???? ???? ??????? ?? ????????? ???? ?? ??? ?? ?????????? ???? ?? ??? ??????? ???? ??? ???

???? ????? ?? ?????????? ???? ?? ??? ????? ????? ?? ???? ????:
  1. ????? Visual Studio .NET ?? Visual Studio 2005?
  2. ??? ??? ????? ????????? ??? Visual Basic 2005 ?? Visual Basic .NET ??? ?????? ???? ??????? ?? ???? ??, ?? ??? ??? ???? ?? ???Main()?? ??? 1 ?? ??? ?? ????..
  3. ?????Imports??? ?????? ????????, System.Security ????????, System.Security.Cryptography ????????, System.Text ??? ?????, ?? System.IO ??? ????? ?? ??? You must do this so that you do not have to qualify declarations from these namespaces later in your code. You must use these statements before any other declarations.
    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Runtime.InteropServices
    Imports System.Text
    					
  4. Generate a secret key to encrypt and to decrypt the data. TheDESCryptoServiceProviderclass is based on a symmetric encryption algorithm. The symmetric encryption requires a key and an initialization vector (IV) to encrypt the data. To decrypt the data, you must have the same key and the same IV. You must also use the same encryption algorithm. ?? ?? ?? ????? ??????? ?? ????? ?? ????? ????? ?? ???? ???:
    • ?????? 1:?? ?????????? ?? ??????? ?? ??? ????? ??? ???? ???? ????? ?? IV ?? ??? ???, ?? ??????? ?? ????? ?????
    • ???? 2?? ?? ????????? ??????????????? ???? ?? ?? ?? ??????? ????? ???, ?? ?? ????? ?? ??? ??? IV ???????? ??? ?? ???? ???? ??? ???? ?? ???? ?? ????? ?? IV ?????? ???????? ????????? ??????????????? ???? ?? ?????????? ???? ?? ??? ?? ????? ?? ????????? ???? ?? ??? ????? ??? ?? ?? ????? ?? ???? ????

      For more information about how to generate and to distribute keys, visit the following Microsoft Web site or see the .NET Framework software development kit (SDK) documentation:

      http://msdn.microsoft.com/en-us/library/5e9ft273(VS.71).aspx
  5. Add the following function to generate a new key for a session as noted in Method 2:
       ' 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. Create a method in your class that is namedEncryptFile. TheEncryptFilemethod must have three parameters:
    • sInputFile
    • sOutputFile
    • sKey(This is the secret key that is used to encrypt and to decrypt the file.)
        Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
        End Sub
    					
  7. ?????EncryptFileprocedure, create an inputFileStreamobject and an outputFileStream???????? ??? ?? ?????????? ?? ????? ?? ?????? ??????? ?? ??? ???? ?? ???? ????
            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 ?????? ?? ??? ???? ???

    ??? ?? ??? ????? ?????? ????? ????, ??????? ?????????? ????? ??? ?? encrypts ??????????? ???? ??, ????? ????? ?? ????????? ???? ?? ??? ??? ????? ???? ??? ??? ???? ?? ???? ?? ?????? IV ??? ?? ??? ?? ??????????? ?? ?? ??? ?? ??? ??? ????? ???? ???? ??? ????? ?? ??? IV ?? ?????????? ??????? ??? ?? ??? ?????? ????? ??? ??????????? ?? ?????????? ?? ??? ???? ???? ?????, ??????? ?? ?? ????? ?? ????? ????? ?????? ???? ???? ??????
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    					
  10. ?? ??????? ?????CryptoStream????? ???????? ???? encrypting (??????? ???? ?? ??? ??????????????? ??????? ?? ????? ????CreateEncryptor) ?? ?????? ??????FileStream???????? constructor ?? ???? ??? ?? ??? ??? ???
    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?? ??? 1 ?? ??? ?? ????..
    • CreateDecryptor?? ????? ?? ????? ???? ???? ??CreateEncryptor????? ?? ???CryptoStream???????? ?? ?? ????? ?? ?? ???? ???????? ????? ???? ?? ?????
    • ?? decrypted ??? ?????? ????? ?? ??? ???? ???? ??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()?? ???? ??? ?????????) ?? ????? ?? ??? ????? ?? ???? Decrypted ????? ?? ??????? ???? ?? decrypted ????? ?? ??? ?? ????? ?????

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

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/

???

???? ID: 301070 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005swept kbvs2005applies kbsecurity kbio kbcrypt kbhowtomaster kbmt KB301070 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??: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