Skip to content

Instantly share code, notes, and snippets.

@jamcut
Created January 26, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamcut/df0e5b59e2f0c86781a738b7501c1625 to your computer and use it in GitHub Desktop.
Save jamcut/df0e5b59e2f0c86781a738b7501c1625 to your computer and use it in GitHub Desktop.
Dim strCurrent As String 'Store the current string, as the textboxes can't be trusted not to truncate
Private Sub cmdEncrypt_Click()
Dim i As Integer 'Loop counter
Dim intKeyChar As Integer 'Character within the key that we'll use to encrypt
Dim strTemp As String 'Store the encrypted string as it grows
Dim strText As String 'The initial text to be encrypted
Dim strKey As String 'The encryption key
Dim strChar1 As String * 1 'The first character to XOR
Dim strChar2 As String * 1 'The second character to XOR
'Get the text and key from the textboxes
If strCurrent = "" Then
strText = txtText.Text
Else
strText = strCurrent
End If
strKey = txtKey.Text
'Loop through each character in the text
For i = 1 To Len(strText)
'Get the next character from the text
strChar1 = Mid(strText, i, 1)
'Find the current "frame" within the key
intKeyChar = ((i - 1) Mod Len(strKey)) + 1
'Get the next character from the key
strChar2 = Mid(strKey, intKeyChar, 1)
'Convert the charaters to ASCII, XOR them, and convert to a character again
strTemp = strTemp & Chr(Asc(strChar1) Xor Asc(strChar2))
Next i
'Display the resultant encrypted string
strCurrent = strTemp
txtText.Text = strCurrent
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment