当前位置:网站首页>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))
边栏推荐
- 我想知道,我在肇庆,到哪里开户比较好?网上开户是否安全么?
- JNI的 静态注册与动态注册
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- #26class中get和set设置
- Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
- 玩转Linux,轻松安装配置MySQL
- LeetCode——226. Flip binary tree (BFS)
- Redis and database data consistency
- 分布式缓存/缓存集群简介
- No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete
猜你喜欢
小程序设置按钮分享功能
[buuctf.reverse] 126-130
mysql Add column 失败 因为之前有数据,不是默认null 不行
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
Leetcode HOT100 (22--- bracket generation)
玩轉Linux,輕松安裝配置MySQL
#26class中get和set设置
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
随机推荐
LeetCode——226. 翻轉二叉樹(BFS)
[buuctf.reverse] 126-130
JNI的 静态注册与动态注册
Leetcode HOT100 (22--- bracket generation)
你好,现在网上股票开户买股票安全吗?
Applet setting button sharing function
手写promise.all
ZCMU--1367: Data Structure
vue--vuerouter缓存路由组件
Platform management background and merchant menu resource management: Design of platform management background data service
解决pycharm里面每个字母占一格空格的问题
Problems encountered this week
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
【代码随想录-动态规划】T583、两个字符串的删除操作
Knapsack problem with dependency
next(iter(dataloader))的一点点体会
【NPOI】C#跨工作薄复制Sheet模板导出Excel
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
VSCode使用 - Remote-SSH 配置说明
Synchronized description of concurrency