Hello Helper,
This is my code.
I want to compress the file encrypted and then decrypt the same original file.Can you help ?
/*

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

#define ENCRYPTION_FORMULA (int)Byte+25
#define DECRYPTION_FORMULA (int)Byte-25

int Encrypt(char *FILENAME,char *NEW_FILENAME)
{
	ifstream fin;
	ofstream fout;
	char Byte;
	char NewByte;

	fin.open(FILENAME,ios::in,ios::binary);
	fout.open(NEW_FILENAME,ios::out,ios::binary);

	if(!fin)
	{
		cout<<"Error in opening the file:"<<endl;
		return 1;
	}

	while(!fin.eof())
	{
		Byte = fin.get();
		if(fin.fail())
		{
			return 0;
		}

		NewByte = ENCRYPTION_FORMULA;
		fout.put(NewByte);
	}

	fin.close();
	fout.close();
	return 1;
}

int Decrypt(char *FILENAME,char *NEW_FILENAME)
{
	ifstream fin;
	ofstream fout;
	char Byte;
	char NewByte;

	fin.open(FILENAME,ios::in,ios::binary);
	fout.open(NEW_FILENAME,ios::out,ios::binary);

	if(!fin)
	{
		cout<<"Error in opening the file:"<<endl;
		return 1;
	}

	while(!fin.eof())
	{
		Byte = fin.get();
		if(fin.fail())
		{
			return 0;
		}

		NewByte = DECRYPTION_FORMULA;
		fout.put(NewByte);
	}

	fin.close();
	fout.close();
	return 1;
}

void main()
{
	clrscr();
	int option;

	char encfile[200];
	char new_encfile[200];
	char new_decfile[200];
	char decfile[200];

	cout<<"Enter 1 to ENCRYPT the file"<<endl;
	cout<<"Enter 2 to DECRYPT the file:"<<endl;
	cout<<"option:"<<endl;
	cin>>option;

	switch(option)
	{
		case 1:
		cout<<"Enter the filename to be encrypted:"<<endl;
		cin>>encfile;
		cout<<"Enter the new filename:"<<endl;
		cin>>new_encfile;
		Encrypt(encfile,new_encfile);
		cout<<"ur file is encrypted now:"<<endl;
		break;

		case 2:
		cout<<"Enter the filename to be decrypted:"<<endl;
		cin>>decfile;
		cout<<"Enter the new filename:"<<endl;
		cin>>new_decfile;
		Decrypt(decfile,new_decfile);
		cout<<"ur file is decrypted now:"<<endl;
		break;
	}

	getch();
}
*/

Thanks And Regards.
kawal

Dani AI

Generated

Short answer: compress first, then encrypt. As pointed out, encryption removes the repeating patterns that compressors need, so compressing encrypted data usually gives no benefit. Also avoid the home-rolled Caesar approach in the original post — it is not secure. Use a well-tested compression library (zlib/LZMA) and a real authenticated cipher (AES-GCM or ChaCha20-Poly1305) with a proper key-derivation function.

A safe file workflow (high level):

  1. Compress the plaintext (zlib, gzip, LZMA/7z) — streaming if the file is large.
  2. Encrypt the compressed bytes using an AEAD mode (so you get confidentiality + integrity). Generate a fresh random nonce/IV per file.
  3. Write a small header before the ciphertext that includes algorithm IDs, nonce/IV, and any needed parameters (KDF salt, compression method). Keep the authentication tag with the ciphertext.
  4. Reverse those steps to decrypt: read header → derive key → decrypt and verify → decompress.

Practical notes and fixes for ’s C++ approach:

  • Don’t use while(!fin.eof()); use while (fin.read(...)) or while (fin.get(byte)).
  • Use unsigned char for byte arithmetic to avoid sign-extension when adding/subtracting.
  • For passphrases, use a KDF (PBKDF2/Argon2) — do not use raw passphrase bytes as the key.
  • Use streaming APIs: feed compressor output into the cipher in blocks so you never need the whole file in memory.

Pseudocode (streaming, simplified):

open infile
open outfile
write header (algo, nonce, salt)
init compressor
init AEAD with key+nonce
while read chunk from infile:
  compressed = compressor.compress(chunk)
  out = AEAD.encrypt_update(compressed)
  write out to outfile
final = compressor.finish()
write AEAD.encrypt_final(final) and auth tag

If a file does not shrink after compression it is likely already compressed (images, zip). For library choices see well-known options (zlib, LZMA, OpenSSL/libsodium) and prefer libsodium or a modern AEAD mode for correct, easy-to-use encryption.

Recommended Answers

All 6 Replies

File compression/decompression is a pretty complex task. Here are some free libraries.

File compression/decompression is a pretty complex task.

For that matter, so is encryption. What you're doing barely qualifies; it's a Caesar cipher, which provides no real security. It's fine as a quick and easy substitute in a homework assignment, but you'd never use it in the "real" world.

I want to compress the file encrypted and then decrypt the same original file.

I recommend that you compress first, encrypt second. Encryption usually hides the patterns and structure in data that make compression worthwhile.

Of course, the Caesar cipher you're currently using should compress just as well as the original data, precisely because of one of its weaknesses--it doesn't hide patterns and structure.

How should I proceed to compress and decompress.

sir i am doing mca project in networking ...concept is compress the encrypted file and then decompress and decrypt the same original file..
the file which may be text, image format or any combination which will be implemented in java sir...

which is the best algorithm for compression and encryption the bulk of data...

i need related title for my project sir.........

sir i am doing mca project in networking ...concept is compress the encrypted file and then decompress and decrypt the same original file..
the file which may be text, image format or any combination which will be implemented in java sir...

which is the best algorithm for compression and encryption the bulk of data...

i need related title for my project sir.........

Not enough you hijacked someone else thread in different programming language you are even lazy to ask others to do your work. Use google to search for relevant topics and come back when you have some specific problem, not general query that can be solved by using online search...

You could use one of those open source compression libraries. Last time I needed compression, I used the LZMA SDK.

And with encryption, you haven't specified what sort of encryption are you looking for. The strongest known encryption is AES. There are a few C++ implementations of the said encryption scheme, which you could find out with a Google search.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.