Cogwheel Encryption Method

jhai_salvador 1 Tallied Votes 487 Views Share

This is my Cogwheel Encryption Method to encrypt and decrypt strings.

Wheel1 is Replace by Wheel2 when Encrypting and Wheel2 is Replace by wheel1 when you are decrypting...

To Encrypt a string, just call the EncryptString function

Text2.Text = EncryptString(Text1.Text)

To Decrypt a string, just call the DecryptString function

Text2.Text = DecryptString(Text1.Text)

This is my First Code Snippet so hope you like it.

AndreRet commented: Nice for a first time post Jhai. +3
Option Explicit

'Valid Characters for Password
Const Wheel1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+=-{}[]:;<>,.?/"
'Replacemet For Wheel1 (Must be same with Wheel1 but make it Random)
Const Wheel2 = "89afi1jg2hbekAwxKBCFlmV=-cZrs3X#GY4.?/5tuOJ%^vyLnWzM{NP}[QR$EIoSpq;<>,T0*()67HUdD&!@_+]:"

Public Function EncryptString(xStr As String) As String
Dim xStrHolder As String
Dim xStrIndex As Integer
Dim i As Integer
For i = 1 To Len(xStr)
   xStrIndex = InStr(Wheel1, Mid(xStr, i, 1))
   If xStrIndex = 0 Then
      MsgBox "Invalid use of character!...", vbExclamation, "Invalid Character"
      Exit Function
   Else
      xStrHolder = xStrHolder & Mid(Wheel2, xStrIndex, 1)
   End If
Next i
EncryptString = xStrHolder
End Function

Public Function DecryptString(xStr As String) As String
Dim xStrHolder As String
Dim xStrIndex As Integer
Dim i As Integer
For i = 1 To Len(xStr)
   xStrIndex = InStr(Wheel2, Mid(xStr, i, 1))
   If xStrIndex = 0 Then
      MsgBox "Invalid use of character!...", vbExclamation, "Invalid Character"
      Exit Function
   Else
      xStrHolder = xStrHolder & Mid(Wheel1, xStrIndex, 1)
   End If
Next i
DecryptString = xStrHolder
End Function
AndreRet 526 Senior Poster

Nice way of manipulating strings. There are some easier ways to encrypt and decrypt though. Nice code as well.

jhai_salvador 48 Junior Poster

Nice way of manipulating strings. There are some easier ways to encrypt and decrypt though. Nice code as well.

:) Thanks. Hope more will learn from it (Manipulating strings)..

AndreRet 526 Senior Poster

Only a pleasure. I'm sure they will learn from this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.