密码学

密码学

2023年5月20日
202305
202305, cryptography

reference #

cryptography https://learn-cryptography.readthedocs.io/zh/latest/basic/
https https://www.bilibili.com/video/BV1KY411x7Jp/?spm_id_from=333.337.search-card.all.click&vd_source=dfa9bf517c1718c832302c15f80df948

密码学 #

  1. 凯撒密码: 平移

  2. 置换密码: 替换

    1. 通过letter frequency分析, 可以破解
  3. one-time pad: 一次性密码本

    import random
    
    def generate_key(n):
        return bytes([random.randrange(0, 256) for i in range(n)])
    
    def xor(key, message):
        return bytes([key[i % len(key)] ^ message[i] for i in range(len(message))])
    
    message = b"ATTACK"
    
    key = generate_key(len(message))
    print("Key: ", key)
    
    cipher = xor(key, message)
    print(cipher)
    
    plain_text = xor(key, cipher)
    print(plain_text)
    
    # 伪随机(线性同余)
    m = 2 ** 31
    a = 1103515245
    c = 12345
    
    def lcg(a, c, m, seed):
        return (a * seed + c) % m
    
    seed = 1
    for _ in range(10):
        seed = lcg(a, c, m, seed)
        print(seed)
    
  4. 流密码

  5. 块密码

  6. diffie-hellman key exchange D-H密钥交换

    1. 在不安全的信道内,安全的交换密钥 信息交换的基石
    2. 对称加密 AES
  7. RSA非对称加密 https://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF%86%E6%BC%94%E7%AE%97%E6%B3%95

  8. 数字签名 digital_signature 解决的问题:证明消息来自私钥方 不能解决的问题:证明公钥是来自安全的私钥方,而不是来自攻击者(证书解决)

  9. 证书 digital_certificate

    1. 特殊的签名
    2. 自签名证书
    3. 证书让浏览器知道服务器是否受信任
  10. https: http+tls https_tls