Cryptography With Python 简明教程

Understanding RSA Algorithm

RSA 算法是一种公钥加密技术,被认为是最安全的加密方式。它由 Rivest、Shamir 和 Adleman 于 1978 年发明,因此得名 RSA 算法。

RSA algorithm is a public key encryption technique and is considered as the most secure way of encryption. It was invented by Rivest, Shamir and Adleman in year 1978 and hence name RSA algorithm.

Algorithm

RSA 算法具有以下特点:

The RSA algorithm holds the following features −

  1. RSA algorithm is a popular exponentiation in a finite field over integers including prime numbers.

  2. The integers used by this method are sufficiently large making it difficult to solve.

  3. There are two sets of keys in this algorithm: private key and public key.

您必须执行以下步骤才能使用 RSA 算法:

You will have to go through the following steps to work on RSA algorithm −

Step 1: Generate the RSA modulus

初始步骤从选择两个质数 p 和 q 开始,然后计算它们的乘积 N,如下所示:

The initial procedure begins with selection of two prime numbers namely p and q, and then calculating their product N, as shown −

N=p*q

此处,令 N 为指定的大数。

Here, let N be the specified large number.

Step 2: Derived Number (e)

将数字 e 视为一个派生数字,它应该大于 1 且小于 (p-1) 和 (q-1)。主要条件是 (p-1) 和 (q-1) 不存在 1 之外的公因子

Consider number e as a derived number which should be greater than 1 and less than (p-1) and (q-1). The primary condition will be that there should be no common factor of (p-1) and (q-1) except 1

Step 3: Public key

指定的数字对 ne 构成 RSA 公钥,并且公之于众。

The specified pair of numbers n and e forms the RSA public key and it is made public.

Step 4: Private Key

私钥 d 根据数字 p、q 和 e 计算。数字之间的数学关系如下:

Private Key d is calculated from the numbers p, q and e. The mathematical relationship between the numbers is as follows −

ed = 1 mod (p-1) (q-1)

以上公式是扩展欧几里得算法的基本公式,它把 p 和 q 作为输入参数。

The above formula is the basic formula for Extended Euclidean Algorithm, which takes p and q as the input parameters.

Encryption Formula

考虑一个发送者,他给拥有公钥 (n,e). 的某人发送明文消息。要在给定的场景中加密明文消息,请使用以下语法:

Consider a sender who sends the plain text message to someone whose public key is (n,e). To encrypt the plain text message in the given scenario, use the following syntax −

C = Pe mod n

Decryption Formula

解密过程非常简单,包括分析以便按照系统方式进行计算。假设接收器 C 拥有私钥 d ,则结果模数将被计算为:

The decryption process is very straightforward and includes analytics for calculation in a systematic approach. Considering receiver* C* has the private key d, the result modulus will be calculated as −

Plaintext = Cd mod n