zoukankan      html  css  js  c++  java
  • 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

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    [arXiv18]更快的基于非二叉化自底向上策略的转移系统成分句法分析
    [AAAI18]面向序列建模的元多任务学习
    [COLING18]两种成分句法分析的局部特征模型
    [ACL18]基于RNN和动态规划的线性时间成分句法分析
    成分句法分析综述
    [NAACL16]RNN文法
    [TACL17]基于中序转移的成分句法分析
    K-摇臂赌博机算法与实现
    关于JVM案例分析(四)
    关于JVM案例分析(三)
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14381552.html
Copyright © 2011-2022 走看看