Thank you for replying "Spiritovod"
I have made few more changes in code also found aft xor decrypted file,
It have zlib compression.
Providing both code for your reference.
Kindly help me to make quick bms script.
Xor Enc Dec code :
Code: Select all
#define CHUNK_SIZE 4096 // 4KB
#define XOR_KEY 0x90 // Key in hexadecimal format
void xor_encrypt(const char* in_file, const char* out_file) {
int in_fd = open(in_file, O_RDONLY);
if (in_fd == -1) {
perror("open");
exit(1);
}
int out_fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (out_fd == -1) {
perror("open");
close(in_fd);
exit(1);
}
unsigned char buffer[CHUNK_SIZE] = {0};
ssize_t bytes_read = 0;
while ((bytes_read = read(in_fd, buffer, CHUNK_SIZE)) > 0) {
for (size_t i = 0; i < bytes_read; i++) {
buffer[i] ^= XOR_KEY;
}
if (write(out_fd, buffer, bytes_read) != bytes_read) {
perror("write");
close(in_fd);
close(out_fd);
exit(1);
}
}
if (bytes_read == -1) {
perror("read");
close(in_fd);
close(out_fd);
exit(1);
}
close(in_fd);
close(out_fd);
}
int main(int argc, char* argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
return 1;
}
xor_encrypt(argv[1], argv[2]);
return 0;
}
Zlib compression code
Code: Select all
const int CHUNK_SIZE = 1024;
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " [input file] [output file]" << std::endl;
return 1;
}
// Open input and output files
std::ifstream input_file(argv[1], std::ios::binary);
std::ofstream output_file(argv[2], std::ios::binary);
// Initialize zlib structures
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
deflateInit(&strm, Z_DEFAULT_COMPRESSION);
// Buffer for the input and compressed data
char input_data[CHUNK_SIZE];
char compressed_data[CHUNK_SIZE];
// Compress the input data chunk by chunk
do {
input_file.read(input_data, CHUNK_SIZE);
int bytes_read = input_file.gcount();
strm.avail_in = bytes_read;
strm.next_in = (Bytef *)input_data;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = (Bytef *)compressed_data;
deflate(&strm, Z_NO_FLUSH);
output_file.write(compressed_data, CHUNK_SIZE - strm.avail_out);
} while (strm.avail_out == 0);
} while (input_file);
// Finish compression
strm.avail_in = 0;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = (Bytef *)compressed_data;
deflate(&strm, Z_FINISH);
output_file.write(compressed_data, CHUNK_SIZE - strm.avail_out);
} while (strm.avail_out == 0);
// Clean up and return
deflateEnd(&strm);
return 0;
}
Zlib decompression code
Code: Select all
const int CHUNK_SIZE = 1024;
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " [input file] [output file]" << std::endl;
return 1;
}
// Open input and output files
std::ifstream input_file(argv[1], std::ios::binary);
std::ofstream output_file(argv[2], std::ios::binary);
// Initialize zlib structures
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
inflateInit(&strm);
// Buffer for the input and decompressed data
char input_data[CHUNK_SIZE];
char decompressed_data[CHUNK_SIZE];
// Decompress the input data chunk by chunk
do {
input_file.read(input_data, CHUNK_SIZE);
int bytes_read = input_file.gcount();
strm.avail_in = bytes_read;
strm.next_in = (Bytef *)input_data;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = (Bytef *)decompressed_data;
inflate(&strm, Z_NO_FLUSH);
output_file.write(decompressed_data, CHUNK_SIZE - strm.avail_out);
} while (strm.avail_out == 0);
} while (input_file);
// Finish decompression
strm.avail_in = 0;
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = (Bytef *)decompressed_data;
inflate(&strm, Z_FINISH);
output_file.write(decompressed_data, CHUNK_SIZE - strm.avail_out);
} while (strm.avail_out == 0);
// Clean up and return
inflateEnd(&strm);
return 0;
}