Tuesday, March 9, 2010

Simple encryption algorithm

Function RotateLeft(TheString As String, ByVal nbrPlaces As Byte) As String
' this is a simple encryption algorithm.
' knowing all 'printable' characters are 8 bytes long,
' you can shift the bits to the left and still have a printable character
' to decrypt, all you have to do is to shift them left
' until you have come full circle.  ie.
' to Encrypt:  RotateLeft(TheString, nbrPlaces)
' to Decrypt:  RotateLeft(TheString, 8 - nbrPlaces)

Dim tmp As Integer, i As Integer
Dim mult As Integer, ln As Integer
Dim tmpSt As String

   ln = Len(TheString)
   tmpSt = ""
   nbrPlaces = nbrPlaces Mod 8   ' no point doing more than 7, besides
   mult = 2 ^ nbrPlaces          ' mult (an Integer) would be too small
   For i = 1 To ln
      tmp = Asc(Mid$(TheString, i, 1))  ' get ASCII value of each character
      tmp = tmp * mult                  ' apply the multiplier
      tmp = tmp Mod 256 + tmp \ 256     ' rotate any "carry" bit
      tmpSt = tmpSt & Chr$(tmp)         ' add the character to the string
   Next i
   RotateLeft = tmpSt
End Function

No comments:

Post a Comment

Search This Blog