Top Ad unit 728 × 90

كيفية تشفير وفك تشفير ملف باستخدام. NET Visual Basic أو Visual Basic 2005

كيفية تشفير وفك تشفير ملف باستخدام. NET Visual Basic أو Visual Basic 2005


هام: تمت ترجمة هذا المقال باستخدام برنامج ترجمة آلية لشركة مايكروسوفت بدلاً من الاستعانة بمترجم بشري. تقدم شركة مايكروسوفت كلاً من المقالات المترجمة بواسطة المترجمين البشر والمقالات المترجمة آليًا وبالتالي ستتمكن من الوصول إلى كل المقالات الموجودة في قاعدة المعرفة الخاصة بنا وباللغة الخاصة بك. بالرغم من ذلك، فإن المقالة المترجمة آليًا لا تكون دقيقة دائمًا وقد تحتوي على أخطاء إملائية أو لغوية أو نحوية، مثل تلك الأخطاء الصادرة عن متحدث أجنبي عندما يتحدث بلغتك. لا تتحمل شركة مايكروسوفت مسئولية عدم الدقة أو الأخطاء أو الضرر الناتج عن أية أخطاء في ترجمة المحتوى أو استخدامه من قبل عملائنا. تعمل شركة مايكروسوفت باستمرار على ترقية برنامج الترجمة الآلية

اضغط هنا لرابط المقالة باللغة الانجليزية301070
للحصول على إصدار Microsoft Visual C# .NET من هذه المقالة، راجع 307010.
يشير هذه المقالة التالية Microsoft .NET Framework مكتبة الفئات مساحات الأسماء:
  • System.IO
  • System.Security
  • System.Security.Cryptography

في هذه المهمة

الموجز
توضح هذه المقالة كيفية استخدام فئات التشفير التي يتم توفيرها من قبل Microsoft net Framework. يمكنك استخدام فئات التشفير لتشفير ملف نصي إلى حالة غير قابل لقراءة. ثم، يمكنك فك تشفير هذا الملف النصي إعادتها إلى تنسيقها الأصلي.

عودة إلى الأعلى

متطلبات

توضح القائمة التالية الأجهزة الموصى بها برامج البنية الأساسية للشبكة وحزم الخدمات التي يجب أن يكون لديك:
  • نظام التشغيل Microsoft Windows XP، Microsoft Windows Server 2003 أو نظام التشغيل Microsoft Windows 2000 Professional نظام التشغيل Microsoft Windows 2000 ونظام التشغيل Microsoft Windows 2000 Server متقدمة ملقم أو ملقم Microsoft Windows NT 4.0
  • 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. إنشاء مفتاح سري لتشفير وفك تشفير البيانات. الفئة ديسكريبتوسيرفيسيبروفيدير استناداً إلى خوارزمية تشفير متماثل. يتطلب تشفير متماثل متجه تهيئة (IV) لتشفير البيانات ومفتاح. لفك تشفير البيانات، يجب أن يكون لديك نفس مفتاح و IV نفس. يجب عليك أيضا استخدام نفس خوارزمية التشفير. يمكنك إنشاء المفاتيح باستخدام أي من الطرق التالية:
    • الطريقة الأولى يمكنك مطالبة المستخدم بكلمة مرور. ثم استخدم كلمة المرور كالمفتاح و 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 المعلمات الثلاث:
    • سينبوتفيلي
    • سوتبوتفيلي
    • سكي (هذا هو المفتاح السري المستخدمة لتشفير وفك تشفير الملف.)

    Sub EncryptFile(ByVal sInputFilename As String, _
    ByVal sOutputFilename As String, _
    ByVal sKey As String)
    End Sub
  7. إنشاء كائن FileStream إدخال وإخراج في الإجراء EncryptFile ، كائن FileStream . هذه الكائنات يمكن القراءة من والكتابة إلى الملف الهدف.

    Dim fsInput As New FileStream(sInputFilename, _
    FileMode.Open, FileAccess.Read)
    Dim fsEncrypted As New FileStream(sOutputFilename, _
    FileMode.Create, FileAccess.Write)
  8. تعريف مثيل من فئة ديسكريبتوسيرفيسيبروفيدير . يمثل هذا التشفير الفعلي وتكنولوجيا فك التشفير الفعلي الذي يتم استخدامه على الملفات. عند هذه النقطة، يمكنك إنشاء موفر مختلف إذا كنت تريد استخدام أمان 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 . استخدام موفر التشفير للحصول على كائن تشفير (كريتينكريبتور) والموجودة على إخراج الكائن 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. إنشاء أسلوب يسمى ديكريبتفيلي. يشبه عملية فك التشفير عملية التشفير. ومع ذلك، ديكريبتفيلي على الاختلافات الأساسية بين من إجراء EncryptFile .
    • يتم استخدام كريتيديكريبتور بدلاً من كريتينكريبتور لإنشاء كائن 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 و ديكريبتفيلي.

    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) للتأكد من أن الملف بشكل صحيح تشفير وفك تشفير. تأكد من تشفير الملف إلى ملف جديد (كما هو الحال في إجراء Sub main () في هذه المقالة) بدلاً من الملف الأصلي. فحص الملف الذي تم فك تشفيره ومقارنة الملف الأصلي الذي تم فك تشفيره.

عودة إلى الأعلى

قائمة التعليمات برمجية الكاملة

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 Visual Basic أو Visual Basic 2005 Reviewed by Unknown on 8:17 م Rating: 5

ليست هناك تعليقات:

All Rights Reserved by محترفي التكنولوجيا © 2014 - 2015
Powered by Themes24x7

نموذج الاتصال

الاسم

بريد إلكتروني *

رسالة *

يتم التشغيل بواسطة Blogger.