博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Python进行RSA安装加密

Posted on 2017-09-30 23:01  红与黑hyh  阅读(2010)  评论(0编辑  收藏  举报

一、下载ez_setup.pyhttp://peak.telecommunity.com/dist/ez_setup.py

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

三、安装完成后,会在scripts文件夹下生成几个exe可执行文件。(如: D:\Python27\Scripts目录下)

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

加密实例

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)
#====================================