ScripticXLearn programming visually
Platform
Problems
Community
Leaderboard
Learn
Docs
BasicsVariablesLoopsInput / Output
Examples
BasicsLoopsConditionsAlgorithms
What's newHelpContact
Loading…

Examples

MiniScript+

ExamplesMiniScript+
Fundamentals
Basic programsConditions
Programming patterns
LoopsAlgorithmsEncode text with a key
Examples/Encode text with a key

Encode text with a key

Read text and a key, then turn each character into a number.

On this page
The ruleComplete programRecover one character

The program reads the text and key from the user. For each text character, multiply its Unicode code by the code of the matching key character, then add 1.

The rule#

TXTPlain text
y[i] = ORD(CHAR(x, i)) * ORD(CHAR(key, i MOD LEN(key))) + 1

If the text is longer than the key, start the key again from the beginning. The expression i MOD LEN(key) chooses the key position for any key length. Spaces and accented letters are processed just like other characters.

For example, with the text salut and the key abc, the first calculation is ORD("s") * ORD("a") + 1, or 115 * 97 + 1 = 11156.

Complete program#

The function takes both the text and key as parameters. The key must have at least one character and must not contain the character with code 0; the program checks both conditions before calculating.

The result is one string of numbers separated by spaces. The separator lets us identify each number when decoding.

MS+encode.mspMiniScript+
1FUNCTION encrypt(x, key)2  IF LEN(key) == 0 THEN3    RETURN "The key cannot be empty."4  END5  FOR j FROM 0 TO LEN(key) - 16    IF ORD(CHAR(key, j)) == 0 THEN7      RETURN "The key cannot contain the character with code 0."8    END9  END10  y = ""11  FOR i FROM 0 TO LEN(x) - 112    n = ORD(CHAR(x, i)) * ORD(CHAR(key, i MOD LEN(key))) + 113    IF i > 0 THEN14      y = y + " "15    END16    y = y + STR(n)17  END18  RETURN y19END20​21INPUT x22INPUT key23PRINT encrypt(x, key)

At the first input, enter the text salut. At the second, enter the key abc. The program prints:

TXTPlain text
11156 9507 10693 11350 11369

You can change the text and key on every run. With a valid key, empty text produces an empty string. If the text or key contains only digits and should stay a string, enter the value in quotes in the editor console, such as "00123".

Recover one character#

Subtract 1 from the number, divide by the key character code, and convert the result with CHR. This program recovers the first character in the example:

MS+decode-one-character.mspMiniScript+
1number = 111562key = "abc"3position = 04keyCode = ORD(CHAR(key, position MOD LEN(key)))5PRINT CHR((number - 1) / keyCode)

The result is s. To recover the whole text, repeat the operation at each position with the same key. For numbers from another source, also check divisibility and whether the resulting Unicode code is valid.

This exercise demonstrates a reversible text transformation. Do not use it to protect real passwords.

See Strings for the functions used here.

PreviousAlgorithms

On this page

The ruleComplete programRecover one character