user720694
user720694

Reputation: 2075

ascii output of string compression in C

I need to use both compression and encryption in a project. There are two programs in the project.

In the first program, an ascii text file is first compressed and then encrypted. Further operations follow on this encrypted version of the file. However, a second program in the project follows the reverse process i.e. first decrypts and then decompresses to get the original ascii text file.

I've implemented the encryption module (aes via openssl) and it works fine. But when i looked for compression options in linux, i found that gzip, zlib etc throw their own versions of the file i.e. filename.gz or some other extension, the contents of which are not purely ascii. (For instance, i see diamond shaped symbols when i view the output in the terminal) Beause of this, i'm unable to read the compressed file completely in my C program.

So in short, i require a compressed file which contains only ascii characters. Is this possible by any means?

Upvotes: 1

Views: 1446

Answers (2)

user720694
user720694

Reputation: 2075

Finally resolved the issue. The program is handling everything correctly.

On the sending side:

compression: gzip -c secret.txt -9 > compressed.txt.gz
encryption: openssl enc -aes-256-cbc -a -salt -in compressed.txt.gz -out encrypted.txt

The compression output (gz) is given as an input for encryption which outputs a text file. The resulting output is purely ascii.

On the receiving side:

decryption: openssl enc -d -aes-256-cbc -a -in decryptme.txt -out decrypted.txt.gz
decompression: gunzip -c decrypted.txt.gz > message.txt

Upvotes: 2

Aki Suihkonen
Aki Suihkonen

Reputation: 20027

You can add uuencode / uudecode filter in between compression and encryption -- or you might want to loosen the restriction of the compressed data to be in ascii form: options:

  • read binary data from you c-program. (e.g. char buffer[256]; c=fread(buffer,1,256,stdin); )
  • convert the data to hexadecimal format
    static char encrypted_file[]={ 0x01,0x6e, ... };

Upvotes: 0

Related Questions