I've created an encryption-decryption module that works as follows:
1) It takes in a .txt file and reads the text inside
2) Using my encryption algorithm, it converts the original text (OT) to a unique code of 1's and 0's string characters.
3) It then takes the 1's and 0's string characters, encodes them to bytes with the standard format (which writes 8 bits for every 1 or 0) and exports the bytes sequence as a file.
4) It then calls the second script in the module, which reads the bytes file, and opens it as 1's and 0's characters again.
5) Finally, using my decryption algorithm, it takes the 1's and 0's and successfully translates them back to the Original Text.
The problem though is with step 3 - I'm unfortunately very new to Python and Comp Sci, so apologies if I garble this explanation:
I've realised that after converting the OT to 1's and 0's characters, instead of converting them to bytes with the usual codec format (i.e. 8-bits per character, on average) , I need it to write the actual bit of '1' for every 1 character in the code, and likewise the bit of '0' for every 0 character - for which I presume I'll need some sort of 'custom codec'.
What you get then is a bytes file that has exactly the same number of bits as the number of 1's and 0's characters in the coded sequence, instead of 8 bits per character as before. I'll then do some file measurement on this output and the OriginalText.txt file to make sure my output has a fewer number of bytes (i.e. smaller file size) than the OT.
Finally of course, the second script takes in this bytes file, translates it back to the same 1's and 0's sequence using my 'custom encoding-decoding format' and then uses my decryption algorithm to turn this into my original message.
Unfortunately, I'm not sure how I could create my own 'custom codec' and instruct Python to write me some sort of bit sequence in this way?
Further, say perhaps I manage to achieve the above and get my 'custom bit sequence', and that I have it stored in some kind of array (or other object) of bits (bitArray). Before I write it to the output file, I need to put my bitArray plus a couple other objects into a list. This list is actually the thing that needs to be converted to binary and then exported as a file - for which I'm using pickle, and have done for all my original char-binary/binary-char conversion - leaving the binary inside bitArray unaltered.
Further, when pickle converts the output file back into my list object, the bitArray and its 'custom binary' needs to arrive totally unmodified.
So, on top of not knowing how to write this custom codec, I'm assuming pickle can handle the above as I need, but of course could be totally wrong.
Is there a way I might achieve what I need, and am I right in assuming pickle won't mangle the already-bits inside my bitArray?
Thanks very much indeed,
Really appreciate any advice!