String Encrypt and Decrypt

By | 2019-08-23

These two function provide a simple method to encrypt and decrypt a string. Simply drop this code in to a module, and it is ready for use.

Option Explicit

Public Function Encrypt(ByVal icText As String) As String
Dim icLen As Integer
Dim icNewText As String
icChar = ""
   icLen = Len(icText)
   For i = 1 To icLen
      icChar = Mid(icText, i, 1)
      Select Case Asc(icChar)
         Case 65 To 90
            icChar = Chr(Asc(icChar) + 127)
         Case 97 To 122
            icChar = Chr(Asc(icChar) + 121)
         Case 48 To 57
            icChar = Chr(Asc(icChar) + 196)
         Case 32
            icChar = Chr(32)
      End Select
      icNewText = icNewText + icChar
   Next
   Encrypt = icNewText
End Function

Public Function Decrypt(ByVal icText As String) As String
Dim icLen As Integer
Dim icNewText As String
icChar = ""
   icLen = Len(icText)
   For i = 1 To icLen
      icChar = Mid(icText, i, 1)
      Select Case Asc(icChar)
         Case 192 To 217
            icChar = Chr(Asc(icChar) - 127)
         Case 218 To 243
            icChar = Chr(Asc(icChar) - 121)
         Case 244 To 253
            icChar = Chr(Asc(icChar) - 196)
         Case 32
            icChar = Chr(32)
      End Select
      icNewText = icNewText + icChar
   Next
   Decrypt = icNewText
End Function

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.