Python-based rsa encryption algorithm, pythonrsa Encryption Algorithm
This example describes the rsa encryption algorithm implemented by Python. We will share this with you for your reference. The details are as follows:
Algorithm process
1. Select two large prime numbers p and q at will. p is not equal to q and N = pq is calculated.
2. According to the Euler's function, the number of integers not greater than N and interacting with N is (p-1) (q-1 ).
3. Select an integer e and (1) (q-1), and e is less than (1) (q-1 ).
4. Use the formula below to calculate d: d × e limit 1 (mod (p-1) (q-1 )).
5. Destroy records of p and q.
(N, e) is the public key, (N, d) is the private key.
Python code
#-*-Coding: UTF-8 -*-#! /Usr/bin/env pythondef 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 ldef generate_keys (p, q): # numbers = (11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47) numbers = range_prime (10,100) N = p * q C = (p-1) * (q-1) e = 0 for n in numbers: if n <C and C % n> 0: e = n break if e = 0: raise StandardError ("e not found") # change to BaseException d = 0 for n in range (2, C) in Python3 ): 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) % Cdecrypt = encryptif _ name _ = '_ main _': pub, pri = generate_keys (47, 79) L = range (20, 30) C = map (lambda x: encrypt (x, pub), L) D = map (lambda x: decrypt (x, pri), C) print "helper house test results: "print" keys: ", pub, pri print" message: ", L print" encrypt: ", C print" decrypt: ", D
Running result:
In fact, it is not very difficult to implement this process in any language, but our teacher asked us to generate a random number of 1024. It is a bit disgusting to write it in C language, so it is more convenient to implement it in python or java.
PS: if you are interested in encryption and decryption, refer to the online tools on this site:
Online text encryption and decryption tools (including AES, DES, and RC4 ):
Http://tools.jb51.net/password/txt_encode
MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password
Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode