initialData = file.read(1) newData = initialData >> 1(with carry of LSB to MSB) newData = ~newData newData = newData * key(key is an int passed to the algorithm) pad newData to 2 bytes with 0s encryptedFile.write(newData)
So with that algorithm in mind I wrote this code to try and do that:
def encrypt(fileName, key, m = ''): if m == '': fRead = open(fileName+'.txt', 'rb') fWrite = open(fileName+'encrypted.txt', 'wb') tempB = fRead.read(1) while tempB != b'': if (tempB & b'\x01') != b'\x00': newB = tempB>>1 newB = newB | b'\x80' else: newB = tempB>>1 newB = ~newB newB = newB*key bytesWritten = fWrite.write(newB) print(bytesWritten) tempB = fRead.read(1) fRead.close() fWrite.close() return
However python is not even letting me do these bit-wise operations on the bytes. I keep getting "TypeError: unsupported operand type(s)" for the operations(&, >>, and ~) which I thought were bit-operations(bit-wise AND, rotate right, and bit-wise invert respectively). Is there a way to get the full individual bit control without having to make my own class for this application? Also I know I have not padded the newB out to 2 bytes with 0s yet. I could not even get to try doing that because I can't even execute the previous parts of the algorithm yet.