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.
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: