Results 1 to 4 of 4

Thread: Ben's Encryption codes

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Lightbulb Ben's Encryption codes

    Hi this page will contain some of my little snippets of encryption codes this is my first try it a basic encryption using XOR and random function it also uses a function to get a hash coded from your password hope you like this first example. comments and suggestions welcome.


    Sniplet 1 XOR using random encryption for strings.

    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private Function GetHash(ByVal source As String) As Integer
    4.         Dim hash As Integer = 0
    5.         'Add some random hex codes add more if you want.
    6.         Dim Magic = {&H3FEE, &H3A9B, &H3977, &H37A4, &H3583, &H3019, &H2F86, &H2E55, &H554}
    7.         'Build hash code.
    8.         For x As Integer = 0 To source.Length - 1
    9.             hash = (hash >> 2) Xor Asc(source(x)) + Magic(x And &H8)
    10.         Next x
    11.  
    12.         Return hash
    13.  
    14.     End Function
    15.  
    16.     Private Function EncryptString(ByVal source As String, ByVal Password As String) As String
    17.         Dim Hash As Integer = 0
    18.         Dim sb As New System.Text.StringBuilder()
    19.         'Get hash of password.
    20.         Hash = GetHash(Password)
    21.  
    22.         Rnd(-3)
    23.         'Random hash
    24.         Randomize(hash)
    25.  
    26.         For x As Integer = 0 To source.Length - 1
    27.             Dim b As Byte = Asc(source(x)) Xor Int(Rnd() * Hash) Mod 256
    28.             'Append chars.
    29.             sb.Append(Chr(b))
    30.         Next x
    31.  
    32.         'Return string
    33.         Return sb.ToString
    34.     End Function
    35.  
    36.     Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
    37.         Dim src As String = "hello,hello,hello world"
    38.  
    39.         Dim en As String = EncryptString(src, "secret")
    40.         Dim de As String = EncryptString(en, "secret")
    41.  
    42.         'Show encrypted string.
    43.         MessageBox.Show("Encrypted string: " & en, "Encrypt1", MessageBoxButtons.OK, MessageBoxIcon.Information)
    44.         'Show decrypted string.
    45.         MessageBox.Show("Decrypted string: " & de, "Encrypt1", MessageBoxButtons.OK, MessageBoxIcon.Information)
    46.     End Sub
    47.  
    48. End Class
    Last edited by BenJones; Sep 3rd, 2012 at 06:55 PM.

  2. #2

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Ben's Encryption codes

    Hi this is my second example this example shows you how to use XOR to encrypt and decrypt a file using a password.
    suggestions and comments welcome hope you like the example.

    Class code cXorFile

    vbnet Code:
    1. 'A simple to use XOR File encrypter / Decrypter
    2. 'By Ben Jones use this code as you like it's yours.
    3.  
    4. Option Explicit On
    5. Imports System.IO
    6.  
    7. Public Class cXorFile
    8.  
    9.     Private m_Password As String = vbNullString
    10.     Private m_Source As String = vbNullString
    11.     Private m_Output As String = vbNullString
    12.  
    13.     Public Property SourceFile As String
    14.         Get
    15.             Return m_Source
    16.         End Get
    17.         Set(ByVal value As String)
    18.             m_Source = value
    19.         End Set
    20.     End Property
    21.  
    22.     Public Property OutputFile As String
    23.         Get
    24.             Return m_Output
    25.         End Get
    26.         Set(ByVal value As String)
    27.             m_Output = value
    28.         End Set
    29.     End Property
    30.  
    31.     Public Property Password As String
    32.         Get
    33.             Return m_Password
    34.         End Get
    35.         Set(ByVal value As String)
    36.             m_Password = value
    37.         End Set
    38.     End Property
    39.  
    40.     Public Sub EncryptDecrypt()
    41.         Dim br As BinaryReader = Nothing
    42.         Dim bw As BinaryWriter = Nothing
    43.         Dim KeyHash As Integer = 0
    44.  
    45.         'Generate hash from password.
    46.         For x As Integer = 0 To 256
    47.             KeyHash += Asc(Password(x Mod Len(Password))) << 8
    48.         Next x
    49.  
    50.         Rnd(-1)
    51.         'Set random seed.
    52.         Randomize(KeyHash)
    53.  
    54.         'Check that source file is found.
    55.         If Not File.Exists(SourceFile) Then
    56.             Throw New FileNotFoundException
    57.             Exit Sub
    58.         End If
    59.  
    60.         Try
    61.             'Open files for reading and writeing.
    62.             br = New BinaryReader(File.OpenRead(SourceFile))
    63.             bw = New BinaryWriter(File.OpenWrite(OutputFile))
    64.  
    65.             'Loop though the file xor each byte and write to output file.
    66.             For x As Integer = 0 To br.BaseStream.Length - 1
    67.                 'Xor byte
    68.                 Dim b As Byte = br.ReadByte Xor Int(Rnd() * KeyHash) Mod 256
    69.                 'Write byte.
    70.                 bw.Write(b)
    71.             Next x
    72.  
    73.             bw.Close() 'Close output file.
    74.             br.Close() 'Close input file.
    75.         Catch ex As Exception
    76.             Throw New ArgumentException(ex.Message)
    77.         End Try
    78.  
    79.     End Sub
    80.  
    81. End Class

    Example code

    vbnet Code:
    1. Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
    2.         Dim MyCrypt As New cXorFile()
    3.  
    4.         With MyCrypt
    5.             'Set password.
    6.             .Password = "idkfa65"
    7.             'Source filename.
    8.             .SourceFile = "C:\out\data\test.txt"
    9.             'Output filename.
    10.             .OutputFile = "C:\out\data\test.dat"
    11.  
    12.             Try
    13.                 'Do the encryption or decryption.
    14.                 .EncryptDecrypt()
    15.             Catch ex As Exception
    16.                 'Display error message.
    17.                 MessageBox.Show(ex.Message, "Error",
    18.                                 MessageBoxButtons.OK, MessageBoxIcon.Error)
    19.             End Try
    20.         End With
    21.     End Sub

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Ben's Encryption codes

    Here is some code I found on one of my old backup dvds I made years ago in QBasic it is a simple encryption and decryption I thought I place it here with the rest of my examples.
    Anyway this example uses a key value to encrypt and decrypt. comments and suggestions welcome.

    vbnet Code:
    1. Private Sub EncryptFile(ByVal Input As String, ByVal Output As String, ByVal MagicKey As Byte)
    2.         Dim br As BinaryReader = Nothing
    3.         Dim bw As BinaryWriter
    4.         Dim bKey As Integer = 0
    5.  
    6.         Try
    7.             br = New BinaryReader(File.OpenRead(Input))
    8.         Catch ex As Exception
    9.             Throw New ArgumentException(ex.Message)
    10.         End Try
    11.  
    12.         bw = New BinaryWriter(File.OpenWrite(Output))
    13.         For x As Integer = 0 To br.BaseStream.Length - 1
    14.             'Read byte from file.
    15.             Dim b As Byte = br.ReadByte()
    16.             'Encode key
    17.             bKey = (b + MagicKey)
    18.  
    19.             'If more than 255 minus 255
    20.             If (bKey > 255) Then
    21.                 bKey -= 255
    22.             End If
    23.  
    24.             'Write byte to file.
    25.             bw.Write(CByte(bKey))
    26.         Next x
    27.  
    28.         'Close files.
    29.         bw.Close()
    30.         br.Close()
    31.  
    32.     End Sub
    33.  
    34.     Private Sub DecryptFile(ByVal Input As String, ByVal Output As String, ByVal MagicKey As Byte)
    35.         Dim br As BinaryReader
    36.         Dim bw As BinaryWriter
    37.         Dim bKey As Integer = 0
    38.  
    39.         Try
    40.             br = New BinaryReader(File.OpenRead(Input))
    41.         Catch ex As Exception
    42.             Throw New ArgumentException(ex.Message)
    43.         End Try
    44.  
    45.         bw = New BinaryWriter(File.OpenWrite(Output))
    46.  
    47.         For x As Integer = 0 To br.BaseStream.Length - 1
    48.             'Read byte from file.
    49.             Dim b As Byte = br.ReadByte()
    50.  
    51.             'Decrypt key
    52.             bKey = (b - MagicKey)
    53.  
    54.             'If less than 0 plus 255
    55.             If (bKey < 0) Then
    56.                 bKey += 255
    57.             End If
    58.  
    59.             'Write byte to file.
    60.             bw.Write(CByte(bKey))
    61.         Next x
    62.  
    63.         'Close files.
    64.         bw.Close()
    65.         br.Close()
    66.     End Sub
    67.  
    68.     Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
    69.         'Do the encryption and decryption.
    70.         EncryptFile("C:\out\data\test.txt", "C:\out\data\out.txt", 128)
    71.         DecryptFile("C:\out\data\out.txt", "C:\out\data\new.txt", 128)
    72.     End Sub

  4. #4

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Ben's Encryption codes

    Hi all this is a new example this uses the built in TripleDES functions in VB.NET this example has a class and an example. The class allows you to encrypt decrypt files with a password.
    It also has two string functions for encrypting and decrypting strings.
    Hope you find this of some use. comments and suggestions welcome.

    class code cDes.vb

    vbnet Code:
    1. 'TripleDES encrypter / Decrypter for files and strings.
    2. 'This example is part of Bens Jones encryption codes use as you like.
    3.  
    4. Option Explicit On
    5.  
    6. Imports System.Security.Cryptography
    7. Imports System.Text
    8. Imports System.IO
    9.  
    10. Public Class cDes
    11.  
    12.     'Class variables.
    13.     Private m_Input As String = vbNullString
    14.     Private m_Output As String = vbNullString
    15.     Private m_Password As String = vbNullString
    16.  
    17.     Public Property InputFile As String
    18.         Get
    19.             Return m_Input
    20.         End Get
    21.         Set(ByVal value As String)
    22.             m_Input = value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Property OutputFile As String
    27.         Get
    28.             Return m_Output
    29.         End Get
    30.         Set(ByVal value As String)
    31.             m_Output = value
    32.         End Set
    33.     End Property
    34.  
    35.     Public Property Password As String
    36.         Get
    37.             Return m_Password
    38.         End Get
    39.         Set(ByVal value As String)
    40.             m_Password = value
    41.         End Set
    42.     End Property
    43.  
    44.     Public Sub DecryptFile()
    45.         'Decrypt file.
    46.         DoAction(False)
    47.     End Sub
    48.  
    49.     Public Sub EncryptFile()
    50.         'Encrypt file.
    51.         DoAction(True)
    52.     End Sub
    53.  
    54.     Public Function EncryptString(ByVal source As String, ByVal Password As String) As String
    55.         Dim Bytes() As Byte
    56.  
    57.         Dim Des As New TripleDESCryptoServiceProvider()
    58.         Dim Hash As New MD5CryptoServiceProvider()
    59.         Dim EnTransForm As ICryptoTransform = Nothing
    60.  
    61.         Try
    62.             'Create encrypter.
    63.             Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
    64.             Des.Mode = CipherMode.ECB
    65.             EnTransForm = Des.CreateEncryptor()
    66.             'Get bytes from string.
    67.             Bytes = Encoding.ASCII.GetBytes(source)
    68.             'Return base64 string.
    69.             Return Convert.ToBase64String(EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length))
    70.         Catch ex As Exception
    71.             Throw New ArgumentException(ex.Message)
    72.         End Try
    73.     End Function
    74.  
    75.     Public Function DecryptString(ByVal source As String, ByVal Password As String) As String
    76.         Dim Bytes() As Byte
    77.  
    78.         Dim Des As New TripleDESCryptoServiceProvider()
    79.         Dim Hash As New MD5CryptoServiceProvider()
    80.         Dim EnTransForm As ICryptoTransform = Nothing
    81.  
    82.         Try
    83.             'Create decrypter.
    84.             Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
    85.             Des.Mode = CipherMode.ECB
    86.             EnTransForm = Des.CreateDecryptor()
    87.             'Get bytes from string.
    88.             Bytes = Convert.FromBase64String(source)
    89.             'Return string.
    90.             Return Encoding.ASCII.GetString(EnTransForm.TransformFinalBlock(Bytes,0,Bytes.Length))
    91.         Catch ex As Exception
    92.             Throw New ArgumentException(ex.Message)
    93.         End Try
    94.     End Function
    95.  
    96.     Private Sub DoAction(ByVal Encrypt As Boolean)
    97.         Dim br As BinaryReader = Nothing
    98.         Dim bw As BinaryWriter = Nothing
    99.         Dim Bytes() As Byte
    100.  
    101.         Dim Des As New TripleDESCryptoServiceProvider()
    102.         Dim Hash As New MD5CryptoServiceProvider()
    103.         Dim EnTransForm As ICryptoTransform = Nothing
    104.  
    105.         'Check for password.
    106.         If String.IsNullOrWhiteSpace(Password) Then
    107.             Throw New ArgumentException("No Password Found")
    108.         End If
    109.  
    110.         Try
    111.             br = New BinaryReader(File.OpenRead(InputFile))
    112.             'Create hash from password.
    113.             Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
    114.             'Set mode.
    115.             Des.Mode = CipherMode.ECB
    116.  
    117.             If Encrypt Then
    118.                 'Create encrypter.
    119.                 EnTransForm = Des.CreateEncryptor()
    120.             Else
    121.                 'Create decrypter.
    122.                 EnTransForm = Des.CreateDecryptor()
    123.             End If
    124.  
    125.             'Read bytes from input filename.
    126.             Bytes = br.ReadBytes(br.BaseStream.Length)
    127.             'Encrypt bytes.
    128.             Bytes = EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length)
    129.             'Create output filename.
    130.             bw = New BinaryWriter(File.OpenWrite(OutputFile))
    131.             'Write bytes to output file.
    132.             bw.Write(Bytes)
    133.  
    134.             'Close open files.
    135.             bw.Close()
    136.             br.Close()
    137.         Catch ex As Exception
    138.             Throw New ArgumentException(ex.Message)
    139.         End Try
    140.     End Sub
    141. End Class

    Examples

    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private Sub cmdTest1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest1.Click
    4.         Dim MyDes As New cDes()
    5.  
    6.         With MyDes
    7.             'Set password for encrypting and decrypting.
    8.             .Password = "crimson"
    9.  
    10.             Try
    11.                 'Encrypt file.
    12.                 .InputFile = "C:\out\data\test.txt"
    13.                 .OutputFile = "C:\out\data\out.txt"
    14.                 .EncryptFile()
    15.                 'Decrypt file.
    16.                 .InputFile = "C:\out\data\out.txt"
    17.                 .OutputFile = "C:\out\data\new.txt"
    18.                 .DecryptFile()
    19.             Catch ex As Exception
    20.                 MessageBox.Show(ex.Message, "Error",
    21.                                 MessageBoxButtons.OK, MessageBoxIcon.Error)
    22.             End Try
    23.         End With
    24.     End Sub
    25.  
    26.     Private Sub cmdTest2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest2.Click
    27.         Dim MyDes As New cDes()
    28.         Dim Demo As String = "Hello World"
    29.  
    30.         'Original
    31.         MessageBox.Show("Original: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    32.  
    33.         'Encrypt string
    34.         Demo = MyDes.EncryptString(Demo, "alpha45")
    35.         MessageBox.Show("Encrypted: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    36.  
    37.         'Decrypt string
    38.         Demo = MyDes.DecryptString(Demo, "alpha45")
    39.         MessageBox.Show("Decrypted: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    40.     End Sub
    41. End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width