Search code examples
batch-fileencryptionvbscriptbackground-process

Detect keys in vbs/batch and encrypt them


Goal: Live encryption

Code: Vbs & batch (anything that windows can run without external programs).

Meaning: Detect keys and change them.

Example: You type: 'Hello world', then vbs replaces that with '-8208-6840-10032-10032-11400 -15048-11400-12768-10032-6384' (for example).

Problem: I do not want to use Code = InputBox ("Code:"), or in any way be in the program. The idea is the vbs file runs in the background.


Solution

  • I did some thinking, and I came up with this. Far from perfect ofcourse, but it kind of works. It encrypts by using a choice with all letters from the alfabet, and stops when you press 1.

    Pros:

    • Pure batch, can run on any windows computer
    • You can specify your own salt string
    • Doesn't show just typed character, safe for use in public
    • You can make the user set his own salt string with set /P, as well as the two 11s but you would have to make sure the user inputs are valid

    Cons:

    • Only accepts letters, doesn't accept spaces/numbers/special signs
    • Need salt string to decrypt

    Here is the code behind it:

    @echo off
    set saltString="ijklmaopqwxybcrstufghvdenz"
    set "encryptionString="
    :choiceLoop
    cls
    echo press 1 to stop
    choice /C %saltString%1 /N /M %encryptionString%-
    set keyCode=%errorlevel%
    if %keyCode% GEQ 27 GOTO breakLoop
    ::encryption logic here
    set /A keyCode=%keyCode%+11*11
    echo %keyCode%
    set encryptionString=%encryptionString%-%keyCode%
    goto choiceLoop
    :breakLoop
    set encryptionString=%encryptionString%-
    echo encrypted string: %encryptionString%
    pause
    

    Note that the salt string has to contain exactly 26 letters. You can change the encryption logic by changing the two 11s in the set /A command.

    This first creates a variable, encryptionString, and the saltString. It than uses the choice command with the saltstring+1 as /C parameter, which is the characters to choose from. It uses /M (message) to show you what you've typed so far.

    A thing to note about how you use the choice command is that it sets the errorlevel, to the position of the character you just typed in the /C parameter. This makes sure you can have assign differenct numbers to different characters, in this case i=1, j=2, k=3, l=4, m=5, a=6... (note that backspace and space, like for instance ., are invalid characters for the choice command)

    It than multiplies the value of the errorlevel (which is saved in keyCode) with 11, and adds 11. After that it just adds the value of keyCode to the encryptionString. If the choice command returns 27 as errorlevel (1 is pressed), the loop is broken and the trailing - is added to the encryptionString.