### Vigenere Cipher

Can anyone help me with building a code for VigenĂ¨re cipher ? I am ahving trouble understanding the whole concept

Thanks!
Wikipedia's algorithm is this:
http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher#Algebraic_description

Encryption:
Ci = (Mi + Ki) modulo 26

Decryption:
Mi = (Ci - Ki) modulo 26

Where:
M is the Message array.
C is the Ciphertext array and
K is the Key array.
i is the current index of the arrays.

Firstly, be advised the cipher, in this form, only encrypts letters. No punctuation marks or digits.

Secondly, you have to represent your letters as numbers:
A = 0, B = 1, C = 2, ..., Y = 24, Z = 25

Thirdly, your Key array must be the exact same size as the Message array. This means trimming it down if it's longer, or duplicating it if it's shorter.

The modulo operator in C++ is %.

Here's a sample implementation of the encryption and decryption functions.
It's untested, and it's still up to you to convert the letters to numbers, store them, and making sure the `key` and `message` arrays are the same size.

(I used std::vector instead of plain arrays, because this is C++, not C.)

 ``12345678910111213141516171819202122232425262728`` ``````#include #include std::vector encrypt(const std::vector &message, const std::vector &key) { if (message.size() != key.size()) throw std::length_error("message and key aren't the same size"); std::vector ciphertext; for (std::vector::size_type i = 0; i < message.size(); ++i) ciphertext.push_back((message[i] + key[i]) % 26); return ciphertext; } std::vector decrypt(const std::vector &ciphertext, const std::vector &key) { if (ciphertext.size() != key.size()) throw std::length_error("ciphertext and key aren't the same size"); std::vector message; for (std::vector::size_type i = 0; i < ciphertext.size(); ++i) message.push_back((ciphertext[i] - key[i]) % 26); return message; }``````

Edit: hmm. Maybe you should use int instead of unsigned int.
Last edited on
Thanks for explaining in detail :D .

Firstly , I have to use C .. I'm not too sure I can understand d code u posted.

Secondly, On the three things u highlighted...

1) I got rid of d special characters.

2) How can I represent A as 0 till Z as 25? since, 0 to 25 ASCII values stand for something else.

3) How do I make the key_array size equal the message_array size?

For eg : if my key is : key
if my message is : message

then I would want
key array to be: "keykeyke" mapping from
"message"

How do I do that?

Thank you very much!
 2) How can I represent A as 0 till Z as 25? since, 0 to 25 ASCII values stand for something else.

You write a nasty function which transforms the letters. I'm feeling dumber than usual, today, so you (or other people on this forum) can probably find a smarter way.

 ``12345678910111213`` ``````#include int nasty(char c) { c = std::toupper(c); switch (c) { case 'A': return 0; case 'B': return 1; // ... } }``````

 3) How do I make the key_array size equal the message_array size? For eg : if my key is : key if my message is : message then I would want key array to be: "keykeyke" mapping from "message"

"keykeyk", but you got the right idea. You can use a for() loop for this.
 ``1234`` ``````std::vector expandedKey(message.size()); for (std::vector::size_type i=0; i < expandedKey.size(); ++i) expandedKey[i] = key[i % key.size()]; // this will blow up if key.size() is zero ``````

Thanks!
But sorry I do not understand "std::vector", expanded key etc u wrote :|
std::vector is a container from the C++ library.
http://cplusplus.com/reference/vector/vector/

It's better than plain old arrays because: it knows its own size, and can be resized easily.

Usage comparison:
 ``1234567891011`` ``````float arrayOfFloats[20]; std::vector vectorOfFloats(20); arrayOfFloats[5] = 0.1f; vectorOfFloats[5] = 0.1f; // easy copy std::vector secondVectorOfFloats = vectorOfFloats; // easy resize secondVectorOfFloats.resize(100);``````

In the encrypt() and decrypt() functions, I'm passing the vectors by constant reference. This saves memory and time. But if it's clearer to you, you can use this form instead:
 ``123456789`` ``````std::vector encrypt(std::vector message, std::vector key) { // ... } std::vector decrypt(std::vector ciphertext, std::vector key) { // ... }``````

But I am not allowed to use these for my coursework..thanks though! :)

If you could help me come up wid a code using dynamic memory allocation..dat'll be more than awesome!

 If you could help me come up wid a code using dynamic memory allocation..dat'll be more than awesome!

Dynamically allocated arrays are an old-fashioned pain in the ass (which is why std::vector exists in the first place). But I won't waste more time, when I see you put in no effort yourself.
You havnt seen my logic at all..so u can't really comment on my efforts.

Thanks for the code. But it is of no use to me as my coursework doesnt allow it.

Instead of spoon feeding me wid d code..i was just asking u to help me come up wid d logic.

This way I wont be able to learn.

and just because you don't want to use dynamic allocation u rubbish my question altogether. that's strange!

The code u posted firstly is available online.. it's d first thing when we google.
The second one i don't understand.

Isn't this forum to help learn the beginners?
Topic archived. No new replies allowed.