当前位置:网站首页>RSA加密解密详解
RSA加密解密详解
2022-06-26 17:45:00 【lmn_】

0x01 RSA概要
RSA算法是非对称密码算法。非对称密码又称为公钥密码,意思为每对加密包含一个公钥(可能为他人所知)和一个私钥(可能不为所有人所知)。有效的安全需要保持私钥的私密性;公钥可以在不影响安全性的情况下公开分发。
RSA 的安全性依赖于分解两个大素数乘积的实际困难,但相对较慢,可以称为“分解问题”。
RSA应用
- 客户端将其公钥发送到服务器并请求一些数据
- 服务器使用客户端的公钥加密数据并发送加密数据
- 客户端接收此数据并对其进行解密
0x02 RSA算法详细流程
RSA算法四个步骤
- 密钥生成
- 密钥分发
- 加密
- 解密
关于RSA相关公式
n = p * q
ø(n) = (p - 1) * (q - 1)
ed ≡ 1 mod ø(n)
c = m**e mod n
m = c**d mod n
simple example
已知:P = 11,q = 29,e = 3
求d
通过公式:
ø(n) = (p - 1) * (q - 1)
φ(n) = (p-1)(q-1) = (11-1)*(29-1) = 280
通过公式:
ed ≡ 1 mod ø(n)“≡”是数论中表示同余的符号
如果两个整数 ed 和 1 满足 ed-1 能被 ø(n) 整除,称为整数 ed 与 1 对模 ø(n) 同余1 < d < ø(n)
3d ≡ 1 mod 280
3d mod 280 = 1
d = 187
通过公式:
n = p * q
n = 11 * 29 = 319
公钥(n,e)
私钥(n,d)
得到:
公钥(319,3)
私钥(319,187)

或者用脚本解d:
gmpy2.invert(e,(p-1)*(q-1))
使用此密钥对可以加密的最大数字是多少
加密函数为:
c(m) = m^e mod n解密函数为:
m(c) = c^d mod n
RSA算法一次能加密的名文长度与密钥长度成正比。
len_in_byte(raw_data) = len_in_bit(key)/8-11
如果小于这个长度,就需要进行数据补齐,称为padding,不进行数据补齐用户就无法确分解密后内容的真实长度。
n的长度就是密钥长度,n = 319,n的二进制为100111111,密钥为9位,RSA实际可加密的明文长度最大也是1024bits
当加密明文m = 23时,求密文c
加密函数为:
c(m) = m^e mod n

c(m) = 23^3 mod 319 = 12167 mod 319
c(m) = 45
import gmpy2
n = 319
e = 3
m = 23
print(pow(m, e, n))
当密文c = 23时,求明文m
解密函数为:
m(c) = c^d mod n
m(c) = 23^187 mod 319
m(c) = 199
import gmpy2
n = 319
d = 187
c = 23
print(pow(c,d,n))

边栏推荐
- 背包问题求方案数
- KDD 2022 | how to use comparative learning in cross domain recommendation?
- [C language] static modifies local variables
- Microservice architecture practice: user login and account switching design, order query design of the mall
- Niuke network: Design LRU cache structure design LFU cache structure
- Necessary decorator mode for 3 years' work
- How does Guosen Securities open an account? Is it safe to open a stock account through the link
- Some explanations for latex CJK
- [uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure
- Number of solutions for knapsack problem
猜你喜欢

二分查找法-1

10 cloud security best practices that enterprises need to know

wechat_微信小程序中解决navigator进行页面跳转并传递参数问题

玩轉Linux,輕松安裝配置MySQL

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!

Demonstrate to Xiaobai the case of sub database and sub table

Vue--vuerouter cache routing component

LeetCode——226. 翻转二叉树(BFS)

Microservice architecture practice: business management background and SSO design, SSO client design

【推荐系统学习】推荐系统的技术栈
随机推荐
9、智慧交通项目(2)
清华&商汤&上海AI&CUHK提出Siamese Image Modeling,兼具linear probing和密集预测性能!
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
Don't believe it, 98% of programmers are like this
在国金证券开户怎么样?开户安全吗?
Preparing for the Blue Bridge Cup and ccf-csp
你好,现在网上股票开户买股票安全吗?
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure
Viewing the task arrangement ability of monorepo tool from turborepo
Chen Qiang: Alibaba's 100 billion level large-scale digital business knowledge map helps business growth
KDD 2022 | how to use comparative learning in cross domain recommendation?
Concurrent thread safety
Getting started with mongodb
Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
Today, I met a "migrant worker" who took out 38K from Tencent, which let me see the ceiling of the foundation
股票开账户如何优惠开户?现在在线开户安全么?
合约量化系统开发方案详细,量化合约系统开发技术说明
Use middleware to record slow laravel requests
ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup