This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleEncryption.bas
executable file
·51 lines (41 loc) · 2.45 KB
/
ModuleEncryption.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Attribute VB_Name = "ModuleEncryption"
Option Explicit
'Declare constants
' KEY is the encryption key that gets XORed with each character of the provided string
' IV is the initilization vector for the CBC aspect of the encryption. Since a given character needs to be XORed with the key and the previous ciphertext, the IV is used for the first character, because there is no previous ciphertext.
Const KEY As Byte = 165, IV As Byte = 181
'Encrypts or decrypts a string by XORing each character with a key and using the cipher-block chaining (CBC) mode of operation (each block of plaintext is XORed with the previous ciphertext)
' StartString is the provided string that is to be encrypted or decrypted
' Encryption determines if the data is to be encrypted (True) or decrypted (False)
Function EncryptDecrypt(StartString As String, Encryption As Boolean) As String
'Declare variables
' count is the loop counter for each character in the given string
' CurrChar stores the character of the given string in each instance of the loop
' EndString stores the characters after they have been encrypted/decrypted
Dim count As Integer, CurrChar As String, EndString As String
'Loops to encrypt/decrypt each character in the given string
For count = 1 To Len(StartString)
'Gets the character to be encrypted/decrypted
CurrChar = Mid(StartString, count, 1)
'The character is XORed with the key
CurrChar = Chr(Asc(CurrChar) Xor KEY)
'Cipher-block chaining:
'If it is the first character, the initilization vector is used instead of the previous ciphertext
If count = 1 Then
CurrChar = Chr(Asc(CurrChar) Xor IV)
'If it is anywhere else in the string it is XORed with the previous ciphertext block
Else
'If the string is being encrypted, the ciphertext is in EndString
If Encryption Then
CurrChar = Chr(Asc(CurrChar) Xor Asc(Mid(EndString, count - 1, 1)))
'If the string is being decrypted, the ciphertext is in StartString
Else
CurrChar = Chr(Asc(CurrChar) Xor Asc(Mid(StartString, count - 1, 1)))
End If
End If
'Stores the result
EndString = EndString & CurrChar
Next count
'Returns the resulting string
EncryptDecrypt = EndString
End Function