[菜鸟实录]Python进行RSA加密

2020-01-06 11:36:06 浏览数 (1)

一. 安装RSA模块

     参考http://changfengmingzhi.blog.163.com/blog/static/167105288201331594158559/

1. 下载ez_setup.py(http://peak.telecommunity.com/dist/ez_setup.py)

2. 用python解释执行它 (如使用IDLE打开该py文件,按F5解释执行)

3. 安装完成后,会在scripts文件夹下生成几个exe可执行文件。(如: D:Python27Scripts目录下)

4.可以把之前下载的文件rsa-3.1.1-py2.7.egg拷贝到D:Python27Scripts目录下, 然后在cmd中切换到D:Python27Scripts目录下,执行egg文件:easy_install.exe rsa-3.1.1-py2.7.egg

二. 用Python进行RSA加密实例

实例源码如下:

import os import sys import math def GetDataFromFile(filename):     f = open(filename)     n = int(f.read(),16)     print('*'*77)     print(filename)     print(hex(n))     print('*'*77)     return (n) def my_RSA_encrypt(src, d, n):     x = pow(src, d, n)     print('*'*77)     print("Encrypted Data is:")     print(hex(x))     print('*'*77)     return x def my_RSA_decrypt(src, e, n):     y = pow(src, e, n)     print('*'*77)     print("Decrypted Data is:")     print(hex(y))     print('*'*77)     return y    path = os.getcwd() #==================================== fname = path "\Firm_N.txt" plaintData = GetDataFromFile(fname) fname = path "\Manuf_private_d.txt" d = GetDataFromFile(fname) fname = path "\Manuf_N.txt" n = GetDataFromFile(fname) encryptedData = my_RSA_encrypt(plaintData, d, n) #==================================== fname = path "\Firm_public_e.txt" plaintData = GetDataFromFile(fname) fname = path "\Manuf_private_d.txt" d = GetDataFromFile(fname) fname = path "\Manuf_N.txt" n = GetDataFromFile(fname) encryptedData = my_RSA_encrypt(plaintData, d, n) #====================================

0 人点赞