Python implements RSA encryption (decryption) algorithm

Source: Internet
Author: User
RSA is currently the most influential public-key encryption algorithm, it can resist the most known password attacks so far, has been recommended by the ISO public key data Encryption standard.

Only short RSA keys today can be broken by brute force mode. Until 2008, there was no reliable way to attack the RSA algorithm in the world. As long as its key length is long enough, the information encrypted with RSA cannot actually be broken. However, with the maturity of distributed computing and quantum computer theory, RSA encryption security has been challenged.

The RSA algorithm is based on a very simple number theory fact: it is easy to multiply two large primes, but it is extremely difficult to factorization the product, so you can expose the product as an encryption key.

Core code:

#-*-ENCODING:GBK-*-import math,random# import module Def prime_num (max_num): #生成小于max_num的素数列表 prime_num=[] for i in Xrange (2,max _num): Temp=0 sqrt_max_num=int (Math.sqrt (i)) +1 for J in Xrange (2,sqrt_max_num): If i%j==0:temp=j break if temp==0:prime_ Num.append (i) return prime_num def rsa_key (): #生成密钥的函数 prime=prime_num (+) #小于400的素数列表 P=random.choice (Prime[-50:-1]) #从后50个素数中随机选择一个作为p Q=random.choice (prime[-50:-1]) #从后50个素数中随机选择一个作为q while (p==q): #如果p和q相等则重新选择 Q=random.choice ( PRIME[-50:-1]) n=p*q r= (p-1) * (q-1) R_prime=prime_num (R) e=random.choice (r_prime) #随机选一个素数 d=0 for N in Xrange (2,r): if (e* N)%r==1:d=n break return ((N,e), (n,d)) def encrypt (Pub_key,origal): #生成加密用的公钥 n,e=pub_key return (origal**e)%N def DECRYP T (pri_key,encry): #生成解密用的私钥 n,d=pri_key return (encry**d)%N

The following section of code to introduce PYTHON_RSA encryption and decryption

Use Python for RSA encryption and encryption, including public key encryption private key decryption, private key encryption public key decryption. (The M2crypto library needs to be installed).

Code:

#!/usr/bin/env python#encoding=utf-8 ' test RSA encryption decryption ' from M2crypto import RSA msg = ' aaaa-aaaa ' rsa_pub = Rsa.load_pub_key (' Rsa_pub.pem ') Rsa_pri = Rsa.load_key (' Rsa_pri.pem ') print ' ****************************************************** ' Print ' public key encryption, private key decryption ' Ctxt = Rsa_pub.public_encrypt (msg, rsa.pkcs1_padding) Ctxt64 = Ctxt.encode (' base64 ') print (' Ciphertext:%s '% ctxt64) Rsa_pri = Rsa.load_key (' rsa_pri.pem ') txt = rsa_pri.private_decrypt (ctxt, rsa.pkcs1_padding) print (' PlainText: %s '% txt) print ' ************************************************************* ' print ' private key encryption, public key decryption ' CTXT_PRI = Rsa_ Pri.private_encrypt (msg, rsa.pkcs1_padding) Ctxt64_pri = Ctxt.encode (' base64 ') print (' Ciphertext:%s '% ctxt64_pri) Txt_pri = RSA _pub.public_decrypt (Ctxt_pri, rsa.pkcs1_padding) print (' Clear text:%s '% txt_pri)

Installation instructions for the library

Download address of M2crypto library:

Https://github.com/martinpaljak/M2Crypto

Or: Https://pypi.python.org/pypi/M2Crypto

Dependent libraries: Openssh-devel gcc Swig (these 3 libraries can be installed directly with Yum on CentOS)

  • 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.