# Encrypt String With Key Script

L

#### LeQuestionnaire

##### Guest
I want to duplicate https://www.braingle.com/brainteasers/codes/keyword.php. I want to make an encryption script that encrypts a string of text using a key. I tried this but it doesn't work. I can't figure out why. Could someone help me?

What I want to do is say, if the key is "key", make the keyed alphabet by deleting all letters that exist in the key from the alphabet, like so:

abcdefghijklmnopqrstuvwxyz
becomes abcdfghijlmnopqrstuvwxz

Then appending the key to the beginning of the new (keyed) alphabet:

keyabcdfghijlmnopqrstuvwxz

Then lining up the original alphabet with the keyed alphabet:

abcdefghijklmnopqrstuvwxyz
keyabcdfghijlmnopqrstuvwxz

And replacing each letter a in the text with the first letter of the keyed alphabet, each letter b with the second letter of the keyed alphabet, and so on. But I'm having problems with the last step.

Code:
``````// string_key(text,key)
text = argument0; key = argument1;
alphabet = "abcdefghijklmnopqrstuvwxyz";
keyed_alphabet = "";

// delete all letters in key from alphabet
i = 1;
repeat string_length(key)
{
alphabet = string_replace_all(alphabet,string_char_at(key,i),"");
i += 1;
}

// append key to beginning of keyed alphabet
keyed_alphabet = key + alphabet;

// encrypt text using keyed alphabet
i = 1;
repeat string_length(text)
{
text = string_replace_all(text, string_char_at(text,i), string_char_at(keyed_alphabet,string_pos(string_char_at(text,i),alphabet)));
i += 1;
}

return text;``````

Last edited by a moderator:
L

#### LeQuestionnaire

##### Guest
Bump because I rewrote the thread so it's easier to understand what my goal is.

#### jaydee

##### Member
How I would do it is thus:

Before looping, create an empty string EncodedString.

In the loop where you are replacing the string with the keyed alphabet,
Step 1) Find out what character it is you need to replace. var ThisChar
Step 2) Identify the index of ThisChar in the original alphabet. var ThisCharIndex
Step 3) Find the corresponding character in the keyed alphabet, and append it to EncodedString.

So you'll get something roughly like this:
Code:
``````EncodedString = "";

for(i=1;i<string_length(text)+1;i++)
{
ThisChar = string_char_at(text, i); // Step 1
ThisCharIndex = string_pos(ThisChar, alphabet); // Step 2
EncodedString += string_char_at(keyed_alphabet, ThisCharIndex); // Step 3
}``````

L

#### LeQuestionnaire

##### Guest
How I would do it is thus:

Before looping, create an empty string EncodedString.

In the loop where you are replacing the string with the keyed alphabet,
Step 1) Find out what character it is you need to replace. var ThisChar
Step 2) Identify the index of ThisChar in the original alphabet. var ThisCharIndex
Step 3) Find the corresponding character in the keyed alphabet, and append it to EncodedString.

So you'll get something roughly like this:
Code:
``````EncodedString = "";

for(i=1;i<string_length(text)+1;i++)
{
ThisChar = string_char_at(text, i); // Step 1
ThisCharIndex = string_pos(ThisChar, alphabet); // Step 2
EncodedString += string_char_at(keyed_alphabet, ThisCharIndex); // Step 3
}``````
I tried your code but instead of using the key xyz to encrypt abcdefghijklmnopqrstuvwxyz to xyzabcdefghijklmnopqrstuvw like it should, it encrypts it to xyzabcdefghijklmnopqrstxxx. This is an improvement over my original code, which produced xxxabcdefghijklmnopqrstxxx, but still not functional.

#### chamaeleon

##### Member
Code:
``````var keystr = "zyxwvutsrqponmlkjihgfedcba"
var key;

for (var i = 0; i < string_length(keystr); i++)
{
key[ord("a")+i] = string_char_at(keystr, i+1);
}

var message= "encode this";
var encoded = "";

for (var i = 1; i <= string_length(message); i++)
{
var ch = string_char_at(message, i);
if (ch >= "a" && ch <= "z")
encoded += key[ord(string_char_at(message, i))];
else
encoded += ch;
}

show_debug_message(encoded);``````

L

#### LeQuestionnaire

##### Guest
Code:
``````var keystr = "zyxwvutsrqponmlkjihgfedcba"
var key;

for (var i = 0; i < string_length(keystr); i++)
{
key[ord("a")+i] = string_char_at(keystr, i+1);
}

var message= "encode this";
var encoded = "";

for (var i = 1; i <= string_length(message); i++)
{
var ch = string_char_at(message, i);
if (ch >= "a" && ch <= "z")
encoded += key[ord(string_char_at(message, i))];
else
encoded += ch;
}

show_debug_message(encoded);``````
Thank you very much! This works!

#### chamaeleon

##### Member
Thank you very much! This works!
You're welcome. Btw, when I add the encoded character to the encoded string, I didn't mean to use the string_char_at() function, I meant to use ch..