有人可以解释这段代码,发表评论吗? RSA密码。 [英] Can somebody explain this code, make comments? RSA cipher.

查看:80
本文介绍了有人可以解释这段代码,发表评论吗? RSA密码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码应使用RSA密码加密和解密文本。如果我在文本中有空格用于加密,那么它不会加密空格后的单词。它也没有显示e&的可能值。 d。



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.

#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");
}

推荐答案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一段解释!例如:

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();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分理由吗?



如果您在某处找到的代码没有做你想做的事,要么问你得到它的人,使用调试器找出原因,或者写自己的。我们不是来为互联网上的每一段代码提供随机技术支持。


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.


请参阅我对您对Solution 1的评论的评论。



Please see my comment to your comment to Solution 1.

morrismasonbonham问:
morris "mason" bonham asked:



也许有人做过任何熟悉的事情,可以分享...


Maybe someone have done anything familiar and can share it…

例如,看看这个实现: https://code.google.com/p/rsa [ ^ ]。



但是不要指望代码的解释。 OriginalGriff和我试图解释为什么它不够富有成效。请注意,即使您有实施的原始作者,要求此人解释通常会导致相同的情况:这个人会问:你到底究竟是什么?



但是如果你真的想要理解它,做两件事1)学习编程到给定软件所需的水平,2)尝试自己实现算法。非常典型的是,那些完全独立地,甚至不太成功地解决某些问题的人变得更有能力掌握其他人所写的想法。怎么做?学习算法本身。你可以从这里开始:

https://en.wikipedia.org/wiki/RSA_ %28cryptosystem%29 [ ^ ] ,

http://people.csail.mit.edu/rivest/Rsapaper.pdf [ ^ ]。



如果由于某些原因,您对公钥加密的整个概念及其用途不太自信,您可能需要了解它: https://en.wikipedia.org/wiki/Public-key_cryptography [ ^ ]。



< dd> -SA

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


错误的问题:这里没有可能的解决方案。



如果您需要有关此代码的说明,我推断它不是您的代码。为什么不问作者?



这行代码的解释更多的是关于语言的知识,永远不会帮助你理解RSA密码原理。 />
需要书籍才能解释RSA密码的概念。

维基百科的快速搜索肯定会带给你一些有用的东西,无论如何它都是巨大的。
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.


这篇关于有人可以解释这段代码,发表评论吗? RSA密码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆