msmshazan
Yesterday I watched the Handmade Hero videos on compression and I thought about what is different between compression and encryption.And why most compression software provide password features .Is it easy to encrypt when compressed or vice versa.
msmshazan
And how do these software store password data in the file .Do they use an algorithm to encrypt password and store it in the file header(maybe XOR).
msmshazan
And will Casey do a video on encryption for maybe i don't know save-file protection.
msmshazan
So if i am writing a basic RLE compressor .What is the encryption to be used? Just for practicing.
Should i just XOR all the File Contents after compressing. Any guidance would be helpful.
Kelimionmsmshazan
Yesterday I watched the Handmade Hero videos on compression and I thought about what is different between compression and encryption.And why most compression software provide password features .Is it easy to encrypt when compressed or vice versa.
It's easy to encrypt after compression, but not easy to compress after encryption. If you do encryption well, the resulting data is almost indistinguishable from random data. That means there's little for a compression algorithm to work with in the way of redundancy it can remove.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | static void Encrypt(size_t MaxOutSize,u8 *Out,char * keys) { u8 * key = (u8 *)keys; for(unsigned int i=0; i<MaxOutSize;) { for(int j = 0; i<strlen(keys);j++) { Out[i]=Out[i]^key[i]; i++; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | int main(int ArgCount ,char** Args) { if(ArgCount >= 4) { size_t FinalOutputSize =0; u8 *FinalOutputBuffer = 0; char * Command = Args[1]; char * InFileName = Args[2]; char * OutFileName = Args[3]; char * Password = Args[4]; file_contents InFile = ReadEntireFileIntoMemory(InFileName); if(ArgCount == 5) { Password = Args[4]; } if(strcmp(Command ,"compress") == 0) { size_t OutBufferSize = GetMaximumCompressedOutputSize(InFile.FileSize); u8 *OutBuffer = (u8 *)malloc(OutBufferSize); size_t CompressedSize = Compress(InFile.FileSize,InFile.Contents,OutBufferSize,OutBuffer + 4); if(ArgCount == 5) Encrypt(OutBufferSize,OutBuffer,Password); *(int unsigned *)OutBuffer = (int unsigned)InFile.FileSize; FinalOutputSize = CompressedSize + 4; FinalOutputBuffer = OutBuffer; } else if(strcmp(Command ,"decompress") == 0) { if(InFile.FileSize >=4) { size_t OutBufferSize = *(int unsigned *)InFile.Contents; u8 *OutBuffer = (u8 *)malloc(OutBufferSize); if(ArgCount == 5) Decrypt(OutBufferSize,OutBuffer,Password); Decompress(InFile.FileSize - 4,InFile.Contents + 4, OutBufferSize,OutBuffer); FinalOutputSize = OutBufferSize; FinalOutputBuffer = OutBuffer; } else { fprintf(stderr,"Invalid input\n"); } } else { fprintf(stderr,"Unrecognized command %s\n",Command); } if(FinalOutputBuffer) { FILE *OutFile = fopen(OutFileName,"wb"); if(OutFile) { fwrite(FinalOutputBuffer,1,FinalOutputSize,OutFile); } else { fprintf(stderr,"Unable to open output file %s\n",OutFileName); } } } else { fprintf(stderr,"Usage: %s compress [raw filename] [compressed filename] [optional:password]\n", Args[0]); fprintf(stderr," %s decompress [compressed filename] [raw filename] [optional:password]\n", Args[0]); } return 0; } |
ratchetfreak
But encrypting after compression leaks information about the length of the compressed data (down to the block granularity). Which in turn leaks information about the general structure of the file (with an LZ compression that means number of repeated strings). If the attacker is in control of even a small part of the message then he can find out more about the rest of the message.
It's things like this that make security and encryption a hard problem.
msmshazan
This is the XOR function but its so slow,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 static void Encrypt(size_t MaxOutSize,u8 *Out,char * keys) { u8 * key = (u8 *)keys; for(unsigned int i=0; i<MaxOutSize;) { for(int j = 0; i<strlen(keys);j++) { Out[i]=Out[i]^key[i]; i++; } } }
1 2 3 4 5 6 7 8 9 10 11 12 | static void Encrypt(size_t MaxOutSize, u8 * Out, char * keys) { u8 * key = (u8 *)keys; size_t key_len = strlen(key); for (size_t i = 0; i < MaxOutSize; ++i) { size_t j = i % key_len; Out[i] = Out[i] ^ key[j]; } } |
msmshazan
This is the XOR function but its so slow,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void Encrypt(size_t MaxOutSize,u8 *Out,char * keys) { u8 * key = (u8 *)keys; int keylen = strlen(keys); for(unsigned int i=0; i<MaxOutSize;) { for(int j = 0; j<keylen && i<MaxOutSize;j++) { Out[i]=Out[i]^key[j]; i++; } } } |