PDA

View Full Version : رمزنگاری یک شی از نوع triple des در دلفی 2007



nafasak
یک شنبه 20 تیر 1395, 23:31 عصر
سلام
من دوتا تابع دارم به زبان VB که یک آبجکت رو رمزنگاری میکه و بصورت بایت خروجی میده،
در دلفی 2007، چطور میتونم این کار رو انجام بدم؟؟


Public Function SecuredObject(ByVal obj As Object) As Byte() Implements ISecureShell.SecuredObject

Dim b_object() As Byte
Dim xml_serializer As New XmlSerializer(obj.GetType)
Dim mem As New MemoryStream
xml_serializer.Serialize(mem, obj)
b_object = mem.GetBuffer()


Dim b_zip() As Byte
Dim ms As New MemoryStream()
Dim zipStream As Stream = Nothing
zipStream = New GZipStream(ms,
CompressionMode.Compress, True)
zipStream.Write(b_object, 0, b_object.Length)
zipStream.Close()
ms.Position = 0
Dim compressed_data(ms.Length - 1) As Byte
ms.Read(compressed_data, 0, ms.Length)
b_zip = compressed_data

Dim b_Encrypt() As Byte
Dim ms2 As New IO.MemoryStream
Dim _crypto As SymmetricAlgorithm
_crypto = New TripleDESCryptoServiceProvider
_crypto.Key = FromHex("F21CFE97B6555EE84773806E6694326D2565892024C47FC7")
_crypto.IV = FromHex("5FBC9394BF8D71EF")
Dim cs As New CryptoStream(ms2, _crypto.CreateEncryptor(), CryptoStreamMode.Write)
cs.Write(b_zip, 0, b_zip.Length)
cs.Close()
ms2.Close()
b_Encrypt = ms2.ToArray


Return b_Encrypt
End Function

Private Function FromHex(ByVal hexEncoded As String) As Byte() Implements ISecureShell.FromHex
If hexEncoded Is Nothing OrElse hexEncoded.Length = 0 Then
Return Nothing
End If
Try
Dim l As Integer = Convert.ToInt32(hexEncoded.Length / 2)
Dim b(l - 1) As Byte
For i As Integer = 0 To l - 1
b(i) = Convert.ToByte(hexEncoded.Substring(i * 2, 2), 16)
Next
Return b
Catch ex As Exception
Throw New System.FormatException("The provided string does not appear to be Hex encoded:" &
Environment.NewLine & hexEncoded & Environment.NewLine, ex)
End Try
End Function