RSA(note)应用密码学第二次作业(9.25)

Basic steps of RSA algorithm

  1. The steps to generate the public key PK and private key SK are as

    follows:

  2. Randomly choose two large prime numbers P and Q. P is not equal to

    Q.

  3. Multiply the two prime numbers P and Q to get an N, that is, N=PQ

  4. Subtract one from P and Q respectively, and then multiply them

    together to get a number T, that is, T = (Q-1) * (P-1)

  5. Choose an integer E as a key so that E and T are relatively prime (

    the greatest common divisor of E and T is 1), and E must be less

    than T

  6. According to the formula DE mod T = 1, the value of D is calculated

    as another key

  7. Through the above steps, the three data N, E, and D can be found,

    where (N, E) is the public key and (N, D) is the private key.

Encrypt information with public key

Plain text: M

Encryption:

Cipher text: C

Decrypt message with private key

Cipher text: C

Decryption:

Plain text: M

In a public-key system using RSA, you intercept the cipher text C = 10 sent lo a user whose public key is e = 5,n = 35. What is the plain text M

answer:

We know that 35 is relatively small, and we can decompose the prime numbers into 5 and 7

Then we can get T=(5-1)X(7-1)=24,24 and 5 are relatively prime

We can use the formula DE mod T=1 to get the value of D which is the private key

We can know the private key from the picture (n,D)=(35,5)

Decrypt

M=5,so the plain text is 5

cpp 复制代码
#include<iostream>
int candp(int b,int p, int k) //b--明文或密文   p--指数(e/d)    k--模数
{
	if (p == 1)
	{
		return b % k;
	}
	if (p == 2)
	{
		return b * b % k;
	}
	if (p % 2 == 0)
	{
		int  sum = candp(b, p / 2, k);
		return sum * sum % k;
	}
	if (p % 2 == 1)
	{
		int sun = candp(b, p / 2, k);
		return sun * sun * b % k;
	}
}
int main()
{
	int d = 1;
	//求e的乘法逆
	int e = 5;
	int t = 24;
	while (((e * d) % t) != 1)
	{
		d++;
	}
	std::cout <<d<<std::endl;


	
		int  n=35, x=0, y=10;

		x = candp(y, d, n);
		std::cout << "明文为:" << x << std::endl;
		return 0;

}
相关推荐
ErizJ6 分钟前
Redis|学习笔记
redis·笔记·学习
加油201929 分钟前
方法论:如何系统性的学习?
学习·学习方法·方法论
小t说说1 小时前
科学素养培养:男孩女孩的不同“方程式”,真的有分性别学习平台?
学习
xian_wwq1 小时前
【学习笔记】变电保护、测控、安自、自动化系统概述
笔记·学习·保护
lizhihai_991 小时前
股市学习心得—商业航天10大核心材料供应商
大数据·人工智能·学习
泰勒朗斯1 小时前
rootflight学习笔记
笔记·学习
知识分享小能手2 小时前
R语言入门学习教程,从入门到精通,R语言时间序列数据可视化(11)
学习·信息可视化·r语言
AI绘画哇哒哒3 小时前
RAG 系统中文档切分策略:如何选择合适的 chunk size?| 收藏这份实用指南,小白也能轻松上手大模型学习
人工智能·学习·ai·程序员·大模型·产品经理·转行
@codercjw3 小时前
方案制作(CAD)
学习
python零基础入门小白3 小时前
从0到1:手把手教你用Coze打造AI Agent,小白也能转行AI!
人工智能·学习·程序员·大模型·agent·产品经理·ai大模型