Preliminary understanding RSA encryption

0. reason

Public election chose a water course, the job is a string of characters using the RSA encryption provided by the teacher, the teacher provides p, q, e, his own hands to save the private key, that is to say, this is not a job checking . No way, can not seem to fool passed. . .
And combined with the recent situation, cryptography is becoming increasingly important, learning about the total will not suffer.

1. Principle

Principle not described in detail here, posted a teacher's blackboard Lee Wing-lok, Lee Wing-lok teacher to explain the principles of RSA
the original video is av26639065

2. Encryption

People can begin to learn to teach the machine. Because the teacher has provided p, q, e, and therefore do not need to find our own, has also been expressly can be encrypted directly.

def RSA_encode(Ming_Wen, n, e):                 # 明文使用公钥加密
    m = Ming_Wen
    return m ** e % n

Then call on it

if __name__ == "__main__":
    str_miwen = #明文
    p = 7
    q = 17
    e = 13
    Mi_Wen1 = RSA_encode(str_miwen[0], p * q, e)
    Mi_Wen2 = RSA_encode(str_miwen[1], p * q, e)
    print("密文是 {},{}".format(Mi_Wen1, Mi_Wen2))

The answer is counted out, but how do you know my calculations are right? In the hands of private teacher, I can not directly decrypt the ciphertext back to plaintext to verify its correctness
it no other way to school to learn a full bar

3. Key Generation

def key_design(q, p):                           # 公私密钥生成
    n = q * p
    fn = (q-1) * (p-1)
    while(1):                                   # 随机选取e
        e = random.randint(2, fn)
        if gcd(fn, e):
            break
    while(1):                                   # 随机选取d
        d = random.randint(2, 20 * e)
        if (e * d) % fn == 1:
            break

    return n, e, d

To be honest, I have selected for the d do not know, there seems to be 10 times what rules?
According to your stomach, but anyway, a checking step of, after the big deal modify
gcd is judged whether the prime function, as follows

def gcd(a, b):                                  # 判断是否互质,是则返回True
    if b == 0:
        return False
    elif a == 1 or b == 1:
        return True
    else:
        return gcd(b, a % b)

4. decryption

def RSA_decode(Mi_Wen, n, d):
    c = Mi_Wen
    return c ** d % n

Very simple, not much talk

5. Checking

——snip——
if __name__ == "__main__":
    print("此处为验算:")
    p = 7
    q = 17
    print("p = {}, q = {}".format(p, q))

    n, e, d = key_design(p, q)
    print("n = {}, e = {}, d = {}\n明文为 23,66".format(n, e, d))

    Mi_Wen1 = RSA_encode(23, p * q, e)
    Mi_Wen2 = RSA_encode(66, p * q, e)
    print("加密得 %d,%d" % (Mi_Wen1, Mi_Wen2))

    Ming_Wen1 = RSA_decode(Mi_Wen1, n, d)
    Ming_Wen2 = RSA_decode(Mi_Wen2, n, d)
    print("解密得 {},{}".format(Ming_Wen1, Ming_Wen2))

    if Ming_Wen1 != 23 or Ming_Wen2 != 66:
        print("程序出错!中止加密")
        exit(0)
    else:
        print("验算成功,下面进行加密:")
——snip——

Ran a lot of times for an error is found, the program run for the time being is considered relatively okay

Released two original articles · won praise 3 · views 69

Guess you like

Origin blog.csdn.net/qq_45433084/article/details/104671839