RSA study notes

Recently
the CTF game which always have problems RSA encryption and decryption

Here's a note on the record do RSA topic

First introduces the basic routines that RSA encryption and decryption:

1, two large selection parameters, to calculate the modulus N = p * q

2, computing the Euler function φ = (p-1) * (q-1), and then select a e (1 <e <φ) , e and [Phi] and relatively prime (coprime: only common divisor of two integers 1 ) F
    = (. 1-P) * (. 1-Q)

3, take the inverse of e modulo d, is calculated as: e * d ≡ 1 (mod φ) ( modular multiplicative inverse: If both e and n are positive integers relatively prime, then we can find an integer d, such that e * d --1 divisible by n, or the remainder e * d is divided by n is 1. in this case, d e is called "modular multiplicative inverse" Euler's theorem may be used to demonstrate the presence of two modular multiplicative inverse necessarily integers a.. , b, which is divided by an integer equal to the remainder resulting m:. a ≡ b (mod m), for example, in addition to the remainder of 3 to 5 except 3 2,11 remainder is also 2, may then be written as 11 ≡ 5 (mod 3))
d = gmpy2.invert (e, modular multiplicative inverse)

Encrypt the plaintext m: c = pow (m, e , N), can be obtained the ciphertext c.
Decrypting ciphertext c: m = pow (c, d , N), can be obtained plaintext m.


Use gmpy2 the python extension library, a high-precision arithmetic operations for the GNU package library, which is the predecessor gmpy, and after adjustment of the package, so greatly simplifying use gmpy2

Mpz large integer
    to initialize a large integer
    Import gmpy2
        n-gmpy2.mpz = (1,257,787) # initialization
        gmpy2.is_prime (n) # probabilistic primality test

d = gmpy2.invert (e, phi_n) modulo counter-element

With this basic knowledge we can now do some of the topics to practice hand it

 


http://ctf5.shiyanbar.com/crypto/rsarsa/rsa.txt

It's simple experiment RSA topic

p =  9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q =  11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
e =  65537
c =  83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

Use RSA to find the secret message

 

 

 

On gmpy2

python code:

import gmpy2
p=gmpy2.mpz(9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483)
q=gmpy2.mpz(11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407)
e=gmpy2.mpz(65537)
f=(p-1)*(q-1)
d=gmpy2.invert(e,f)
print "key:"
print d
c=gmpy2.mpz(83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034)
print "text is"
print pow(c,d,p*q)

 

The subject does not suggest that a string of numbers and finally · flag is output

RSA later add another title here

Guess you like

Origin blog.csdn.net/qq_35811830/article/details/90707536