【问题标题】:RC4 implementation bugRC4 实现错误
【发布时间】:2017-04-10 23:57:45
【问题描述】:

我正在使用 openssl 和 C++ 实现 RC4 到 S3fs。我正在尝试加密一个包含字符串的简单文本文件:

我是一个样本

我不断收到分段错误,但我不知道为什么。任何人都可以对我的问题有所了解吗?下面是结果,下面是代码。

文件 1 已打开
文件 2 已打开
分段错误(核心转储)

#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;

【问题讨论】:

    标签: c++ openssl rc4-cipher


    【解决方案1】:
    unsigned char * buf;
    

    你还没有初始化这个变量。

    【讨论】:

    • 有没有办法让我初始化一些空白值,以便它被foo &gt;&gt; buf覆盖?
    • 错误,unsigned char buf[1024];?或者它应该是什么尺寸?
    • 好的,我试试看。
    • 问题已解决!文本文件仍然没有加密,但这是一个不同的问题。也许缺少组件或其他东西。我会继续努力。谢谢你帮我解决这个问题! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 2018-05-26
    • 1970-01-01
    相关资源
    最近更新 更多