Implementing RSA algorithm code using python

Source: Internet
Author: User
Tags asymmetric encryption
RSA algorithm is an asymmetric encryption algorithm, which is widely used in public key cryptography, and it is mainly used to encrypt information and digital signature.

Wikipedia gives an introduction to the RSA algorithm as follows:

Suppose Alice wants to receive a private message from Bob through an unreliable medium. She can generate a public key and a private key in the following ways:

Randomly select two large prime numbers p and q,p not equal to Q, calculate N=PQ.

According to Euler functions, the number of integers not greater than N and N coprime is (p-1) (q-1)

Select an integer e with (p-1) (q-1) coprime, and E is less than (p-1) (q-1)

Use the following formula to calculate D:dxe≡1 (mod (p-1) (q-1))

The records of P and Q are destroyed.

(n,e) is the public key, (N,D) is the private key. (N,D) is a secret. Alice passes her public key (N,e) to Bob and hides her private key (N,d).

#!/usr/bin/env python def range_prime (start, end): L = list () for I in range (Start, end+1): flag = True for J in range (2, i): if I% j = = 0:flag = False break if Flag:l.append (i) return L def generate_keys (P, q): #numbers = (11, 13, 17, 19, 2 3, (+), Notoginseng, Numbers =range_prime (TEN) n = p * Q C = (p-1) * (q-1) e = 0 for N in numbers:if N < C a  nd C% n > 0:e = n break if e==0:raise standarderror (' e not found ') d = 0 for n in range (2, C): if (e * n)% C = = 1:d = n Break if d==0:raise StandardError ("D not Found") return ((N, e), (n, D)) def encrypt (M, key): C, x = key return (M *  * x)% c decrypt = Encrypt if __name__ = = ' __main__ ': pub, pri = Generate_keys (M, m) L = range (+) c = map (lambda x:  Encrypt (x, pub), l) D = map (lambda x:decrypt (x, PRI), c) print "Keys:", pub, PRI print "message:", l print "encrypt:", C Print "Decrypt:", D Keys: (3713, one) (3713, 1631) message: [406, 3622, (+), (+), [+], [+], [] Encrypt 3168, 134, 3532, 263, 132743, 2603, 1025] Decrypt: [20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L]  

The above is a small part of the introduction of the use of Python to implement the RSA algorithm code, we hope to help!

  • Related Article

    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.