บทความนี้อธิบายวิธีการใช้ระดับชั้นการเข้ารหัสที่จัดให้ โดย Framework Microsoft .NET คุณสามารถใช้ระดับชั้นการเข้ารหัสการเข้ารหัสลับแฟ้มข้อความไปยังสถานะไม่สามารถอ่าน แล้ว คุณสามารถที่ถอดรหัสลับแฟ้มข้อความนั้นกลับไปเป็นรูปแบบเดิม
ความต้องการ
รายการต่อไปนี้แสดงฮาร์ดแวร์ที่แนะนำ ซอฟต์แวร์ โครงสร้างพื้นฐานของเครือข่าย และ service pack ที่คุณต้องมี:
Microsoft Windows XP, Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Server การขั้นสูง หรือเซิร์ฟเวอร์ของ Microsoft Windows NT 4.0
Microsoft Visual Studio .NET หรือ Microsoft Visual Studio 2005
' 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
Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
สร้างอินสแตนซ์ของการCryptoStreamคลาสที่ ใช้ตัวให้บริการการเข้ารหัสลับเพื่อขอรับการ(encrypting ของวัตถุCreateEncryptor) และผลผลิตที่มีอยู่FileStreamobject as a part of the constructor.
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)
Read in the input file, and then write out to the output
file. Pass through theCryptoStreamobject where the file is encrypted by using the key that you
provided.
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
To decrypt a file, follow these steps:
Create a method that is namedDecryptFile. The decryption process is similar to the encryption process.
However,DecryptFilehas two key differences from theEncryptFileขั้นตอน
CreateDecryptoris used instead ofCreateEncryptorเมื่อต้องการสร้างการCryptoStreamobject that specifies how the object can be used.
When the decrypted text is written to the destination
file, theCryptoStreamobject is now the source instead of the destination
stream.
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
Add the following lines to theMain()procedure to call bothEncryptFileและ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
บันทึกแฟ้ม Run your application. Make sure that the
path that is used for the input file name points to an existing and a not
important file.
Verify That the Encryption and the Decryption Work
Test this code with a text (.txt) file to confirm that the file
is correctly encrypted and decrypted. Make sure that you decrypt the file to a
new file (as in theSub Main()procedure in this article) instead of to the original file.
Examine the decrypted file and compare the decrypted file to the
original.
A Complete Code List
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
ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ