L
L
Lolec3222021-05-29 15:13:26
Python
Lolec322, 2021-05-29 15:13:26

How to change this DES code so that it becomes applicable to the Russian alphabet?

There is a code from github that implements DES: https://github.com/RobinDavid/pydes/blob/master/py...

There is a task to make it applicable to the Russian alphabet. As far as I understand, this code does not support it, because it works with characters by one byte:

def string_to_bit_array(text):#Convert a string into a list of bits
    array = list()
    for char in text:
        binval = binvalue(char, 8)#Get the char value on one byte
        array.extend([int(x) for x in list(binval)]) #Add the bits to the final list
    return array

def bit_array_to_string(array): #Recreate the string from the bit array
    res = ''.join([chr(int(y,2)) for y in [''.join([str(x) for x in _bytes]) for _bytes in  nsplit(array,8)]])   
    return res

def binvalue(val, bitsize): #Return the binary value as a string of the given size 
    binval = bin(val)[2:] if isinstance(val, int) else bin(ord(val))[2:]
    if len(binval) > bitsize:
        raise "binary value larger than the expected size"
    while len(binval) < bitsize:
        binval = "0"+binval #Add as many 0 as needed to get the wanted size
    return binval

def nsplit(s, n):#Split a list into sublists of size "n"
    return [s[k:k+n] for k in range(0, len(s), n

if padding and action==ENCRYPT:
        self.addPadding()
    elif len(self.text) % 8 != 0:#If not padding specified data size must be multiple of 8 bytes
        raise "Data size should be multiple of 8"


self.generatekeys() #Generate all the keys
    text_blocks = nsplit(self.text, 8) #Split the text in blocks of 8 bytes so 64 bits
    result = list()
def addPadding(self):#Add padding to the datas using PKCS5 spec.
    pad_len = 8 - (len(self.text) % 8)
    self.text += pad_len * chr(pad_len)

I tried to change this to work 16 (replaced values ​​8 with 16) bit characters, but in the case of padding on, the decrypted message becomes empty, and with off, the line about the message multiplicity always raises an error. If it is ignored, then only the first 4 bytes of the message are decrypted (the Russian alphabet works).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question