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. 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:
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. 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:
{
'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.
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
>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.