C/C++, Codes

Runningkey Cipher

A Running key Cipher is a technique that generate cipher text by adding key to plaintext in which alphabetic text(key and plaintext) is mapped to number starting from 0 such as a- ‘0’, b -‘1’, etc. If the key.length is smaller than plaintext.length, then adding plaintext character to key until they are the same length, then do the generator.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	//initialize file
	string key = "";
	string plaintext = "";
	string ciphertext = "";
	ifstream in;
	if (!in)
		cout << "No file exist!!!";
        in.open("E:\\DATA\\Source Code\\input.txt");

	//read character in
	char c = 1;
	while (!in.eof())
	{
		in >> key >> plaintext;
	}

	in.close();
	int i;
	int k = plaintext.length();
	int l = key.length();
	for (i = 0; i < (k - l);i++)
		key += plaintext[i];

	cout << key << endl;
	cout << plaintext << endl;
	//cipher text
	i = 0;
	while (i < key.length()){
		ciphertext += (((char)toupper(key[i]))
                   + ((char)toupper(plaintext[i]))) % 26 + 65;
		i++;
	}
	cout << ciphertext;
	return 0;

}

Sample run :

In in.txt :
deceptive
wearediscoveredsaveyourself

Program run :
deceptivewearediscoveredsav <——————– new key
wearediscoveredsaveyourself <——————– plaintext
ZICVTWQNGKZEIIGASXSTSLVVWLA <—– ciphertext

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s