3DES CBC encryption decryption

Hi,

I have to build a application that can do encryption and decryption using Triple Des CBC mode. Can anybody give me a sample code for this. I trid with google. But it's not poroperly suited.

Thank you.
Hi,

Thank you. I tried with open ssl. But It's not working properly.

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
37
38
39
40
#include <openssl/des.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	
	DES_cblock cb1 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
	DES_cblock cb2 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
	DES_cblock cb3 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };

	DES_key_schedule ks1;
	DES_key_schedule ks2;
	DES_key_schedule ks3;

	DES_cblock cblock = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

	char string[] = "I am a software developer";

	printf("Plain Text : %s\n",string);

	char* cipher = (char *) malloc(32);
	char* text = (char *) malloc(strlen(string));

	DES_set_odd_parity(&cblock);

	if (DES_set_key_checked(&cb1, &ks1) || DES_set_key_checked(&cb2, &ks2) || DES_set_key_checked(&cb3, &ks3)) {
		printf("Key error, exiting ....\n");
		return 1;
	}

	DES_ede3_cbc_encrypt(string, cipher, strlen(string), &ks1, &ks2, &ks3,&cblock, 1);

	printf("Encrypted : %s+\n",cipher);

	DES_ede3_cbc_encrypt(cipher, text, strlen(cipher), &ks1, &ks2, &ks3, &cblock,0);

	printf("Decrypted : %s\n",text);
}


Oputput :

1
2
3
Plain Text : I am a software developer
Encrypted : 0}�~i���:����u�8O<`�l���s�[q+
Decrypted : $���R�7oftware developer


Can you please help me to do this.

Thank you.
This works but be careful with the sizes of your arrays. Didn't check to much if you were going outside of your array boundaries.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <openssl/des.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
   DES_cblock cb1 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
   DES_cblock cb2 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };
   DES_cblock cb3 = { 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE };

   DES_key_schedule ks1,ks2,ks3;

   DES_cblock cblock = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

   char string[] = "I am a software developer";
   // ---------------------------------------------
   // I use sizeof instead of strlen because I want
   // to count the '\0' at the end, strlen would
   // not count it
   int stringLen(sizeof(string));

   printf("Plain Text : %s\n",string);

   char* cipher(new char[32]);
   char* text(new char[stringLen]);
   memset(cipher,0,32);
   memset(text,0,stringLen);

   DES_set_odd_parity(&cblock);

   if (DES_set_key_checked(&cb1, &ks1) ||
        DES_set_key_checked(&cb2, &ks2) ||
         DES_set_key_checked(&cb3, &ks3)) {
      printf("Key error, exiting ....\n");
      return 1;
   }

   DES_ede3_cbc_encrypt((const unsigned char*)string,
                         (unsigned char*)cipher,
                          stringLen, &ks1, &ks2, &ks3,
                                  &cblock, DES_ENCRYPT);
   printf("Encrypted : %32.32s\n",cipher);

   //-----------------------------------------------
   // You need to start with the same cblock value
   memset(cblock,0,sizeof(DES_cblock));
   DES_set_odd_parity(&cblock);

   //-----------------------------------------------
   // I think you need to use 32 for the cipher len.
   // You can't use strlen(cipher) because if there
   // is a 0x00 in the middle of the cipher strlen
   // will stop there and the length would be short
   DES_ede3_cbc_encrypt((const unsigned char*)cipher,
                         (unsigned char*)text,
                          32, &ks1, &ks2, &ks3,
                                     &cblock,DES_DECRYPT);
   printf("Decrypted : %s\n",text);
}
Plain Text : I am a software developer
Encrypted : 0}¶~iúÌ:¾Èu8O`¾l³×âs¢q
Decrypted : I am a software developer
Last edited on
Hi,

Thank you very much..
Topic archived. No new replies allowed.