I'm looking to make a program to encrypt a string using a vigenere cipher. So far, I have been successful in doing this, apart from special characters (e.g. spaces, full stops, commas, etc).

I have come to this solution, which includes the correct special characters. However, everything after the first special character in the string becomes gibberish. They are not special characters, they are still in the alphabet, although they don't match with the cipher. I cannot work out why this happening. I've tried several totally different methods, and all of them lead to this same error. This is the neatest method I've come up with so far, but it still doesn't work (for this example you can assume that the text and the key are the same length).

I wonder if it would be easier to write your cypher code so that it just maps special characters to themselves.
– Mark MeyerAug 20 '18 at 2:06

I don't see any way to remember where the special characters should be after encrypting/encoding short of storing the original string in its entirety. And maybe this would defeat the purpose of what you are trying to do here.
– Tim BiegeleisenAug 20 '18 at 2:07

@Bergi I gave that a shot, and it significantly decreased the length of my code, so thank you. Unfortunately, it did not solve my problem; I still get the exact same error. I updated my question above to incorporate this method. If you could take a look that would be excellent.
– InteltaAug 20 '18 at 10:41

2 Answers
2

For anyone wondering, I fixed it. I noticed that after every special character, they key would shift back one letter. For example, if the key was apples, then after the first special character, the key would become pplesa. After the second special character, the key would become plesap. To counter this, I just added p -= 1; at the end of the if statement for special characters. This fixed the problem. Thank you to everyone who helped.

You can check the character type using ASCII value. Since you specified any non-lowercase letter, you can mark a character special if its ASCII value is not in the range 97-122.

You can store the special characters from your original string in some sort of hashmap. You can make the character the key, and the value being a linked list for each character. The linked list can store the indices of the characters, so you know where they were in the original string.

How would I go about making the hashmap, and more specifically how would I reenter the missing characters? I've already converted them to ASCII.
– InteltaAug 20 '18 at 6:52

Why would you store the indices by character? This makes no sense.
– BergiAug 20 '18 at 7:23

@Bergi Because then for each key (a special character the OP detected in his original string), you know what index that character appeared in the original string, for all instances of that character in that string.
– Mr.MipsAug 20 '18 at 21:53

@Intelta You can use the dictionary in Javascript. You can iterate over the keys in the dictionary and reinsert whatever new characters you want to the indices where each special character occurred in your original string.
– Mr.MipsAug 20 '18 at 21:57