05-10-12, 01:40 AM
كاتب الموضوع : samerselo
فكرة المشروع – بناء خدمة ويندوز تقوم بتشفير أي ملف تلقائيا عند نسخه إلى مجلد معين وسنستخدم هنا إجرائية تشفير قابلة للعكس بحيث أن عملية إعادة نسخ الملف لذلك المجلد تقوم بفك تشفيرهأنشئ مشروعا جديدا من نوع Windows Service وقم بتسميته AutoFileEncryptor وغير تسمية Service1.vb إلى AutoFileEncryptor.vb ووافق على الرسالة التي تطلب منك إعادة تسمية الفئة ثم افتح محرر التصميم لـ AutoFileEncryptor و اسحب العنصر FileSystemWatcher على سطع النافذة ثم افتح محرر الكود الخاص بـ AutoFileEncryptor
أضف التعريف التالي في القسم العام للفئة AutoFileEncryptor
كود :
Private WatcPath = "d:\AutoFileEncryptor"
كود :
Public Class AutoFileEncryptor
Private WatcPath = "d:\AutoFileEncryptor"
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Me.FileSystemWatcher1.Path = WatcPath
Me.FileSystemWatcher1.EnableRaisingEvents = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Me.FileSystemWatcher1.EnableRaisingEvents = False
End Sub
End Class
كود :
' This is the binary password.
Dim pwBytes() As Byte = {123, 234, 12, 9, 78, 89, 212}
' This is the extension used for temporary files.
Dim tempExt As String = ".$$$"
كود :
Imports System.IO
كود :
' This is the encryption/decryption routine.
Private Sub EncryptFile(ByVal Filename As String, ByVal pwBytes() As Byte)
' This is the size of each input block.
' (Files must be decrypted using the same block size.)
Const BLOCKSIZE = 8192
' Determine the name of the temporary file.
Dim tempFile As String = Filename & tempExt
' Open the source file as a binary input stream.
Dim inStream As New FileStream(Filename, IO.FileMode.Open)
' Open the temporary output file as a binary input stream.
Dim outStream As New FileStream(tempFile, IO.FileMode.Create)
' Determine the number of bytes to read.
Dim bytesLeft As Long = inStream.Length
' Prepare an input buffer.
Dim buffer(BLOCKSIZE - 1) As Byte
' Loop until there are bytes to read.
Do While bytesLeft > 0
' Read max 8 KB at a time.
Dim bytesToRead As Long = Math.Min(BLOCKSIZE, bytesLeft)
' Read into the input buffer.
inStream.Read(buffer, 0, bytesToRead)
' Encrypt this buffer.
EncryptArray(buffer, pwBytes)
' Output to the temporary file.
outStream.Write(buffer, 0, bytesToRead)
' We have fewer bytes to read now.
bytesLeft -= bytesToRead
Loop
' Close the two streams.
inStream.Close()
outStream.Close()
' Delete the source file.
File.Delete(Filename)
' Rename the temporary file as the original file.
File.Move(tempFile, Filename)
End Sub
' This routine encrypts an array of bytes.
Sub EncryptArray(ByVal buffer() As Byte, ByVal pwBytes() As Byte)
' This index points to the password array.
Dim i As Integer
' The max value for i
Dim maxval As Integer = pwBytes.Length
For index As Integer = 0 To buffer.Length - 1
' XOR each element with the corresponding element in the password.
buffer(index) = buffer(index) Xor pwBytes(i)
' Ensure that the index is always in the valid range.
i = (i + 1) Mod maxval
Next
End Sub
كود :
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, _
ByVal e As System.IO.FileSystemEventArgs) _
Handles FileSystemWatcher1.Created
' Ignore temporary files created by the encryption process.
If System.IO.Path.GetExtension(e.FullPath) = tempExt Then Exit Sub
' Encrypt the file being created.
EncryptFile(e.FullPath, pwBytes)
End Sub
كود :
If Not Directory.Exists(WatcPath) Then
Directory.CreateDirectory(WatcPath)
End If
كود :
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
If Not Directory.Exists(WatcPath) Then
Directory.CreateDirectory(WatcPath)
End If
Me.FileSystemWatcher1.Path = WatcPath
Me.FileSystemWatcher1.EnableRaisingEvents = True
End Sub
قم بعمل Build للمشروع
أنشئ مجلدا مؤقتا باسم Temp على السواقة D وانسخ الملف التنفيذي للمشروع إليه
ثم أنشئ فيه ملفا نصيا باسم Install.bat بحيث تكون محتوياته
كود :
INSTALLUTIL AutoFileEncryptor.exe
كود :
INSTALLUTIL /U AutoFileEncryptor.exe
كود :
D:
CD\temp
Install
اترك نافذة الكونسول مفتوحة وانتقل الآن إلى بيئة التطوير وانتقل إلى نافذة Server Explorer ووسع الشجرة حتى ترى العقدة Services انقر على العقدة Services بزر الفأرة اليميني و اختار الأمر Launch Services Manager ابحث ضمن القائمة عن Auto File Encryptor ثم قم ببدء تشغيل الخدمة
لتجربة الخدمة انسخ أي ملف نصي إلى المجلد d:\AutoFileEncryptor وحاول قراءة محتويته ستراها بالصيغة المشفرة ولفك تشفير الملف انقله إلى أي مجلد آخر ثم انسخه للمجلد d:\AutoFileEncryptor مرة ثانية ليتم فك تشفيره تلقائيا
لإزالة الخدمة عد إلى برنامج Services Manager وقم بإيقاف الخدمة ثم عد إلى نافذة الكونسول ونفذ الأمر Uninstall