当前位置:网站首页>RSA encryption and decryption details
RSA encryption and decryption details
2022-06-26 17:51:00 【lmn_】

0x01 RSA Summary
RSA The algorithm is an asymmetric cryptographic algorithm . Asymmetric cryptography is also called public key cryptography , It means that each pair of encryption contains a public key ( May be known to others ) And a private key ( May not be known to everyone ). Effective security requires maintaining the privacy of the private key ; Public keys can be publicly distributed without compromising security .
RSA The security of depends on the practical difficulty of decomposing the product of two large primes , But relatively slow , Can be called “ Break down the problem ”.
RSA application
- The client sends its public key to the server and requests some data
- The server encrypts the data with the public key of the client and sends the encrypted data
- The client receives this data and decrypts it
0x02 RSA Detailed algorithm flow
RSA Four steps of algorithm
- Key generation
- Key distribution
- encryption
- Decrypt
About RSA The relevant formula
n = p * q
ø(n) = (p - 1) * (q - 1)
ed ≡ 1 mod ø(n)
c = m**e mod n
m = c**d mod n
simple example
It is known that :P = 11,q = 29,e = 3
seek d
Through the formula :
ø(n) = (p - 1) * (q - 1)
φ(n) = (p-1)(q-1) = (11-1)*(29-1) = 280
Through the formula :
ed ≡ 1 mod ø(n)“≡” It's the sign of congruence in number theory
If two integers ed and 1 Satisfy ed-1 Can be ø(n) to be divisible by , It's called an integer ed And 1 To mold ø(n) congruence1 < d < ø(n)
3d ≡ 1 mod 280
3d mod 280 = 1
d = 187
Through the formula :
n = p * q
n = 11 * 29 = 319
Public key (n,e)
Private key (n,d)
obtain :
Public key (319,3)
Private key (319,187)

Or use a script to solve d:
gmpy2.invert(e,(p-1)*(q-1))
What is the maximum number that can be encrypted with this key pair
The encryption function is :
c(m) = m^e mod nThe decryption function is :
m(c) = c^d mod n
RSA The length of the name text that the algorithm can encrypt at one time is proportional to the length of the key .
len_in_byte(raw_data) = len_in_bit(key)/8-11
If less than this length , You need to supplement the data , be called padding, Without data complement, users cannot determine the true length of the decrypted content .
n The length of the key is the length of the key ,n = 319,n The binary of is 100111111, The key is 9 position ,RSA The maximum length of the actual encrypted plaintext is also 1024bits
When encrypting plaintext m = 23 when , Seeking ciphertext c
The encryption function is :
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))
When ciphertext c = 23 when , Ask for clear text m
The decryption function is :
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))

边栏推荐
- Jouer avec Linux et installer et configurer MySQL facilement
- 【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
- [npoi] C copy sheet template across workbooks to export Excel
- 无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!
- 在国金证券开户怎么样?保障安全吗?
- 玩转Linux,轻松安装配置MySQL
- How sparksql returns a specific day of the week by date -dayofweek function
- 力扣每日一题-第28天-566.重塑矩阵
- #25class的类继承
- pycharm的plt.show()如何保持不关闭
猜你喜欢

二分查找-2

【uniapp】uniapp手机端使用uni.navigateBack失效问题解决

一起备战蓝桥杯与CCF-CSP之大模拟炉石传说

Leetcode - 226. Retourner l'arbre binaire (bfs)

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!

sparksql如何通过日期返回具体周几-dayofweek函数

pycharm如何修改多行注释快捷键

transforms.RandomCrop()的输入只能是PIL image 不能是tensor

Distributed Architecture Overview

Daily record 2
随机推荐
pycharm的plt.show()如何保持不关闭
Distributed Architecture Overview
Synchronized description of concurrency
[qt learning notes]qt inter thread data communication and data sharing
直播预告|程序员进击,如何提升研发效能?6月21日晚视频号、B站同步直播,不见不散!
Introduction to distributed cache / cache cluster
Jouer avec Linux et installer et configurer MySQL facilement
Leetcode - 226. Retourner l'arbre binaire (bfs)
next(iter(dataloader))的一点点体会
Comp281 explanation
mysql Add column 失败 因为之前有数据,不是默认null 不行
sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
QPushButton 样式使用示例(以及按钮setmenu添加下拉菜单的方法)
17.13 补充知识、线程池浅谈、数量谈、总结
Troubleshooting ideas that can solve 80% of faults!
Number of solutions for knapsack problem
#26class中get和set设置
请指教同花顺开户选选择哪家券商比较好?现在在线开户安全么?
KDD 2022 | how to use comparative learning in cross domain recommendation?
ZCMU--1367: Data Structure