Realization of RSA algorithm for generating large prime number by Miller_rabin algorithm

Source: Internet
Author: User
Tags pow

If you like, welcome to the lower right corner.

ImportMath fromFunctoolsImportReduce#used to combine characters fromOsImportUrandom#system-Random charactersImportBinascii#conversion between binary and ASCII#===========================================defmod_1 (x,n):" "modulo negative 1 algorithm: Calculates the value of x2= x^-1 (mod n), r = gcd (A, B) = ia + JB, x and n are mutual primes" "x0=x y0=N X1=0 Y1= 1X2= 1y2=0 whilen! =0:q= x//n (x, N)= (n, x%N) (x1, x2)= ((x2-(Q *x1), x1) (y1, y2)= ((y2-(Q *y1)), y1)ifX2 <0:x2+=y0ifY2 <0:y2+=x0returnX2#===========================================defFast_mod (a,p,m):" "Fast modulus exponential algorithm: calculates the value of (a ^ p)% m and can be replaced with POW ()" "a,p,m=Int (a), int (p), int (m)if(p = =0):return1R= a%m K= 1 while(P > 1):                if((P & 1)! =0): K= (k * r)%m R= (R * r)%m P>>= 1return(R * k)%m#===========================================defrandint (n):" "Random is a pseudo-random number that requires a higher security number to be generated, so use Os.urandom () or systmerandom module, generate N-byte random number (8 bits per byte), return 16 binary to 10 binary integer return" "Randomdata=urandom (n)returnInt (binascii.hexlify (randomdata), 16)    #===========================================defprimality_testing_1 (n):" "test One, a small prime test, with a small prime number within 100 to detect random numbers x, can be a large probability of elimination is not prime, #创建有25个素数的元组" "Sushubiao= (2,3,5,7,11,13,17,19,23,29,31,37,41                   ,43,47,53,59,61,67,71,73,79,83,89,97)         forYinchSushubiao:ifn%y==0:returnFalsereturnTrue#===========================================defprimality_testing_2 (n, k):" "Test Two, K-detection of n using the Miller_rabin algorithm" "        ifN < 2:                returnFalse D= N-1R=0 while  not(D & 1): R+ = 1D>>= 1 for_inchRange (k): a= Randint (120)#Random numberx =Pow (A, D, N)ifx = = 1orx = = N-1:                        Continue                 for_inchRange (r-1): x= POW (x, 2, N)ifx = = 1:                        returnFalseifx = = N-1:                         Break        Else:                returnFalsereturnTrue#===========================================defGetprime (byte): whiletrue:n=randint (Byte)ifprimality_testing_1 (n):ifPrimality_testing_2 (N, 10) :                                Pass                        Else:Continue                Else:Continue                 returnN#===========================================defRSA (): P=getprime (128)#Large integer of 1024bitQ=getprime (128)         whileP==Q:#avoid the same p/q valueQ=getprime (128) n=p*q#N-Value DisclosureOrla= (p-1) * (q-1)#Euler functionse=524289" "E's choice: The binary representation of e should contain as little as 1. E take e=524289, the second binary is 10000000000000000001, only two 1, fast encryption speed and large numbers" "D=mod_1 (E,orla)Print('the public key is ({0},{1}); \ n the private key is ({2},{3})'. Format (n,e,n,d)) message=input ('Please enter any content that needs to be encrypted:')        #receive data from standard input and output streams, digitize and decryptMessage=list (map (ord,message))Print('Ciphertext digitization:', message) ciphertext=[]         forXinchmessage:ciphertext.append (POW (x,e,n))Print('Ciphertext Encryption:', ciphertext) message=[]         whileTrue:message.append (int (Input ('Enter the redaction group \ n (enter 0 at the end):')))                ifmessage[-1]==0:delMessage[-1]                         Breakplaintext=[]         forXinchmessage:plaintext.append (POW (x,d,n))Print('PlainText decryption:', plaintext) plaintext=list (map (chr,plaintext))Print('plaintext word Fu Hua:', plaintext)Print('plaintext=', Reduce (Lambdax,y:x+y), plaintext))#===================================================RSA ()

Realization of RSA algorithm for generating large prime number by Miller_rabin algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.