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

    Security and Cryptography in Python - Hash Functions(2)

    Digital Signatures works - uses hash functions

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

    Generate the public key and secret key:

    image-20210227140337738

    Code in Python;

    import hashlib
    
    # These are Alice's RSA keys
    # Public key (e, n): 3 279379
    # Secret key(d) 46387
    n = 279379
    e = 3
    d = 46387
    
    # This is the message that Alice wants to sign and send to Bob
    message = "Bob you are awesome".encode()
    
    # Step 1: hash the message
    sha256 = hashlib.sha256()
    sha256.update(message)
    h = sha256.digest()
    h = int.from_bytes(h, "big") % n
    print("Hash value", h)
    # Step 2:decrypt the hash value(use secret exponent)
    sign = h**d % n
    # Step 3: send message with signature to Bob
    print(message, sign)
    
    # Bob verifying the signature
    # Step 1: calculate the hash value of the message
    sha256 = hashlib.sha256()
    sha256.update(message)
    h = sha256.digest()
    h = int.from_bytes(h, "big") % n
    print("Hash value", h)
    # Step 2: Verify the signature
    verification = sign**e % n
    print("Verification value", verification)
    

    Running Result:

    image-20210227140306885

    Eve trying to modify a signed message

    import hashlib
    
    def modify(m):
        l = list(m)
        l[0] = l[0] ^ 1
        return bytes(l)
    
    # These are Alice's RSA keys
    # Public key (e, n): 3 279379
    # Secret key(d) 46387
    n = 279379
    e = 3
    d = 46387
    
    # This is the message that Alice wants to sign and send to Bob
    message = "Bob you are awesome".encode()
    
    # Step 1: hash the message
    sha256 = hashlib.sha256()
    sha256.update(message)
    h = sha256.digest()
    h = int.from_bytes(h, "big") % n
    print("Hash value", h)
    # Step 2:decrypt the hash value(use secret exponent)
    sign = h**d % n
    # Step 3: send message with signature to Bob
    print(message, sign)
    
    # This is Eve being evil and modifies the message
    message = modify(message)
    print(message)
    
    # Bob verifying the signature
    # Step 1: calculate the hash value of the message
    sha256 = hashlib.sha256()
    sha256.update(message)
    h = sha256.digest()
    h = int.from_bytes(h, "big") % n
    print("Hash value", h)
    # Step 2: Verify the signature
    verification = sign**e % n
    print("Verification value", verification)
    

    Running result (The hash values is different now):

    image-20210227155602832

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    poj1811
    linux系统nginx如何部署vue项目(附详细步骤)
    vue中设置input输入框的值为正整数,不能为负数和小数
    nginx(windows)如何部署vue项目
    vue多个表单验证(Promise.all)
    vue 路由传递参数,刷新页面 数据变成 [Object object]
    vue中解决父组件给子组件传值,子组件拿不到值
    vue项目封装axios请求的get、post、put、delect请求
    vue代码格式化-eslint
    vue后台管理项目PC端页面自适应
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14455525.html
Copyright © 2011-2022 走看看