-
Sep 3rd, 2012, 05:33 PM
#1
Thread Starter
Fanatic Member
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:
Public Class Form1 Private Function GetHash(ByVal source As String) As Integer Dim hash As Integer = 0 'Add some random hex codes add more if you want. Dim Magic = {&H3FEE, &H3A9B, &H3977, &H37A4, &H3583, &H3019, &H2F86, &H2E55, &H554} 'Build hash code. For x As Integer = 0 To source.Length - 1 hash = (hash >> 2) Xor Asc(source(x)) + Magic(x And &H8) Next x Return hash End Function Private Function EncryptString(ByVal source As String, ByVal Password As String) As String Dim Hash As Integer = 0 Dim sb As New System.Text.StringBuilder() 'Get hash of password. Hash = GetHash(Password) Rnd(-3) 'Random hash Randomize(hash) For x As Integer = 0 To source.Length - 1 Dim b As Byte = Asc(source(x)) Xor Int(Rnd() * Hash) Mod 256 'Append chars. sb.Append(Chr(b)) Next x 'Return string Return sb.ToString End Function Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click Dim src As String = "hello,hello,hello world" Dim en As String = EncryptString(src, "secret") Dim de As String = EncryptString(en, "secret") 'Show encrypted string. MessageBox.Show("Encrypted string: " & en, "Encrypt1", MessageBoxButtons.OK, MessageBoxIcon.Information) 'Show decrypted string. MessageBox.Show("Decrypted string: " & de, "Encrypt1", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Class
Last edited by BenJones; Sep 3rd, 2012 at 06:55 PM.
-
Sep 16th, 2012, 12:44 PM
#2
Thread Starter
Fanatic Member
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:
'A simple to use XOR File encrypter / Decrypter 'By Ben Jones use this code as you like it's yours. Option Explicit On Imports System.IO Public Class cXorFile Private m_Password As String = vbNullString Private m_Source As String = vbNullString Private m_Output As String = vbNullString Public Property SourceFile As String Get Return m_Source End Get Set(ByVal value As String) m_Source = value End Set End Property Public Property OutputFile As String Get Return m_Output End Get Set(ByVal value As String) m_Output = value End Set End Property Public Property Password As String Get Return m_Password End Get Set(ByVal value As String) m_Password = value End Set End Property Public Sub EncryptDecrypt() Dim br As BinaryReader = Nothing Dim bw As BinaryWriter = Nothing Dim KeyHash As Integer = 0 'Generate hash from password. For x As Integer = 0 To 256 KeyHash += Asc(Password(x Mod Len(Password))) << 8 Next x Rnd(-1) 'Set random seed. Randomize(KeyHash) 'Check that source file is found. If Not File.Exists(SourceFile) Then Throw New FileNotFoundException Exit Sub End If Try 'Open files for reading and writeing. br = New BinaryReader(File.OpenRead(SourceFile)) bw = New BinaryWriter(File.OpenWrite(OutputFile)) 'Loop though the file xor each byte and write to output file. For x As Integer = 0 To br.BaseStream.Length - 1 'Xor byte Dim b As Byte = br.ReadByte Xor Int(Rnd() * KeyHash) Mod 256 'Write byte. bw.Write(b) Next x bw.Close() 'Close output file. br.Close() 'Close input file. Catch ex As Exception Throw New ArgumentException(ex.Message) End Try End Sub End Class
Example code
vbnet Code:
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click Dim MyCrypt As New cXorFile() With MyCrypt 'Set password. .Password = "idkfa65" 'Source filename. .SourceFile = "C:\out\data\test.txt" 'Output filename. .OutputFile = "C:\out\data\test.dat" Try 'Do the encryption or decryption. .EncryptDecrypt() Catch ex As Exception 'Display error message. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End With End Sub
-
Sep 26th, 2012, 03:00 PM
#3
Thread Starter
Fanatic Member
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:
Private Sub EncryptFile(ByVal Input As String, ByVal Output As String, ByVal MagicKey As Byte)
Dim br As BinaryReader = Nothing
Dim bw As BinaryWriter
Dim bKey As Integer = 0
Try
br = New BinaryReader(File.OpenRead(Input))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
bw = New BinaryWriter(File.OpenWrite(Output))
For x As Integer = 0 To br.BaseStream.Length - 1
'Read byte from file.
Dim b As Byte = br.ReadByte()
'Encode key
bKey = (b + MagicKey)
'If more than 255 minus 255
If (bKey > 255) Then
bKey -= 255
End If
'Write byte to file.
bw.Write(CByte(bKey))
Next x
'Close files.
bw.Close()
br.Close()
End Sub
Private Sub DecryptFile(ByVal Input As String, ByVal Output As String, ByVal MagicKey As Byte)
Dim br As BinaryReader
Dim bw As BinaryWriter
Dim bKey As Integer = 0
Try
br = New BinaryReader(File.OpenRead(Input))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
bw = New BinaryWriter(File.OpenWrite(Output))
For x As Integer = 0 To br.BaseStream.Length - 1
'Read byte from file.
Dim b As Byte = br.ReadByte()
'Decrypt key
bKey = (b - MagicKey)
'If less than 0 plus 255
If (bKey < 0) Then
bKey += 255
End If
'Write byte to file.
bw.Write(CByte(bKey))
Next x
'Close files.
bw.Close()
br.Close()
End Sub
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
'Do the encryption and decryption.
EncryptFile("C:\out\data\test.txt", "C:\out\data\out.txt", 128)
DecryptFile("C:\out\data\out.txt", "C:\out\data\new.txt", 128)
End Sub
-
Sep 28th, 2012, 03:56 PM
#4
Thread Starter
Fanatic Member
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:
'TripleDES encrypter / Decrypter for files and strings.
'This example is part of Bens Jones encryption codes use as you like.
Option Explicit On
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class cDes
'Class variables.
Private m_Input As String = vbNullString
Private m_Output As String = vbNullString
Private m_Password As String = vbNullString
Public Property InputFile As String
Get
Return m_Input
End Get
Set(ByVal value As String)
m_Input = value
End Set
End Property
Public Property OutputFile As String
Get
Return m_Output
End Get
Set(ByVal value As String)
m_Output = value
End Set
End Property
Public Property Password As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
Public Sub DecryptFile()
'Decrypt file.
DoAction(False)
End Sub
Public Sub EncryptFile()
'Encrypt file.
DoAction(True)
End Sub
Public Function EncryptString(ByVal source As String, ByVal Password As String) As String
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
Try
'Create encrypter.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
Des.Mode = CipherMode.ECB
EnTransForm = Des.CreateEncryptor()
'Get bytes from string.
Bytes = Encoding.ASCII.GetBytes(source)
'Return base64 string.
Return Convert.ToBase64String(EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Function
Public Function DecryptString(ByVal source As String, ByVal Password As String) As String
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
Try
'Create decrypter.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
Des.Mode = CipherMode.ECB
EnTransForm = Des.CreateDecryptor()
'Get bytes from string.
Bytes = Convert.FromBase64String(source)
'Return string.
Return Encoding.ASCII.GetString(EnTransForm.TransformFinalBlock(Bytes,0,Bytes.Length))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Function
Private Sub DoAction(ByVal Encrypt As Boolean)
Dim br As BinaryReader = Nothing
Dim bw As BinaryWriter = Nothing
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
'Check for password.
If String.IsNullOrWhiteSpace(Password) Then
Throw New ArgumentException("No Password Found")
End If
Try
br = New BinaryReader(File.OpenRead(InputFile))
'Create hash from password.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
'Set mode.
Des.Mode = CipherMode.ECB
If Encrypt Then
'Create encrypter.
EnTransForm = Des.CreateEncryptor()
Else
'Create decrypter.
EnTransForm = Des.CreateDecryptor()
End If
'Read bytes from input filename.
Bytes = br.ReadBytes(br.BaseStream.Length)
'Encrypt bytes.
Bytes = EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length)
'Create output filename.
bw = New BinaryWriter(File.OpenWrite(OutputFile))
'Write bytes to output file.
bw.Write(Bytes)
'Close open files.
bw.Close()
br.Close()
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Sub
End Class
Examples
vbnet Code:
Public Class Form1
Private Sub cmdTest1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest1.Click
Dim MyDes As New cDes()
With MyDes
'Set password for encrypting and decrypting.
.Password = "crimson"
Try
'Encrypt file.
.InputFile = "C:\out\data\test.txt"
.OutputFile = "C:\out\data\out.txt"
.EncryptFile()
'Decrypt file.
.InputFile = "C:\out\data\out.txt"
.OutputFile = "C:\out\data\new.txt"
.DecryptFile()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End With
End Sub
Private Sub cmdTest2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest2.Click
Dim MyDes As New cDes()
Dim Demo As String = "Hello World"
'Original
MessageBox.Show("Original: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Encrypt string
Demo = MyDes.EncryptString(Demo, "alpha45")
MessageBox.Show("Encrypted: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Decrypt string
Demo = MyDes.DecryptString(Demo, "alpha45")
MessageBox.Show("Decrypted: " & Demo, "Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|