程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementation of RSA encryption and decryption in Python

編輯:Python

Two 64 The ciphertext of bits is multiplied to become 128 A cipher , Can the ciphertext be decrypted without the original private key , After executing the code written by yourself, the decryption fails all the time , Can't find the problem . The code is as follows : On the penultimate line , Decrypt m4 Has been decrypted unsuccessfully

-- coding: utf-8 --

import rsa

Sir, make a pair of keys , Then save .pem Format file , Of course, it can also be used directly

(pubkey, privkey) = rsa.newkeys(512)
pub = pubkey.save_pkcs1()
pri = privkey.save_pkcs1()

load Public key and key

message = '18.234'.encode('utf-8')
m2='1.3'.encode('utf-8')
pubkey = rsa.PublicKey.load_pkcs1(pub)
privkey = rsa.PrivateKey.load_pkcs1(pri)

Encrypt with a public key 、 Decrypt with the private key

```python

crypto = rsa.encrypt(message, pubkey)```
m2 =rsa.encrypt(m2,pubkey)
print(crypto)
print(m2)

```python

crypto = int.from_bytes(crypto, byteorder='little', signed=True)
m2 = int.from_bytes(m2, byteorder='little', signed=True)```
print(crypto)
print(m2)
m3=crypto*m2
print(m3)

```python

crypto = crypto.to_bytes(64, byteorder='little', signed=True)
m2 = m2.to_bytes(64, byteorder='little', signed=True)
m3 = m3.to_bytes(128, byteorder='little', signed=True)
print(crypto)
print(m2)
length=len(m3)
m4=m3[0:64]
m5=m3[64:length]
message = rsa.decrypt(crypto, privkey)
m2 = rsa.decrypt(m2, privkey)
m4 = rsa.decrypt(m4, privkey)
m5 = rsa.decrypt(m5, privkey)
print(message.decode('utf-8'))
print(m2.decode('utf-8'))```
print(m4.decode('utf-8'))
print(m5.decode('utf-8'))




Take the answer :

You should comment out this one , try m5 Can it be decrypted successfully , If m5 Also decryption error , That is, interception is wrong



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved