• Security and Cryptography in Python


    Security and Cryptography in Python - Stream Ciphers(1)

    The practical implementations of One Time Pads

    Real-life One Time Pad?

    • 1Gb message requires 1Gb key streams
    • No resus of key stream
    • Key distribution is difficult
    Stream Cipher

    A stream cipher is like a One Time Pad

    • No requirements on key stream
    • Hence, not TRUE randomness
    • Also, can re-use key stream

    Examples:

    • A5/1(G2 encryption) - 54 bits
    • A5/2(export version) - 17 bits
    • RC4(WEP, SSL) - 40-2048 bits

    All algorithms kept secret

    Challenges

    • Authenticity - MAC
    • Re-use key
    • Low entropy
    Implementation of our Stream Cipher

    Refer to :https://en.wikipedia.org/wiki/Linear_congruential_generator

    class KeyStream:
        def __init__(self, key=1):
            self.next = key
    
        def rand(self):
            self.next = (1103515245*self.next + 12345) % 2**31
            return self.next
    
        def get_key_byte(self):
            return self.rand() % 256
    
    key = KeyStream()
    for i in range(10):
        print(key.get_key_byte())
    

    Running Result:

    image-20210206134003858

    class KeyStream:
        def __init__(self, key=1):
            self.next = key
    
        def rand(self):
            self.next = (1103515245*self.next + 12345) % 2**31
            return self.next
    
        def get_key_byte(self):
            return self.rand() % 256
    
    def encrypt(key, message):
        return bytes([message[i]^ key.get_key_byte() for i in range(len(message))])
    
    key = KeyStream(10)
    message = "Hello, World".encode()
    cipher = encrypt(key, message)
    print(cipher)
    
    key =KeyStream(10)
    message = encrypt(key, cipher)
    print(message)
    

    Running Result:

    image-20210206134533877

    Benefit of Stream Cipher
    import random
    
    class KeyStream:
        def __init__(self, key=1):
            self.next = key
    
        def rand(self):
            self.next = (1103515245*self.next + 12345) % 2**31
            return self.next
    
        def get_key_byte(self):
            return self.rand() % 256
    
    def encrypt(key, message):
        return bytes([message[i]^ key.get_key_byte() for i in range(len(message))])
    
    def transmit(cipher, likely):
        b = []
        for c in cipher:
            if random.randrange(0, likely) == 0:
                c = c ^ 2**random.randrange(0, 8)
            b.append(c)
        return bytes(b)
    
    key = KeyStream(10)
    message = "Hello, World! I am here to declare that I will take over the universe and become the supreme emperor.".encode()
    print(message)
    cipher = encrypt(key, message)
    print(cipher)
    
    cipher = transmit(cipher, 5)
    
    key = KeyStream(10)
    message = encrypt(key, cipher)
    print(message)
    

    Running Result:

    image-20210206150221442

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    冒泡排序
    最长回文子串
    两个排序数组的中位数
    Manacher算法解析
    绕过校园网WEB认证_iodine实现
    绕过校园网WEB认证_dns2tcp实现
    ajax跨域请求
    Vue实例生命周期
    组件化应用构建
    表单输入绑定
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14381552.html
Copyright © 2020-2023  润新知