2

I made codes in c++, for encryption and decryption. first code creates an output in vector and then write it in a file by using fwrite, and the second reads that output from the first by using fread. Here is the snippet of my codes :

1st code :

.....
string a;
vector<long long int> c;

cout << "message to be encrypted = ";
cin >> a;   
cout << endl;

cout << "Encrypted message : ";
for (i=0;i<a.size();i++) 
{
    x=(int)a.at(i);
    cout << x << " ";
    c.push_back(powerMod(x,e,n));
}

for (i=0;i<c.size();i++) 
{
    //cout << char(c.at(i));
}
cout << endl;

//Write ciphertext c to a file
FILE * pWrite;
pWrite = fopen ("ciphertext", "w");
fwrite (&c , sizeof(c), 1, pWrite);
fclose (pWrite);

The output is :

message to be encrypted = test
Encrypted message : 116 101 115 116 

And then the 2nd code :

....
//Read Ciphertext from ciphertext
FILE * pRead2;
pRead2 = fopen ("ciphertext", "r");
fread (&c , sizeof(c), 1, pRead2);
//cout << "ciphertext is " << c << endl;

// Decryption
cout << "Decrypted message : ";
for (i=0;i<c.size();i++) 
{
    cout << powerMod(c.at(i),d,n) << " " ;
}
cout << endl;

But it return :

Segmentation Fault(Core Dumped)

I appreciate any help, since I don't know where is the problem, in the fwrite or in the fread. But I think the problem is in the 2nd, when it tries to read the ciphertext (which is a vector), because if I erase that lines, the program is running perfectly, but without decrypting the message.

Thanks.

4

1 回答 1

6

这是因为您写入了指向向量对象实例的指针,而不是实际的向量数据。利用

fwrite (&c[0], sizeof(vector<long long int>::value_type), c.size(), pWrite);

还要记住sizeof(c)返回向量对象实例的大小,而不是向量中的项目数。

阅读矢量时您也遇到了类似的问题。你必须在一个循环中一个一个地做,再次将项目推到向量上。


如果您学会使用C++ I/O 流库和一些不错的标准算法并使用迭代器,则使用 C++ 有更简单的方法。

要将向量写入文件:

std::ofstream os{"ciphertext", std::ios::out};

std::copy(std::begin(c), std::end(c),
          std::ostream_iterator<long long int>(os));

并从文件中读取:

std::ifstream is{"ciphertext", std::ios::in};

std::copy(std::istream_iterator<long long int>(is),
          std::istream_iterator<long long int>(),
          std::back_inserter(c));

实际上有一种更简单的方法可以从文件中读取到向量中:

std::ifstream is{"ciphertext", std::ios::in};

std::vector<long long int> c(std::istream_iterator<long long int>(is),
                             std::istream_iterator<long long int>());

这依赖于将两个迭代器作为参数的std::vector构造函数。


如果您不想使用文本文件,而是二进制文件,那么不幸的是,您必须手动循环并写入/读取数据,即您必须手动执行std::copy对您有用的操作。

像这样写数据:

std::ofstream os{"ciphertext", std::ios::out | std::ios::binary};

for (const auto& value : c)
    os.write(reinterpret_cast<const char*>(&value), sizeof(value));

像这样阅读它:

std::ifstream is{"ciphertext", std::ios::in | std::ios::binary};

long long int value:
while (is.read(reinterpret_cast<char*>(&value), sizeof(value)))
    c.push_back(value);

如果您没有 C++11基于范围的for循环(在上面的编写示例中使用),请使用普通的经典迭代for循环:

std::vector<long long int>::const_iterator i;
for (i = c.begin(); i != c.end(); ++i)
    os.write(reinterpret_cast<const char*>(&(*i)), sizeof(*i));
于 2013-08-02T10:53:32.600 回答