欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

rsa算法的python實(shí)現(xiàn)示例-創(chuàng)新互聯(lián)

這篇文章主要介紹rsa算法的python實(shí)現(xiàn)示例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

采用H5高端網(wǎng)站建設(shè)+css3國際標(biāo)準(zhǔn)網(wǎng)站建設(shè),讓網(wǎng)站自動(dòng)適應(yīng)用戶使用終端設(shè)備,PC、平板、手機(jī)等,一個(gè)網(wǎng)址適應(yīng),一套內(nèi)容統(tǒng)一戰(zhàn)略,節(jié)約企業(yè)資源。創(chuàng)新互聯(lián)還提供網(wǎng)站后期營銷如:軟文發(fā)稿友情鏈接、一元廣告等。一般建站公司不為企業(yè)填充資料,更談不上內(nèi)容策劃,結(jié)果導(dǎo)致網(wǎng)站界面優(yōu)秀,內(nèi)容卻十分空泛或整體不協(xié)調(diào),內(nèi)容策劃、內(nèi)容填充請交給我們。
'''
Created on 2012-11-5

@author: Pandara
'''
import math
import random 
import sys

def is_prime(num):
    '''
    determine wether num is prime
    '''
    for i in range(int(math.floor(float(num) / 2)) + 1)[2:]:
        if num % i == 0:
            return False
    
    return True

def random_prime(low = 0, high = 100):
    '''
    to get prime randomly
    low: lower bound of range
    high: higher bound of range
    '''
    
    rand = random.randint(low, high)
    
    while(True):#is prime or not
        if not is_prime(rand):
            rand = (rand + 1) % (high - low) + low
        else:
            return rand

def gcd(a, b):
    '''
    get gcd from a and b
    '''
    if a < b:
        a, b = b, a
        
    while b:
        a, b = b, a % b
    return a

def egcd(a,b):
    '''
    Extended Euclidean Algorithm
    returns x, y, gcd(a,b) such that ax + by = gcd(a,b)
    '''
    u, u1 = 1, 0
    v, v1 = 0, 1
    while b:
        q = a // b
        u, u1 = u1, u - q * u1
        v, v1 = v1, v - q * v1
        a, b = b, a - q * b
    return u, v, a

def modInverse(e,n):
    '''
    d such that de = 1 (mod n)
    e must be coprime to n
    this is assumed to be true
    '''
    return egcd(e,n)[0]%n

def generate_key(e):
    p = random_prime(1000, 2000) #get p and q, to make p and q big enough
    q = random_prime(1000, 2000) #such that e < euler_n
    
    n = p * q #n equal to p * q
    
    euler_n = (p - 1) * (q - 1) #euler(n) = (p - 1)(q - 1)
    
    #adjusting e
    while gcd(euler_n, e) != 1:
        e += 2 #keep it odd
        
    d = modInverse(e, euler_n)
    return e, d, n

def rsa_encrypt(e, n, msg):
    '''
    e: such that ed mod(n) = 1, public, chosen
    n: equal to p * q
    msg: needs encrypting
    return: list with dicimal digits, from elements in msg after encrypted
    '''
    cmsg = []
    
    for elem in msg:
        cmsg.append(pow(ord(elem), e, n))
    
    return cmsg

def rsa_dencrypt(d, n, cmsg):
    '''
    d: such that ed mod(n) = 1, private, calculated
    n: equal to p * q
    cmsg: list with dicimal digits, from elements in msg after encrypted
    '''
    msg = []
    
    for elem in cmsg:
        msg.append("%c" % pow(elem, d, n))
        
    return "".join(msg)

if __name__ == "__main__":
    while(True):
        print "##############menu###############"
        print "1.generate keys"
        print "2.encryption"
        print "3.decryption"
        print "other for quit"
        chose = input("input your choice:")
        
        if chose == 1:
            e, d, n = generate_key(65537)
            print "generate keys as follow:"
            print "e = %d, d = %d, n = %d" % (e, d, n)
            print "public key(e, n) = (%d, %d), private key(%d, %d)" % (e, n, d, n)
        elif chose == 2:
            msg = raw_input("input your message:")
            e, n = input("input the public key(with format \"e,n\"):")
            
            sys.stdout.write("ciphertext:")
            for elem in rsa_encrypt(e, n, msg):
                sys.stdout.write(" %d" % elem)
            sys.stdout.write("\n")
        elif chose == 3:
            cmsg_str = raw_input("input your ciphertext:")
            d, n = input("input the private key(with format \"d,n\"):")
            
            cmsg = []
            for elem in cmsg_str.split(" "):
                cmsg.append(long(elem))
            
            print "plaintext:", rsa_dencrypt(d, n, cmsg)
        else:
            break

尚未解決的問題:如何利用擴(kuò)展歐幾里得算法求模逆元?即egcd及modInverse的實(shí)現(xiàn)

以上是“rsa算法的python實(shí)現(xiàn)示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:rsa算法的python實(shí)現(xiàn)示例-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article44/dgpihe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、做網(wǎng)站手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管