# -*- encoding:gbk -*- import math,random#导入模块 def prime_num(max_num):#生成小于max_num的素数列表 prime_num=[] for i in xrange(2,max_num): temp=0 sqrt_max_num=int(math.sqrt(i))+1 for j in xrange(2,sqrt_max_num): if i%j==0: temp=j break if temp==0: prime_num.append(i) return prime_num def rsa_key():#生成密钥的函数 prime=prime_num(400)#小于400的素数列表 p=random.choice(prime[-50:-1])#从后50个素数中随机选择一个作为p q=random.choice(prime[-50:-1])#从后50个素数中随机选择一个作为q while(p==q):#如果p和q相等则重新选择 q=random.choice(prime[-50:-1]) N=p*q r=(p-1)*(q-1) r_prime=prime_num(r) e=random.choice(r_prime)#随机选一个素数 d=0 for n in xrange(2,r): if (e*n)%r==1: d=n break return ((N,e),(N,d)) def encrypt(pub_key,origal):#生成加密用的公钥 N,e=pub_key return (origal**e)%N def decrypt(pri_key,encry):#生成解密用的私钥 N,d=pri_key return (encry**d)%N
#!/usr/bin/env python
#encoding=utf-8
'''
测试rsa加密解密
'''
from M2Crypto import RSA
msg = 'aaaa-aaaa'
rsa_pub = RSA.load_pub_key('rsa_pub.pem')
rsa_pri = RSA.load_key('rsa_pri.pem')
print '*************************************************************'
print '公钥加密,私钥解密'
ctxt = rsa_pub.public_encrypt(msg, RSA.pkcs1_padding)
ctxt64 = ctxt.encode('base64')
print ('密文:%s'% ctxt64)
rsa_pri = RSA.load_key('rsa_pri.pem')
txt = rsa_pri.private_decrypt(ctxt, RSA.pkcs1_padding)
print('明文:%s'% txt)
print '*************************************************************'
print '私钥加密,公钥解密'
ctxt_pri = rsa_pri.private_encrypt(msg, RSA.pkcs1_padding)
ctxt64_pri = ctxt.encode('base64')
print ('密文:%s'% ctxt64_pri)
txt_pri = rsa_pub.public_decrypt(ctxt_pri, RSA.pkcs1_padding)
print('明文:%s'% txt_pri)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有