简体   繁体   English

创建一个使用元音 = 0 辅音 = 1 将二进制替换为单词的函数

[英]creating a function to sobstitute binary to words using vowel=0 consonant=1

I'm trying to create a python program to encrypt(obfuscate?) a binary code so that each 0 will become a vowel and each 1 a consonant and there will be only words of complete meaning: for example the bin number 10110 will beacome HELLO.我正在尝试创建一个 python 程序来加密(混淆?)一个二进制代码,以便每个 0 将成为元音,每个 1 成为辅音,并且只有完整含义的单词:例如,bin 编号 10110 将变成 HELLO . the following code is the program i wrote, but i can't find a way to perform the "encription"以下代码是我编写的程序,但我找不到执行“加密”的方法

import binascii

#to convert from ascii to bin / from bin  to ascii
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
    bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
    return bits.zfill(8 * ((len(bits) + 7) // 8))

def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
    n = int(bits, 2)
    return int2bytes(n).decode(encoding, errors)

#making a wordlist
f = open("/home/unkn0wn/PycharmProjects/pythonProject/wordlist.txt","r")
list1 = [line.rstrip('\n') for line in f]
f.close()
vocali = ['a', 'e', 'i', 'o', 'u']

#vowels = 0, consonants = 1
def trasform(word):
    lista_parola = list(word)
    empty = []
    for i in lista_parola:
        if i in vocali:
            empty.append('0')
        else:
            empty.append('1')
    binar = "".join(empty)
    return binar
bin_list = []
for i in list1:
    bin_list.append(trasform(i))

#creations of a dict
dizionario = dict(zip(bin_list, list1))

def cript(text):
    testo_in_bin = text_to_bits(text)

any suggestion?有什么建议吗?

I would generate a dictionary that maps bitmasks to a list of corresponding words to choose from.我会生成一个字典,将位掩码映射到可供选择的相应单词列表。 Let's say you have file words.txt , which looks like this:假设您有文件words.txt ,它看起来像这样:

the
quick
brown
fox
jumped
over
lazy
dog

This function reads each word from words.txt , maps the vowels to 0 and the consonants to 1 , and creates- and returns a dictionary whose keys are possible bitmasks, and the associated values are lists of corresponding words.该函数从words.txt读取每个单词,将元音映射到0并将辅音映射到1 ,然后创建并返回一个字典,其键是可能的位掩码,关联的值是相应单词的列表。 I'm making some assumptions here - like the file will only contain lowercase characters and a newline to separate the words.我在这里做了一些假设——比如文件将只包含小写字符和一个换行符来分隔单词。

def generate_mask():

    from string import ascii_lowercase as alphabet

    vowels = set("aeiou")
    consonants = set(alphabet) ^ vowels

    mapping = str.maketrans(
        "".join(vowels) + "".join(consonants),
        ("0" * len(vowels)) + ("1" * len(consonants))
    )

    mask = {}

    with open("words.txt", "r") as file:
        for word in map(str.rstrip, file):
            key = word.translate(mapping)
            if key not in mask:
                mask[key] = []
            mask[key].append(word)

    return mask

The returned mask in this case would be a dictionary, which looks like:在这种情况下返回的mask将是一个字典,它看起来像:

{
    '110': ['the'],
    '10011': ['quick'],
    '11011': ['brown'],
    '101': ['fox', 'dog'],
    '101101': ['jumped'],
    '0101': ['over'],
    '1011': ['lazy']
}
>>> 

As you can see, fox and dog share the same bitmask, so they end up in the same list.如您所见, foxdog共享相同的位掩码,因此它们最终位于同一个列表中。

Then, you'd use this like this:然后,你会像这样使用它:

def main():

    from random import choice

    mask = generate_mask()

    values = ["101", "1011", "101", "101"]

    for value in values:
        print(choice(mask[value]))
    
    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

Output:输出:

dog
lazy
fox
dog
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM