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

    Security and Cryptography in Python - Stream Ciphers(2)

    Implementation of the Authenticity problem with Stream Ciphers
    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)
    
    def modification(cipher):
        mod = [0]*len(cipher)
        mod[10] = ord(' ') ^ ord('1')
        mod[11] = ord(' ') ^ ord('0')
        mod[12] = ord('1') ^ ord('0')
        return bytes([mod[i] ^ cipher[i] for i in range(len(cipher))])
    
    # This is Sender
    key = KeyStream(10)
    message = "Send Bob:   10$".encode()
    print(message)
    cipher = encrypt(key, message)
    print(cipher)
    
    #  This is Bob
    cipher = modification(cipher)
    
    # This is bank
    key = KeyStream(10)
    message = encrypt(key, cipher)
    print(message)
    

    Running Result:

    image-20210206154649709

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    sublime text 2安装Emment插件
    PHPExcel IE导出乱码问题
    还是PHPExcel问题
    一条SQL语句查询两表中两个字段
    PHPExcel导出插入图片和居中问题
    jqgrid demo
    openstack horizon 学习(1) 总览
    Python学习笔记
    微软2016校园招聘在线笔试 [Recruitment]
    动态树学习(留坑)
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14381705.html
Copyright © 2011-2022 走看看