-1

I am working on implementation for RC4 to S3fs using openssl with C++. And I am trying to encrypt a simple text file that contains the string:

I am a sample

I keep getting segementation faults, but I'm not sure why. Can anyone shed some light on my issue? Below is the result and below that is the code.

File 1 opened
File 2 opened
Segmentation fault (core dumped)

#include<iostream>
#include "rc4_enc.c"
#include "rc4_skey.c"
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#include "rc4.h"
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

int main(){
//cout << "I work \n"; // Test for compilation
ifstream mytext;
ifstream result;

// apply string stream
 stringstream foo;
 stringstream baz;

mytext.open("sample.txt", ios::binary);
 if(!mytext.good()){
     cout << "File 1 not opened \n";
     return 1;
 }
 else{
     cout << "File 1 opened \n";
     foo << mytext.rdbuf();
 }
 result.open("result.txt", ios::binary);
  if(!result.good()){
     cout << "File 2 not opened \n";
     return 1;
  }
  else{
     cout << "File 2 opened \n";
     baz << result.rdbuf();
  }
char source[6] = "tacos";
//const unsigned char source[] = {'t','a','c','o'};
int len = strlen(source);
RC4_KEY mykey;
unsigned char * buf;
foo >> buf;
RC4_set_key(&mykey,len, buf); // causes segfault?
// make a second buffer and cast it.
unsigned char * muf;
baz >> muf;
RC4(&mykey, sizeof(mytext),buf,muf);

    return 0;
Andrew Gaul
  • 2,296
  • 1
  • 12
  • 19
Callat
  • 2,928
  • 5
  • 30
  • 47

1 Answers1

1
unsigned char * buf;

You have not initialized this variable.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Is there a way for me to initialize to some blank value so that it gets overwritten by `foo >> buf`? – Callat Apr 11 '17 at 01:57
  • 1
    Err, `unsigned char buf[1024];`? or whatever size it's supposed to be? – user207421 Apr 11 '17 at 02:11
  • Ok I'll try that. – Callat Apr 11 '17 at 02:21
  • Problem resolved! Text file still isn't encrypting but that's a different issue. Perhaps a missing component or something. I'll continue working on it. Thanks for getting me around that problem! :D – Callat Apr 11 '17 at 02:27