السلام عليكم ورحمة الله..
قمت بالتعديل على هذا الكلاس لمساعدتي في تشفير النصوص بشكل أسرع..
يقوم الكلاس بتوليد مفتاح التشفير ثم حفظه في ملف باينري من خلال الStructure ثم عندما أقوم بتحديد الملف يتم فك التشفير بناء على المفتاح المحفوظ في ملف الباينري..
الآن قمت بتوليد ملف المفتاح في البرنامج الأول من خلال GetNewSecurityKyeFile في الكلاس؛ فلما رجت لقراءته في البرنامج الثاني لم يقبل قراءته لأن الملف الباينري يحتوي على ترويسة فيها معلومات البرنامج الذي قام بكتابة هذا الملف؛ وهذه المعلومات لا تتطابق مع معلومات البرنامج الثاني..
KMyPgrName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
الآن ما الحل لحفظ الملف الباينري من غير كتابة هذه الترويسة؟ أو ما كيفية قراءة الملف الباينري من البرنامج الثاني مع تجاهل معلومات الترويسة؟
Imports System.Security.Cryptography Imports System.IO Imports System.Text Public Class StringSecurity Dim ThisSecurityStructure As MySecurityStructure <Serializable()> Private Structure MySecurityStructure Public Key() As Byte Public IV() As Byte End Structure Public Sub New(Optional ByVal SecurityKyeFileName As String = "") If IO.File.Exists(SecurityKyeFileName) Then Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Try Dim bytes As Byte() = My.Computer.FileSystem.ReadAllBytes(SecurityKyeFileName) ThisSecurityStructure = DirectCast(BF.Deserialize(New System.IO.MemoryStream(bytes)), MySecurityStructure) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical) End Try Else Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider tdes.GenerateKey() tdes.GenerateIV() ThisSecurityStructure.Key = tdes.Key ThisSecurityStructure.IV = tdes.IV End If End Sub Public Function Encrypt(ByVal strData As String) As Byte() Dim data() As Byte = UTF8Encoding.UTF8.GetBytes(strData) Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider tdes.Key = ThisSecurityStructure.Key tdes.IV = ThisSecurityStructure.IV Dim encryptor As ICryptoTransform = tdes.CreateEncryptor() Dim ms As New MemoryStream Dim cs As CryptoStream = New CryptoStream(ms, encryptor, CryptoStreamMode.Write) cs.Write(data, 0, data.Length) cs.FlushFinalBlock() ms.Position = 0 Dim result(ms.Length - 1) As Byte ms.Read(result, 0, ms.Length) cs.Close() Return result End Function Public Function Decrypt(ByVal data() As Byte) As String Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider tdes.Key = ThisSecurityStructure.Key tdes.IV = ThisSecurityStructure.IV Dim decryptor As ICryptoTransform = tdes.CreateDecryptor() Dim ms As New MemoryStream Dim cs As CryptoStream = New CryptoStream(ms, decryptor, CryptoStreamMode.Write) cs.Write(data, 0, data.Length) cs.FlushFinalBlock() ms.Position = 0 Dim result(ms.Length - 1) As Byte ms.Read(result, 0, ms.Length) cs.Close() Return UTF8Encoding.UTF8.GetString(result) End Function Public Function GetNewSecurityKyeFile(ByVal FileName As String) As Boolean Dim tdes As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider tdes.GenerateKey() tdes.GenerateIV() Dim d As MySecurityStructure d.Key = tdes.Key() d.IV = tdes.IV() If IO.File.Exists(FileName) = True Then Kill(FileName) Dim BF As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim MS As New System.IO.MemoryStream() Try BF.Serialize(MS, d) My.Computer.FileSystem.WriteAllBytes(FileName, MS.GetBuffer(), False) Return True Catch ex As Exception MyErrStr = ex.Message Return False End Try End Function End Class
ملاحظة خارجة عن الموضوع: محرر المشاركات في المنتدى متعب جداً كلما أضع الكود يظهرها مع رموز HTML