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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdexcept>
#include <vector>

std::vector<unsigned int> encrypt(const std::vector<unsigned int> &message, const std::vector<unsigned int> &key)
{
    if (message.size() != key.size())
        throw std::length_error("message and key aren't the same size");

    std::vector<unsigned int> ciphertext;

    for (std::vector<unsigned int>::size_type i = 0; i < message.size(); ++i)
        ciphertext.push_back((message[i] + key[i]) % 26);

    return ciphertext;
}

std::vector<unsigned int> decrypt(const std::vector<unsigned int> &ciphertext, const std::vector<unsigned int> &key)
{
    if (ciphertext.size() != key.size())
        throw std::length_error("ciphertext and key aren't the same size");

    std::vector<unsigned int> message;

    for (std::vector<unsigned int>::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.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cctype>

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.
1
2
3
4
std::vector<int> expandedKey(message.size());

for (std::vector<int>::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:
1
2
3
4
5
6
7
8
9
10
11
float arrayOfFloats[20];
std::vector<float> vectorOfFloats(20);

arrayOfFloats[5] = 0.1f;
vectorOfFloats[5] = 0.1f;

// easy copy
std::vector<float> 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:
1
2
3
4
5
6
7
8
9
std::vector<int> encrypt(std::vector<int> message, std::vector<int> key)
{
    // ...
}

std::vector<int> decrypt(std::vector<int> ciphertext, std::vector<int> 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!

But thanks for your help!
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.