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




Reply With Quote
