基于PKCS1_OAEP的RSA加解密

基于PKCS1_OAEP的RSA加解密

# -*- coding: UTF-8 -*-
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import libnum
import base64
'''
#注释部分为加密过程
m = "flag{I_Really_Love_You_Very_much_Forver_every!}"
pub = open('pubkey.pem').read()
key = RSA.importKey(pub)
cipher = PKCS1_OAEP.new(key)
mimi = base64.b64encode(cipher.encrypt(m))
with open(r"mimi.enc","wb") as f:
    f.write(mimi)
    f.close()
'''
#以下为解密过程
#解析pubkey.pem得到以下参数
n = 26405201714915839490865227813246218372938736243516916108608439705738170543023112509150522274284238701776297205717958250714972924576706985074311737321016048409831557758205687745692399643151467933196930799657476449865271038382866908177517793954543176769652784274788769353482450910551831498252972857285424471782215525406445071432588374802623485148684255853068532820859835479998199886719945699488858505070686919320144576280217196858823521754407597888769668827432569034617434944912323704501156532854074083408527717809315663187405585840074689387865750105223058720511199252150772925124516509254841404742306560035497627834727
e = 65537
q = 157790417717035275943197904823645145281147085252905247447260034051878691034747684303715336348507267921249655103263347914128144476912685213431110454636244692224328066884510063590700506729345331153483633231327359450199822698241355428609085077662488946173655043172957247264543259611018596088670385591091710018977
p = 167343506005974003380506069679607737381940204686173214188860057004909006055220516074283090160430833007424970980655748310232878462615469792561310560310363430669700009093597847018287568821792168143170329382585883857083334915378884054389878477389765792275111293420203613159303898365894897865177093362621517279751
d = libnum.invmod(e, (p - 1) * (q - 1))
private_key = RSA.construct((n, e, d, p, q))
decipher = PKCS1_OAEP.new(private_key)

c = open('mimi.enc').read()
c = base64.b64decode(c)
flag = decipher.decrypt(c)
print flag

你可能感兴趣的:(Crypto-RSA,安全)