RSA algorithm (a) CTF in

    I recently viewed a headache c O (≧ mouth ≦) O, so stop and slow a slow write a few blog it.
Before talking about our first look at a well-known network of red teacher Lee Wing-lok video to learn some basic knowledge of RSA, again not read it over twice, twice, three times not see, remember we must understand this video, otherwise, write below likely will not read. Although cryptography and mathematical correlation great, but step by step and not that hard to learn, my math is not good, but this does not prevent me to do these topics cryptography, I can slowly learn. Remember do not yet begun to give up.
    Video link: https://www.bilibili.com/video/av26639065/

    Basic introduction:
    RSA 1977 by the Ronald Rivest (Ron Rivest), Adi Shamir (Adi Shamir) and Leonard Adleman (Leonard Adleman) presented together. At that time the three of them are working at MIT. RSA is the three of them last names beginning with the letter composed pieces together.
    RSA encryption algorithm is an asymmetric encryption algorithm.
    To do integer factorization great difficulty determines the reliability of the RSA algorithm. In other words, do a great integer factorization more difficult, RSA algorithm is more reliable. If someone finds a fast factorization algorithm, then use RSA encryption reliability of the information would certainly be significantly lowered. But to find such an algorithm possibility is very small. Today, only a short RSA keys can be a powerful way of solution before the break. So far, no reliable world ** RSA algorithm approach. As long as the length of the key is sufficiently long, the information is actually used RSA encryption solution can not be broken.
    Generating public and private keys:
(1) before encryption, first find two different large prime numbers p and Q
(2) = p calculated n-
Q
(. 3) according to the Euler function, to obtain φ (n) = [Phi] (P) [Phi] (Q) = (. 1-P) (Q-. 1)
(. 4) to identify a public key e, e to satisfy: an integer of 1 <e <φ (n), and e and so [Phi] ( N) prime. .
(5) The remainder dividing e * d φ (n) is 1, to find the private key d.
Then: is the public key (n, e) is the private key (n, d)

Encryption ==> m ^ e remainder divided by n is the number of c (ciphertext)
00
to decrypt ==> c ^ d remainder divided by n is the number m (plaintext)
01
Now that the principle is not a question you should do to practice Workout of it
02
Title: according to the RSA algorithm, when selecting two odd p = 5, q = 3, the public key e = 7, d is the private key number? (Do not peek at the answers directly resolve ha)
A.6 B.7 C.8 D.9

Analysis: a p = 5, q = 3, e = 7, seen n-= P Q = 15; [Phi] (n-) = (P-. 1) (Q-. 1) =. 8
      by the e
divided by d φ (n) the remainder 1 was, d = 7 * 1 (8 MOD)
      MOD operator (modulo) should most people it does not, then you can go to Baidu.
enn ...... If the above topic you've done it yourself, or read the answers to resolve, and that you can still be pricey savvy Kazakhstan.
? This question is below that it O (∩_∩) O ~
03
topics from: Jarvis OJ - Basic - VeryEasyRSA ( )
04
    Yes (⊙ o ⊙)! The number of big, how do, which is to be scrapped hand rhythm ah O (≧ mouth ≦) O.
    enn ...... how could so said than done, look at the title name --- VeryEasyRSA, so now, we have our tools on the --python. As the saying goes, "工欲善其事必先利其器."
    The following are commonly used in the RSA module installation, after my several attempts, finally found the method of installation without detours, here is python2, because there will be a variety of problems when python3 modules are installed, even that module python3 not, an error will be less when python2 installation, and do what I have not subject rsa Gangster with python3, all-python2 seen.

  Prerequisites: linux environment based installation, make sure you have wget, gcc, make software will basic linux commands.
  We need to install a total of six modules (Do not change the module version download link, I can not guarantee module changed version for the compiler options for the following source code installed, enn when I was in the pit to jump a long time to come out.)

  1. Check whether the M4 module is installed
        : command-line commands man m4
        If not installed, you first install it, most of the information online are written to use the source code to write mounted, but may be compiled versions of software problems, always install m4 source the installation fails, this is the first hole, and finally was able to directly use apt-get command to install, too hard this:apt-get install m4
  2. GMP Module Installation
    wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
    tar -xf gmp-6.1.2.tar.bz2 

    Unzip the file into the folder cd

    ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic

    After the completion of the implementation of re-execution

    make && make check && make install
  3. MPFR installation module (decompression, enter the following command to catalog what's not write, and 2) the (same.)
    wget https://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.bz2 
    ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static
    make && make check && make install
  4. MPC module installation
    wget https://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
    ./configure --prefix=$HOME/static --enable-static --disable-shared --with-pic --with-gmp=$HOME/static --with-mpfr=$HOME/static
    make && make check && make install
  5. Installation gmpy2 module
    git clone https://github.com/aleaxit/gmpy.git
    sudo python setup.py build_ext --static=$HOME/static install
  6. Installation libnum module
    git clone https://github.com/hellman/libnum
    sudo python setup.py install 

        Well, here commonly used RSA decryption modules are installed almost the same. Back to the topic above, we look at how a VeryEasyRSA law.
        Problem-solving script:

#!/usr/bin/python
#coding:utf-8
#@Author:醉清风

import gmpy2
p = 3487583947589437589237958723892346254777
q = 8767867843568934765983476584376578389
e = 65537
phi = (p-1)*(q-1)       #φ(n)在写的时候多用phi代替,因为键盘不好敲出来。
d = gmpy2.invert(e,phi) #e模phi的逆为d, (e*d)%phi==1 原理上面讲过
print d

or

#!/usr/bin/python
#coding:utf-8
#@Author:醉清风

import libnum
p = 3487583947589437589237958723892346254777
q = 8767867843568934765983476584376578389
e = 65537
phi = (p-1)*(q-1)
d = libnum.invmod(e,phi) #e模phi的逆为d,(e*d)%phi==1 
print d

    Both of the above solution just different module calls, but the effect is the same calculation, the latter title will often use these modules, specifically wanted to see their feelings with which module is like.
    Now it is just the beginning, more interesting behind, to be continued ......

Guess you like

Origin blog.51cto.com/13400543/2413968