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

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

    RSA - 1977

    Key generation

    p,q <- primes

    n = p * q <- modulus

    e <- exponent

    d<- modular multiplicative inverse to e

    Public Key

    (n, e)

    Public encryption of message m

    c = m^e mod n

    Secret Key

    (d)

    Secret decryption of message c

    m = c^d mod n

    Integer Factorization - is that hard? A NP problem.

    RSA(cryptosystem)

    https://en.wikipedia.org/wiki/RSA_(cryptosystem)

    Implementation of RSA in Python
    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
    
    # 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)
    

    The running result:

    image-20210217210141088

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    锤子科技官网:问题整理及注意事项
    springboot中文文档
    Spring Framework 开发参考手册中文(在线HTML)
    .is() 全选复选的判断
    c:forEach用法
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
    火狐浏览器下载文件保存文件名的乱码问题
    多线程安全的解决方法
    MySQL的concat以及group_concat的用法
    mysql 将时间转换成时间戳
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14409753.html
Copyright © 2011-2022 走看看