Vigenere Cipher Trouble

Hi all,

I coded a working Vigenere Cipher today. All seems to be in working order.

A feature that I'm looking to implement though, is that if a space (or any punctuation) was added to the plaintext, for it to be ignored completely.

It seems that my key will lose a letter for these characters, and therefore, the original message cannot be decrypted.

EDIT: My problem is around the else statements, within the encrypt and decrypt functions.

My code is:

REMOVED

What I'm asking, is would someone be able to help me with a line to have the key skip the punctuation or space, and continue as normal?

Cheers,
-Matt
Last edited on
Your assignment on line 86 does nothing. You probably want to append the current plaintext character to the end of the ciphertext string.
Thanks for that, I was still tinkering with the code when I posted.

Ok, I did a quick search for appending to end of strings there, although I'm not quite sure on how to take a character and move it.

Could you point me in the right direction?
You are doing appending on lines 78 and 82 right above it. I imagine you'd want something like ciphertext += plain[i];

What do you mean by "move" a character?
I tried the ciphertext += plain[i] that you suggested. It just adds the plaintext to the ciphertext.

I'd assumed you meant take the space, and append it to the end of the string. Or "moving" in my layman's terms. :P

My problem lies in the key I believe. It skips a letter when it encounters a space or special character, which in turn messes up decryption.

I know it'll probably turn out to be some trivial line of code, I just can't wrap my mind around it..

Thanks for your reply btw.
I tried the ciphertext += plain[i] that you suggested. It just adds the plaintext to the ciphertext.


Isn't that what you want? To add the plaintext unencrypted to the ciphertext when it is not an alphabetical character?

My problem lies in the key I believe. It skips a letter when it encounters a space or special character, which in turn messes up decryption.


Wait, so you don't want to advance the key position when skipping a plaintext letter, then? That makes things more difficult, as you have only one number indicating where you are in both arrays... You could add an extra variable inside the loop that only increments when you actually use the key value.
I'll give an example, because I'm confusing myself at the minute.

Using an example taken off Wikipedia:

1
2
3
Plaintext: ATTACKATDAWN
Key: LEMON
Ciphertext: LXFOPVEFRNHR 


This works, however, if a space is included in the plaintext, I get:

1
2
3
Plaintext: ATTACK AT DAWN
Key: LEMON
Ciphertext: LXFOPVMHOEIB 


When this second ciphertext is decrypted, the original plaintext is scrambled.

The following is happening:

1
2
3
4
5
A T T A C K A T D A W N
L E M O N L E M O N L E

A T T A C K   A T   D A W N
L E M O N L E M O N L E M O


We can see from the second example, that the "E" in the key, is assigned to the space, instead of the "A" in the first example.

I was thinking something like:

key[i] = key[i - 1]

although this code doesn't work for me.

Any ideas?
Last edited on
I`m just a newbie but have you thought if it is posible to assign a space character to the space character ?
something like this ?

1
2
3
4
5
A T T A C K A T D A W N
L E M O N L E M O N L E

A T T A C K   A T   D A W N
L E M O N L   E M   O N L E 

Maybe the code should count the characters till it finds a space char and put it to the "lemon" cypher ?
Some kind of splitting the key word into multiple words and repeat the coding but going on from the letter of the cypher the last word stoped ?
Last edited on
Topic archived. No new replies allowed.