Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Start with reversing the string
- Public Function ReverseString(ByRef strToReverse As String) As String
- Dim result As String = ""
- For i As Integer = 0 To strToReverse.Length - 1
- result += strToReverse(strToReverse.Length - 1 - i)
- Next
- Return result
- End Function
- 'then get byte value
- ' The input String.
- Dim value As String = Textbox1.Text
- ' Convert String to Byte array.
- Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes(value)
- ' Display Bytes.
- For Each b As Byte In array
- Textbox1.Text = ("{0} = {1}", b, ChrW(b))
- 'Byte2String
- ' Input Bytes.
- Dim array() As Byte = {86, 66, 46, 78, 69, 84}
- ' Use GetString.
- Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array)
- Console.WriteLine(value)
- 'Another string 2 byte converter
- Private Shared Function GetBytes(str As String) As Byte()
- Dim bytes As Byte() = New Byte(str.Length * 2 - 1) {}
- System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length)
- Return bytes
- End Function
- Private Shared Function GetString(bytes As Byte()) As String
- Dim chars As Char() = New Char(bytes.Length / 2 - 1) {}
- System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length)
- Return New String(chars)
- End Function
- ' Byte Encoder
- 'encryption key
- Public Shared Key As Byte() = New Byte() {&H43, &H72, &H6e, &H6d, &H54, &H4d, _
- &H65, &H94, &H16, &H32, &H44, &H84, _
- &H7e, &H18, &H64, &H76, &H6e, &H63, _
- &H64, &H7a, &H5f, &H84, &H7f, &H9a}
- 'Decrypt byte[]
- Public Shared Function Decrypt(data As Byte()) As Byte()
- Dim ms As New MemoryStream()
- Dim alg As Rijndael = Rijndael.Create()
- alg.Key = Key
- Dim cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
- cs.Write(data, 0, data.Length)
- cs.Close()
- Dim decryptedData As Byte() = ms.ToArray()
- Return decryptedData
- End Function
- 'Encrypt byte[]
- Public Shared Function Encrypt(data As Byte()) As Byte()
- Dim ms As New MemoryStream()
- Dim alg As Rijndael = Rijndael.Create()
- alg.Key = Key
- Dim cs As New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write)
- cs.Write(data, 0, data.Length)
- cs.Close()
- Dim encryptedData As Byte() = ms.ToArray()
- Return encryptedData
- End Function
- 'serialize object to memory stream
- Public Shared Function SerializeToStream(o As Object) As MemoryStream
- Dim stream As New MemoryStream()
- Dim formatter As IFormatter = New BinaryFormatter()
- formatter.Serialize(stream, o)
- Return stream
- End Function
- 'deserialize object from memory stream
- Public Shared Function DerializeFromStream(Of T As New)(memoryStream As MemoryStream) As T
- If memoryStream Is Nothing Then
- Return New T()
- End If
- Dim o As T
- Dim binaryFormatter As New BinaryFormatter()
- Using memoryStream
- memoryStream.Seek(0, SeekOrigin.Begin)
- o = DirectCast(binaryFormatter.Deserialize(memoryStream), T)
- End Using
- Return o
- End Function
- 'Another byte modifier
- Private Shared Sub EncryptThenDecrypt()
- Dim message As Byte()
- ' fill with your bytes
- Dim encMessage As Byte()
- ' the encrypted bytes
- Dim decMessage As Byte()
- ' the decrypted bytes - s/b same as message
- Dim key As Byte()
- Dim iv As Byte()
- Using rijndael = New RijndaelManaged()
- rijndael.GenerateKey()
- rijndael.GenerateIV()
- key = rijndael.Key
- iv = rijndael.IV
- encMessage = EncryptBytes(rijndael, message)
- End Using
- Using rijndael = New RijndaelManaged()
- rijndael.Key = key
- rijndael.IV = iv
- decMessage = DecryptBytes(rijndael, encMessage)
- End Using
- End Sub
- Private Shared Function EncryptBytes(alg As SymmetricAlgorithm, message As Byte()) As Byte()
- If (message Is Nothing) OrElse (message.Length = 0) Then
- Return message
- End If
- If alg Is Nothing Then
- Throw New ArgumentNullException("alg")
- End If
- Using stream = New MemoryStream()
- Using encryptor = alg.CreateEncryptor()
- Using encrypt = New CryptoStream(stream, encryptor, CryptoStreamMode.Write)
- encrypt.Write(message, 0, message.Length)
- encrypt.FlushFinalBlock()
- Return stream.ToArray()
- End Using
- End Using
- End Using
- End Function
- Private Shared Function DecryptBytes(alg As SymmetricAlgorithm, message As Byte()) As Byte()
- If (message Is Nothing) OrElse (message.Length = 0) Then
- Return message
- End If
- If alg Is Nothing Then
- Throw New ArgumentNullException("alg")
- End If
- Using stream = New MemoryStream()
- Using decryptor = alg.CreateDecryptor()
- Using encrypt = New CryptoStream(stream, decryptor, CryptoStreamMode.Write)
- encrypt.Write(message, 0, message.Length)
- encrypt.FlushFinalBlock()
- Return stream.ToArray()
- End Using
- End Using
- End Using
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement