Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions 3des/3des.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <openssl/des.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
DES_cblock cb1 = {0xFA, 0x5E, 0xAE, 0xBC, 0x19, 0x12, 0x13, 0xA1};
DES_cblock cb2 = {0xAF, 0xA4, 0xAE, 0xCB, 0xED, 0x45, 0x13, 0x1E};
DES_cblock cb3 = {0xB3, 0x24, 0xAE, 0xDE, 0xAD, 0x98, 0x31, 0x4E};

DES_key_schedule ks1, ks2, ks3;

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

unsigned char *plainText = argv[1];
int plainTextLen = strlen(plainText);
printf("Plain Text : %s\n", plainText);

char *cipher[plainTextLen];
char *text[plainTextLen];
memset(cipher, 0, plainTextLen);
memset(text, 0, plainTextLen);

DES_set_odd_parity(&cblock);
DES_set_odd_parity(&cb1);
DES_set_odd_parity(&cb2);
DES_set_odd_parity(&cb3);

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 *)plainText,
(unsigned char *)cipher,
plainTextLen, &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);
}
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ bin/shs: $(wildcard shs/*.c)
@echo "+ $@"
@$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

all: bin/3des
bin/3des:
@echo "+ $@"
@$(CC) 3des/3des.c -o $@ $(CFLAGS) $(LDFLAGS)

all: bin/sym-key-gen
bin/sym-key-gen:
@echo "+ $@"
Expand Down