I am working on trying to encrypt a text file via RC4 with a cpp file that I wrote with openssl/rc4 headers, and then decrypt via the command line to show that my implementation is correct.
My terminal command for the file is below, and the cpp file is below it, along with the terminal compile command I used for it.
There barely seems to be any information about this anywhere online, outside of some vague youtube videos that explain how the RC4 cypher works(which I already know). I can't find anything in the man pages to explain the details of the openssl implementation.
Any pointers on why my cpp file isn't decrypting to the original content would be much appreciated. I am tearing my hair out over here trying to figure this out. Thanks in advance.
(and yes, I understand there are vulnerabilities that make RC4 less of a good option, but right now, I just want to understand how these work)
command line encrypt:
openssl rc4-40 -k PASSword123 -in /home/chris/Desktop/test.txt -out /home/chris/Desktop/ssloutput.txt -p -nosalt
cpp file compilation:
g++ rc4.cpp -o rc4 -lssl -lcrypto
cpp file:
#include <openssl/rc4.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("/home/chris/Desktop/ssloutput.txt", O_RDWR);
unsigned char keygen[12] = "PASSword123";
RC4_KEY key;
struct stat st;
fstat(fd, &st);
int size = st.st_size;
unsigned char* fileIn;
fileIn = (unsigned char*) calloc(size, sizeof(char));
pread(fd, fileIn, size, 0);
unsigned char *fileOut = (unsigned char*)malloc(size);
RC4_set_key(&key, 16, keygen);
RC4(&key, size, fileIn, fileOut);
close(fd);
int fd2 = open("/home/chris/Desktop/rc4output.txt", O_RDWR | O_CREAT);
pwrite(fd2, fileOut, size, 0);
close(fd2);
free(fileIn);
free(fileOut);
return 0;
}