Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
This code should encrypt and decrypt text with RSA cipher. If I have spaces in text for encryption, then it doesn't encrypt words after space. Also it doesn't show possible values of e & d.

C#
#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char message[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();


int prime(long int pr)
{
	int i;
	int j = sqrt(pr);
	for (i = 2; i <= j; i++)
	{
		if (pr % i == 0)
			return 0;
	}
	return 1;
}

int main()
{
	cout << "Enter first number\n";
	cin >> p;
	flag = prime(p);
	if (flag == 0)
	{
		cout << "\nWrong Number\n";
		exit(1);
	}
	cout << "\nEnter second number\n";
	cin >> q;
	flag = prime(q);
	if (flag == 0 || p == q)
	{
		cout << "\nWrong number\n";
		exit(1);
	}
	cout << "\nEnter text\n";
	fflush(stdin);
	cin >> message;
	for (i = 0; message[i] != NULL; i++)
		m[i] = message[i];
	n = p * q;
	t = (p - 1) * (q - 1);
	ce();
	cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
	for (i = 0; i < j - 1; i++)
		cout << e[i] << "\t" << d[i] << "\n";
	encrypt();
	decrypt();
	system("pause");
	return 0;
}
void ce()
{
	int k;
	k = 0;
	for (i = 2; i < t; i++)
	{
		if (t % i == 0)
			continue;
		flag = prime(i);
		if (flag == 1 && i != p && i != q)
		{
			e[k] = i;
			flag = cd(e[k]);
			if (flag > 0)
			{
				d[k] = flag;
				k++;
			}
			if (k == 99)
				break;
		}
	}
}
long int cd(long int x)
{
	long int k = 1;
	while (1)
	{
		k = k + t;
		if (k % x == 0)
			return (k / x);
	}
}
void encrypt()
{
	long int pt, ct, key = e[0], k, length;
	i = 0;
	length = strlen(message);
	while (i != length)
	{
		pt = m[i];
		pt = pt - 96;
		k = 1;
		for (j = 0; j < key; j++)
		{
			k = k * pt;
			k = k % n;
		}
		temp[i] = k;
		ct = k + 96;
		en[i] = ct;
		i++;
	}
	en[i] = -1;
	cout << "\nTHE ENCRYPTED MESSAGE IS\n";
	for (i = 0; en[i] != -1; i++)
		printf("%c", en[i]);
	system("pause");
}
void decrypt()
{
	long int pt, ct, key = d[0], k;
	i = 0;
	while (en[i] != -1)
	{
		ct = temp[i];
		k = 1;
		for (j = 0; j < key; j++)
		{
			k = k * ct;
			k = k % n;
		}
		pt = k + 96;
		m[i] = pt;
		i++;
	}
	m[i] = -1;
	cout << "\nTHE DECRYPTED MESSAGE IS\n";
	for (i = 0; m[i] != -1; i++)
		printf("%c", m[i]);
	system("pause");
}
Posted

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

If the code you found somewhere doesn't do what you want, either ask the person you got it from, use the debugger to find out why, or write your own. We aren't here to provide random technical support fro every bit of code on the internet.
   
Comments
morris “mason” bonham 10-Dec-15 14:47pm
   
I don't need that detailed explanation. I want to know what functions do (really short explanation).
Sergey Alexandrovich Kryukov 10-Dec-15 16:22pm
   
This is a loos/loos situation, just face it. If you want detailed explanation, it would be too long to be useful. If you "don't need that detailed explanation", it won't explain anything. The key is: the whole idea to explain some code is wrong; a person trying to answer won't know what exactly you are confused with. (So, at best, "explanations" can be given only in a close dialog.) All experience of many people confirms it. This is just not a way to find out how things work. Basically, you need to write your own code using the documentation and ask your questions in case if you face some problems. Or ask conceptual questions. And so on...
—SA
morris “mason” bonham 10-Dec-15 14:49pm
   
Maybe someone have done anything familiar and can share it...
Sergey Alexandrovich Kryukov 10-Dec-15 16:33pm
   
I answered to this in Solution 2.
—SA
Please see my comment to your comment to Solution 1.

morris “mason” bonham asked:

Maybe someone have done anything familiar and can share it…
Look, for example, at this implementation: https://code.google.com/p/rsa[^].

But don't expect explanation of the code. OriginalGriff and I tried to explain why it cannot be productive enough. Note that even you had the original author of the implementation, asking this person for "explanation" would typically lead to the same situation: this person would ask: "what exactly you don't understand?"

But if you really want to understand it, do two things 1) learn programming to the level required by the given piece of software, 2) try to implement the algorithm all by yourself. It's very typical that people who decently tried to solve some problem fully independently, even not quite successfully, becomes much more capable of grasping the idea of what other people wrote. How to do it? Learn the algorithm itself. You can start here:
https://en.wikipedia.org/wiki/RSA_%28cryptosystem%29[^],
http://people.csail.mit.edu/rivest/Rsapaper.pdf[^].

If, by some reasons, you are not quite confident about the whole concept of public-key cryptography and its uses, you may need to learn it: https://en.wikipedia.org/wiki/Public-key_cryptography[^].

—SA
   
v3
Wrong question: so no possible solution here.

If you need explanation about this code, I deduce that it is not your code. Why not ask the author ?

Explanation of this code line by line is more about knowledge of the language and will never help you to understand the RSA cipher principle.
It takes books to explain the RSA cipher concepts.
A quick search in Wikipedia will certainly lead you to something useful, in any case it is huge.
   
Does this line...

cin >> message;


...really do what you think it does?

If you can work that one out then that's the root cause of the whole message not being encrypted.

As for the other error perhaps have a dig through the code for ce() and cd() and work out what they're supposed to be doing and why?

While you're at it you might like to consider the huge pile of global variables and why you're not passing them to functions as parameters to enable you to write some isolated tests for them and get to the bottom of what's wrong far more quickly. Also if you're programming in C++ at least try and use C++ idioms rather than some horrible mish-mash of C and C++, it'll make your code far easier to read and debug.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900