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;

}
相关推荐
AI周红伟4 小时前
周红伟:DeepSeek官方教您如何部署Hermes Agent 和接入 DeepSeek-V4-Pro
人工智能·深度学习·学习·机器学习·copilot·openclaw
GISer_Jing4 小时前
AI原生全栈架构理论体系:从分布式范式演进到全链路工程化理论基石
前端·人工智能·学习·ai编程
babe小鑫4 小时前
零经验转行学习数据分析的价值分析
学习·数据挖掘·数据分析
zhangrelay5 小时前
三分钟云课实践速通--单片机原理与应用--Arduino--SimulIDE--
linux·单片机·嵌入式硬件·学习·ubuntu
格林威5 小时前
工业视觉检测:单样本学习 vs 传统监督学习
人工智能·深度学习·数码相机·学习·计算机视觉·视觉检测·工业相机
vooy pktc5 小时前
Spring Security 官网文档学习
java·学习·spring
TechMix5 小时前
【fkw学习笔记】Android 13 AOSP 源码添加系统预置应用实战指南
android·笔记·学习
承渊政道5 小时前
【动态规划算法】(两个数组的DP问题深度剖析与求解方法)
数据结构·c++·学习·算法·leetcode·动态规划·哈希算法
bendandawugui5 小时前
PCIe协议学习-浅谈SR-IOV
学习
辞旧 lekkk6 小时前
【Qt】初识(上)
开发语言·数据库·qt·学习·萌新