99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀python實現(xiàn)RSA加密(解密)算法
python實現(xiàn)RSA加密(解密)算法
2017-08-20
收藏

python實現(xiàn)RSA加密(解密)算法

RSA是目前最有影響力的公鑰加密算法,它能夠抵抗到目前為止已知的絕大多數(shù)密碼攻擊,已被ISO推薦為公鑰數(shù)據加密標準。

今天只有短的RSA鑰匙才可能被強力方式解破。到2008年為止,世界上還沒有任何可靠的攻擊RSA算法的方式。只要其密鑰的長度足夠長,用RSA加密的信息實際上是不能被解破的。但在分布式計算和量子計算機理論日趨成熟的今天,RSA加密安全性受到了挑戰(zhàn)。

RSA算法基于一個十分簡單的數(shù)論事實:將兩個大素數(shù)相乘十分容易,但是想要對其乘積進行因式分解卻極其困難,因此可以將乘積公開作為加密密鑰。

核心代碼:    
# -*- encoding:gbk -*- import math,random#導入模塊 def prime_num(max_num):#生成小于max_num的素數(shù)列表 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():#生成密鑰的函數(shù) prime=prime_num(400)#小于400的素數(shù)列表 p=random.choice(prime[-50:-1])#從后50個素數(shù)中隨機選擇一個作為p q=random.choice(prime[-50:-1])#從后50個素數(shù)中隨機選擇一個作為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)#隨機選一個素數(shù) 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

下面一段代碼給大家介紹python_rsa加密解密

使用python進行rsa加密與加密,包括公鑰加密私鑰解密,私鑰加密公鑰解密。(需要安裝M2Crypto庫)。

代碼:    
#!/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)

數(shù)據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調,回調的第一個參數(shù)驗證碼對象,之后可以使用它調用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }