Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Source] Encrypt/Decrypt a string using Data Encryption Standard (DES) algorithm
#1
I created a sample for encrypt / decrypt a string using DES algorithm.

[Image: 4u7HR.png]

Full codes:
Code:
Imports System.Security.Cryptography
Imports System.IO

Public Class CryptoMemoryStream

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            ' Create a new DES key.
            Using key As New DESCryptoServiceProvider()
                ' Encrypt a string to a byte array.
                Dim buffer As Byte() = Encrypt(TextBox1.Text, key)

                ' Display the unicodesbytestostring value to the textboxes.
                TextBox2.Text = UnicodeBytesToString(buffer)
                TextBox3.Text = UnicodeBytesToString(buffer)
            End Using
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Message")
        End Try
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Try
            Using key As New DESCryptoServiceProvider()
                ' Decrypt the byte array back to a string.
                Dim buffer As Byte() = Encrypt(TextBox1.Text, key)
                Dim plaintext As String = Decrypt(buffer, key)

                ' Display the plaintext value to the textbox4.
                TextBox4.Text = plaintext
            End Using
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Message")
        End Try
    End Sub

    'Convert the bytes from a byte array into a string
    Private Shared Function UnicodeBytesToString(ByVal bytes() As Byte) As String
        Return System.Text.Encoding.Unicode.GetString(bytes)
    End Function

    ' Encrypt the string.
    Public Shared Function Encrypt(PlainText As String, key As SymmetricAlgorithm) As Byte()
        ' Create a memory stream.
        Dim ms As New MemoryStream()

        ' Create a CryptoStream using the memory stream and the
        ' CSP DES key.  
        Dim encStream As New CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write)

        ' Create a StreamWriter to write a string
        ' to the stream.
        Dim sw As New StreamWriter(encStream)

        ' Write the plaintext to the stream.
        sw.WriteLine(PlainText)

        ' Close the StreamWriter and CryptoStream.
        sw.Close()
        encStream.Close()

        ' Get an array of bytes that represents
        ' the memory stream.
        Dim buffer As Byte() = ms.ToArray()

        ' Close the memory stream.
        ms.Close()

        ' Return the encrypted byte array.
        Return buffer
    End Function 'Encrypt


    ' Decrypt the byte array.
    Public Shared Function Decrypt(CypherText() As Byte, key As SymmetricAlgorithm) As String
        ' Create a memory stream to the passed buffer.
        Dim ms As New MemoryStream(CypherText)

        ' Create a CryptoStream using the memory stream and the
        ' CSP DES key.
        Dim encStream As New CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read)

        ' Create a StreamReader for reading the stream.
        Dim sr As New StreamReader(encStream)

        ' Read the stream as a string.
        Dim val As String = sr.ReadLine()

        ' Close the streams.
        sr.Close()
        encStream.Close()
        ms.Close()

        Return val
    End Function 'Decrypt

End Class
USB Anti-virus? Try USB Drive Defender
[Image: 8bQCusS.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Encrypt String using x509Certificate private Key wih RSA jeffstan 0 2,434 01-26-2014, 04:18 PM
Last Post: jeffstan
  [C#] BitCrypt - Visually Encrypt Binary Data (Bitmap Structures) AceInfinity 4 1,996 08-01-2012, 05:14 PM
Last Post: AceInfinity
  Combination Cracker - String Variant Builder - Developed by AceInfinity AceInfinity 0 1,350 03-15-2012, 06:05 PM
Last Post: AceInfinity
  [Source ] Hemp Tycoon App [/Source] VB.net KoBE 8 9,758 03-05-2012, 10:30 PM
Last Post: SomeWhiteGuy?
  [Source]Sql Database Project [Source] Digital-Punk 0 1,440 10-16-2011, 07:01 AM
Last Post: Digital-Punk

Forum Jump:


Users browsing this thread: 1 Guest(s)