【问题标题】:How to encrypt plaint text to cipher text using PN Sequence? [closed]如何使用 PN 序列将明文加密为密文? [关闭]
【发布时间】:2021-02-25 14:47:16
【问题描述】:

我正在尝试创建一个使用 PN 序列将纯文本加密为密文的函数。 但我不知道该怎么做。是否有人有任何算法可以帮助我创建一个可以帮助我构建加密函数的函数。

【问题讨论】:

    标签: python python-3.x encryption cryptography caesar-cipher


    【解决方案1】:

    这段代码使用LFSR移位寄存器得到一个伪随机序列,然后与明文异或得到密文。代码使用pylfsrnumpy

    from pylfsr import LFSR
    
    # The initial state
    state = [0,0,0,1,0,1,0,1,0,1,1]
    # The LFSR polynomial use a primitive polynomail to get maximum period length
    poly = [5, 4, 3, 2]
    l = LFSR(fpoly=poly, initstate =state)
    
    message = b"This is a Test Message"
    ciphertext = b""
    
    # generate all LFSR sequence
    allseq = l.runFullCycle()
    seq = ""
    seq_index = 0
    
    # Convert LFSR bits into a string
    for x in allseq:
        seq += str(x)
    
    for counter in range(len(message)):
        ran_seq = seq[seq_index: seq_index+8]
        # Now encrypt by XOR convert to bytes and append to ciphertext
        ciphertext += bytes([int(message[counter]) ^ int(ran_seq, 2)])
        seq_index += 8  # Move sequence to Next byte
    
    print(ciphertext)
    
    plaintext = b""
    # Reset The LFSR sequence to start from beginning
    seq_index = 0
    for counter in range(len(ciphertext)):
        ran_seq = seq[seq_index: seq_index+8]
        plaintext += bytes([int(ciphertext[counter]) ^ int(ran_seq, 2)])
        seq_index += 8
    
    print(plaintext)
    

    【讨论】:

    • 谢谢它帮助了我很多。但是是否可以做类似的事情,但不以字节为单位进行编码和解码?
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多