RSA密码解密(学习记录)【含python.pow函数原理应用】

#题目来源于(CTFshow*)

ctf.show

crypto4

p=447685307 q=2037 e=17

(p、q公因数) (e为质数)

#写python脚本

import gmpy2      python版本需与gmpy2库的版本相同即兼容

n = p * q

#phi_n=φ(n)        #python脚本选择使用phi_n

 (下横线→alt+9、5键)

φ(n)= (p-1)*(q-1)

d = gmpy2. invert(e,φ(n))

print(d)

crypto5

p=447685307 q=2037 e=17 c=704796792

(与上题不相同的条件为已知c,且求m)

#C为明文,M为密文。

在原有脚本上更新

import gmpy2     

n = p * q     

φ(n)= (p-1)*(q-1)

d = gmpy2. invert(e,φ(n))

m = pow(c,d,n)

print(m)

#pow函数(补充)

正常情况下    pow函数的基础形式pow(x,y,z)

>>> pow(4,0.5)

2.0

>>> pow(3,2)

9

省略z值时,则pow函数返回x值的y次方的值.

>>> pow(9,2,8)
1
>>> pow(8,2,5)
4

z值存在时,pow函数返回值则是等于pow(x,y)的结果对z求余数.

### 

(1)z值省略时,x值和y值可以是整数和浮点数.

>>>a = pow(8,-0.5)
0.3535533905932738
>>> type(a)
<class'float'>
>>>b = pow(8,2)
64
>>> type(b)
<class'int'>

(2)z值不可为0.

>>> pow(4, 2, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: pow() 3rd argument cannot be 0

(3)当z值存在时,x值和y值只能为整数.

>>> pow(3, 0.7, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pow() 3rd argument not allowed unless all arguments are integers

RSA解密在线工具:

工具提取:

链接:https://pan.baidu.com/s/1q-gWmYemSjI9bUhV8YI2KA?pwd=2333 
提取码:2333

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是用Python实现RSA密码的示例代码: ```python import random import math def generate_key(p, q): # 计算n和欧拉函数φ(n) n = p * q phi_n = (p - 1) * (q - 1) # 随机选择一个整数e,且1 < e < φ(n),且e与φ(n)互质 e = random.randrange(1, phi_n) gcd = math.gcd(e, phi_n) while gcd != 1: e = random.randrange(1, phi_n) gcd = math.gcd(e, phi_n) # 计算e的模反元素d d = mod_inverse(e, phi_n) # 返回公钥和私钥 return (e, n), (d, n) def mod_inverse(a, m): # 扩展欧几里得算法求a在模m下的逆元素 g, x, y = egcd(a, m) if g != 1: raise ValueError('modular inverse does not exist') return x % m def egcd(a, b): if a == 0: return (b, 0, 1) else: g, y, x = egcd(b % a, a) return (g, x - (b // a) * y, y) def encrypt(plaintext, public_key): # 加密明文 e, n = public_key ciphertext = [pow(ord(char), e, n) for char in plaintext] return ciphertext def decrypt(ciphertext, private_key): # 解密密文 d, n = private_key plaintext = ''.join([chr(pow(char, d, n)) for char in ciphertext]) return plaintext # 示例使用 p = 61 q = 53 public_key, private_key = generate_key(p, q) print('公钥:', public_key) print('私钥:', private_key) plaintext = 'Hello, RSA!' ciphertext = encrypt(plaintext, public_key) print('密文:', ciphertext) decrypted_text = decrypt(ciphertext, private_key) print('解密结果:', decrypted_text) ``` 在示例中,我们首先使用`generate_key`函数生成RSA加密的公钥和私钥。然后,我们使用`encrypt`函数将明文加密为密文,使用`decrypt`函数将密文解密为明文。在示例中,我们将字符串'Hello, RSA!'作为明文进行加密和解密

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值