zoukankan      html  css  js  c++  java
  • Security and Cryptography in Python

    Security and Cryptography in Python - Public Key Encryption Systems - RSA(2)

    The security of RSA and Implement an Attack

    import math
    import random
    
    def is_prime(p):
        for i in range(2, math.isqrt(p)):
            if p % i == 0:
                return False
        return True
    
    def get_prime(size):
        while True:
            p = random.randrange(size, 2*size)
            if is_prime(p):
                return p
    def lcm(a, b):
        return a*b//math.gcd(a, b)
    
    def get_e(lambda_n):
        for e in range(2, lambda_n):
            if math.gcd(e, lambda_n) == 1:
                return e
        return False
    
    def get_d(e, lambda_n):
        for d in range(2, lambda_n):
            if d*e % lambda_n == 1:
                return d
        return False
    
    def factor(n):
        for p in range(2, n):
            if n % p == 0:
                return p, n//p
    
    # Key generation done by Alice (secret)
    # Step 1: Generate two distinct primes
    size = 300
    p = get_prime(size)
    q = get_prime(size)
    print("Generated primes", p, q)
    
    # Step 2: compute n = p*q
    n = p*q
    print("Modulus n:", n)
    
    # Step 3: Compute lambda(n) (lcm(n) = λ(n) = lcm(λ(p),λ(q)), λ(p) = p − 1, lcm(a,b) = |ab|/gcd(a,b))
    lambda_n = lcm(p-1, q-1)
    print("Lambda n", lambda_n)
    
    # Step 4: Choose an integer e such that 1 < e < λ(n) and gcd(e, λ(n)) = 1
    e = get_e(lambda_n)
    print("Public exponent", e)
    
    # Step 5: solve for d the equation d⋅e ≡ 1 (mod λ(n))
    d = get_d(e, lambda_n)
    print("Secret exponent", d)
    
    # Done with key generation.
    print("Public key (e, n):", e, n)
    print("Secret key(d)", d)
    
    # This is Bob wanting to send a message
    m = 117
    c = m**e % n
    print("Bob sends", c)
    
    # This is Alice decrypting the cipher
    m = c**d % n
    print("Alice message", m)
    
    # This is Eve
    # Integer factorization and RSA problem
    print("Eve sees the following:")
    print(" Public key(e,n)", e, n)
    print(" Encrypted cipher", c)
    p, q = factor(n)
    print("Eve: Factors", p, q)
    
    lambda_n = lcm(p-1, q-1)
    print("Eve: Lambda n", lambda_n)
    d = get_d(e, lambda_n)
    print("Eve: Secret exponent", d)
    
    m = c**d % n
    print("Eve: message", m)
    

    Running Result:

    image-20210218222850092

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    FastJSON使用笔记
    使用mysql-connector-java出现的错误
    Maven的学习
    前端部分-CSS基础介绍
    前端知识之HTML内容
    python--使用pymyslq操作数据库
    python---反射详解
    python----re正则模块详解
    python---str和repr
    python---random模块详解
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14413829.html
Copyright © 2011-2022 走看看