openssl/des.h

Hello,
I want to create a program that encrypts a text file using DES. I am using the openssl/des.h library but I got stuck and I can't move forward anymore. Here is the code I got so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/des.h>
using namespace std;

int main()
{
  ifstream ptxt;
  ofstream ctxt;
  DES_key_schedule skey;
  DES_cblock  buffer;
  DES_cblock output;
   
  ptxt.open("plain.txt");
  ctxt.open("cipher.txt");

  if(ptxt.is_open())
    {
      while (ptxt.good())
        {
          ptxt >> buffer;
        }
      ptxt.close();
    }
  else
    cout << "Unable to open file\n";
  
  DES_ecb_encrypt (buffer, output, skey, 1);
  if(ctxt.is_open())
    {
      while(ctxt.good())
        {
          ctxt << output<< "\n";
        }
    }


I would really apreciate some help on this. Thanks!
What problem are you hitting?

What are the errors?

Andy
Basically,
I don't know how to use the function call DES_ecb_encrypt. I have read the description and it says to use
1
2
 void DES_ecb_encrypt(const_DES_cblock *input,DES_cblock *output,
		     DES_key_schedule *ks,int enc); 

but I don't know how to create all this variables. I don't know if I have declared them the right way. Thanks for the help.
The code you posted above have a number of problems in addition to the missing SSL calls. If you want to use your own loop, you need to first fix your code so it can copy a plain text file before adapting it to encrypt the data.

If you are not set on ECB mode, then this simple sample might help.

DES encryption with OpenSSL: example in C
http://www.codealias.info/technotes/des_encryption_using_openssl_a_simple_example
Topic archived. No new replies allowed.