top of page

Paired Cipher Decryption and Encryption using C++

  • Writer: LA Nuñez
    LA Nuñez
  • Feb 28, 2022
  • 2 min read

In cryptography, encryption is the process of transforming information using an algorithm

called cipher to make it unreadable to anyone except those possessing special knowledge, usually referred to as a key. Let us implement an encryption program using paired cipher algorithm. A paired cipher algorithm uses two letters encrypted together. In the following formulae P1 and P2 are the numeric values of a pair of letters, while C1 and C2 represent the encrypted letters.


C1= (1xP1 + 3xP2) mod 26 + 1

C2= (2xP1 + 7xP2) mod 26 + 1


So the first letter A becomes, C1= (1x1 + 3x2) mod 26 +1 = 8 which is letter H.

And the second letter B is encrypted as, C2= (2x1 + 7x2) mod 26 + 1 = 17 which is letter Q.

Thus the two letters AB are encrypted as HQ.

If the original letters were YZ, then


C1 = (1x25 + 3x26) mod 26 + 1 = 26 => Z and

C2 = (2x25 + 7x26) mod 26 + 1 = 25 => Y


Thus YZ is encrypted as ZY.

Where two letters span a space, then the two letters should be converted and output with the space output in the same position as in the input.

This method only works in an even number of alphabetic characters.



A paired cipher algorithm uses two letters encrypted together. Nonetheless, it wouldn’t encrypt a message with odd number in alphabet length.


Source Code

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int convertChrToInt(char input)
{
	int output = 0;

	output = (int)input - 64;

	return output;
}

char convertIntToChr(int input)
{
	int output = 0;

	output = input + 64;

	return (char)output;
}

int main()
{
	char orig_msg[100];
	string encrypt_msg = "";
	char C1, C2;
	int P1, P2;
	int index = 0;
	int msg_len = 0;
	int add_space = 0;

	cout << "Original Message : ";
	cin.getline(orig_msg, 100);
	msg_len = strlen(orig_msg);

	while (index < msg_len)
	{

		if (index == (msg_len - 1))
		{

			cout << "Number of alphabetic characters is odd.\n\n";
			system("pause");
			return 0;

		}
			

		if (orig_msg[index] == ' ')
		{
			encrypt_msg += orig_msg[index];
			index++;
		}

		P1 = convertChrToInt(orig_msg[index]);
		index++;

		if (orig_msg[index] == ' ')
		{
			add_space = 1;
			index++;
		}
		P2 = convertChrToInt(orig_msg[index]);
		index++;

		C1 = convertIntToChr((1 * P1 + 3 * P2) % 26 + 1);
		C2 = convertIntToChr((2 * P1 + 7 * P2) % 26 + 1);

		encrypt_msg += C1;

		if (add_space == 1)
		{
			encrypt_msg += ' ';
			add_space = 0;
		}
		encrypt_msg += C2;
	}
	

	cout << "Encrypted Message: " << encrypt_msg << endl;
	cout << "Number of alphabetic characters is even.\n\n";
	system("pause");
}


Authors

LA Nuñez, Norleen Joyce Lloren, Thea Clarisse Navarro, Alliah Tenedero

Comments


© 2023 by LA Nuñez

​エルエイ・ヌネズ

  • LinkedIn
  • YouTube
  • Twitter
  • Instagram
  • kisspng-social-media-github-computer-icons-logo-github-5ac01880ad2ac8_edited_edited_edited
bottom of page