Triple DES Cryptage / Decryptage

Triple DES Cryptage / Decryptage

Messagepar Stephane Maillard » 19 Juil 2005 à 11:32

Exemple d'utilisation :[syntax="ls"]Declare Function DESEncrypt Lib "desdll.dll" (ByVal strIn As String, ByVal key As String, ByVal strOut As String) As Integer
Declare Function DESDecrypt Lib "desdll.dll" (ByVal strIn As String, ByVal key As String, ByVal strOut As String) As Integer
Declare Function TDESEncrypt Lib "desdll.dll" (ByVal strIn As String, ByVal key As String, ByVal strOut As String) As Integer
Declare Function TDESDecrypt Lib "desdll.dll" (ByVal strIn As String, ByVal key As String, ByVal strOut As String) As Integer


Private Function decrypt(ByVal source as string, ByVal password as String) as String
' the string that will be returned must be sized to 1024
Dim strResult As String * 1024
strResult = String$(1024, 0)
TDESDecrypt( source, password, strResult)
decrypt = strResult
End Sub

Private Function encrypt(ByVal source as string, ByVal password as String) as String
' the string that will be returned must be sized to 1024
Dim strResult As String * 1024
strResult = String$(1024, 0)
TDESencrypt = TDESEncrypt( source, password, strResult)
encrypt = strResult
End Sub[/syntax]
Des.h[syntax="c"]
/*****************************************************************************/
/* PROGRAM : DESBC.h */
/* VERSION : 2.0 */
/* COMPLIER : Borland C++ Builder Ver 5.0 */
/* PROGRAMMER : Hong. Jae. Jum */
/* DATE : 2000.04.07 */
/*****************************************************************************/




/*******************************************************************************/
/* Function : DESencrypt */
/* Description : Single Des */
/* orginal_msg : ¾Ïȣȭ ÇÒ Data */
/* encrypt_msg : ¾ÏȣȭµÈ Data ( return value ) */
/* en_key : ¾ÏȣȭÇÒ ±âº» Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
extern "C" __declspec(dllexport) int DESencrypt(char *orginal_msg,
char *encrypt_msg,
char *en_key);


/*******************************************************************************/
/* Function : DESdecrypt */
/* Descryption : Single Des */
/* sub_msg : ¾Ïȣȭ µÈ Data */
/* decrypt_msg : ¾Ïȣȭ¸¦ Ǭ Data ( return value ) */
/* en_key : ¾Ïȣȭ µÈ ±âº» Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
extern "C" __declspec(dllexport) int DESdecrypt(char *sub_msg,
char *decrypt_msg,
char *en_key);


/*******************************************************************************/
/* Function : TripleDESencrypt */
/* Description : Triple Des */
/* orginal_msg : ¾Ïȣȭ ÇÒ Data */
/* encrypt_msg : ¾ÏȣȭµÈ Data ( return value ) */
/* en_key : ¾ÏȣȭÇÒ ±âº» encrypt key */
/* dec_key : ¾ÏȣȭÇÒ ±âº» decrypt Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
extern "C" __declspec(dllexport) int TripleDESencrypt(char *orginal_msg,
char *encrypt_msg,
char *en_key, char *dec_key);


/*******************************************************************************/
/* Function : TripleDESdecrypt */
/* Descryption : Triple Des */
/* sub_msg : ¾Ïȣȭ µÈ Data */
/* decrypt_msg : ¾Ïȣȭ¸¦ Ǭ Data ( return value ) */
/* dec_key : ¾Ïȣȭ µÈ ±âº» Key (decryption key) */
/* en_key : ¾Ïȣȭ µÈ ±âº» Key (encryption key) */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
extern "C" __declspec(dllexport) int TripleDESdecrypt(char *sub_msg,
char *decrypt_msg,
char *dec_key, char *en_key);[/syntax]
Des.cpp[syntax="c"]/*****************************************************************************/
/* PROGRAM : DES.cpp */
/* VERSION : 2.0 */
/* COMPLIER : C++ Builder Ver5.0 */
/* PROGRAMMER : Hong. Jae. Jum */
/* DATE : 2000.03.01 */
/*****************************************************************************/

//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <sys\types.h> /* delete this line for Turbo C */
#include <sys\stat.h>

#include "Des.h"

char box_msg[1024];

HWND ghwnd;

/*****************************************************************************/
/* PROTOTYE */
/*****************************************************************************/
int local_encrypt(char *orginal_msg, char *encrypt_msg, UINT wk_length, char *en_key);
int local_decrypt(char *sub_msg, char *decrypt_msg, UINT wk_length, char *en_key);


void BCD_to_str(char * , char * , int );
void str_to_BCD(char * , char * , int );
void new_encrypt(char *inputmsg, char *outmsg, char *inputkey,
char *nKEY, char *nR, char *nL, char *nE,
char *nA , char *nB, char *nP);

void new_decrypt(char *inputmsg, char *outmsg, char *inputkey,
char *nKEY, char *nR, char *nL, char *nE,
char *nA, char *nB, char *nP);
void new_make_R_and_L_zero(char *inputmsg, char *nR, char *nL);
void new_bit_permutation(char *in_data,char *out_data,int data_length,char *table_name);
void new_expand_R(char *nR, char *nE);
void new_modulo_2_add(char *inputkey, char *nKey,
char *nE, char *nA, int round);
void new_make_PB(char *nR, char *nL, char *nB, char *nP, char *nA);
void new_look_up_S_box(char *nA, char *nB);
char new_get_S_box(int round, char c);
void new_inverse_IP(char *nR, char *nL, char *outmsg);

/******************************************************************************
* *
* KEY CONVERSION TABLE FOR EACH ROUND *
* *
******************************************************************************/

char key_tbl[16][49] = {{10,51,34,60,49,17,33,57, 2, 9,19,42,
3,35,26,25,44,58,59, 1,36,27,18,41,
22,28,39,54,37, 4,47,30, 5,53,23,29,
61,21,38,63,15,20,45,14,13,62,55,31},
{ 2,43,26,52,41, 9,25,49,59, 1,11,34,
60,27,18,17,36,50,51,58,57,19,10,33,
14,20,31,46,29,63,39,22,28,45,15,21,
53,13,30,55, 7,12,37, 6, 5,54,47,23},
{51,27,10,36,25,58, 9,33,43,50,60,18,
44,11, 2, 1,49,34,35,42,41, 3,59,17,
61, 4,15,30,13,47,23, 6,12,29,62, 5,
37,28,14,39,54,63,21,53,20,38,31, 7},
{35,11,59,49, 9,42,58,17,27,34,44, 2,
57,60,51,50,33,18,19,26,25,52,43, 1,
45,55,62,14,28,31, 7,53,63,13,46,20,
21,12,61,23,38,47, 5,37, 4,22,15,54},
{19,60,43,33,58,26,42, 1,11,18,57,51,
41,44,35,34,17, 2, 3,10, 9,36,27,50,
29,39,46,61,12,15,54,37,47,28,30, 4,
5,63,45, 7,22,31,20,21,55, 6,62,38},
{ 3,44,27,17,42,10,26,50,60, 2,41,35,
25,57,19,18, 1,51,52,59,58,49,11,34,
13,23,30,45,63,62,38,21,31,12,14,55,
20,47,29,54, 6,15, 4, 5,39,53,46,22},
{52,57,11, 1,26,59,10,34,44,51,25,19,
9,41, 3, 2,50,35,36,43,42,33,60,18,
28, 7,14,29,47,46,22, 5,15,63,61,39,
4,31,13,38,53,62,55,20,23,37,30, 6},
{36,41,60,50,10,43,59,18,57,35, 9, 3,
58,25,52,51,34,19,49,27,26,17,44, 2,
12,54,61,13,31,30, 6,20,62,47,45,23,
55,15,28,22,37,46,39, 4, 7,21,14,53},
{57,33,52,42, 2,35,51,10,49,27, 1,60,
50,17,44,43,26,11,41,19,18, 9,36,59,
4,46,53, 5,23,22,61,12,54,39,37,15,
47, 7,20,14,29,38,31,63,62,13, 6,45},
{41,17,36,26,51,19,35,59,33,11,50,44,
34, 1,57,27,10,60,25, 3, 2,58,49,43,
55,30,37,20, 7, 6,45,63,38,23,21,62,
31,54, 4,61,13,22,15,47,46,28,53,29},
{25, 1,49,10,35, 3,19,43,17,60,34,57,
18,50,41,11,59,44, 9,52,51,42,33,27,
39,14,21, 4,54,53,29,47,22, 7, 5,46,
15,38,55,45,28, 6,62,31,30,12,37,13},
{ 9,50,33,59,19,52, 3,27, 1,44,18,41,
2,34,25,60,43,57,58,36,35,26,17,11,
23,61, 5,55,38,37,13,31, 6,54,20,30,
62,22,39,29,12,53,46,15,14,63,21,28},
{58,34,17,43, 3,36,52,11,50,57, 2,25,
51,18, 9,44,27,41,42,49,19,10, 1,60,
7,45,20,39,22,21,28,15,53,38, 4,14,
46, 6,23,13,63,37,30,62,61,47, 5,12},
{42,18, 1,27,52,49,36,60,34,41,51, 9,
35, 2,58,57,11,25,26,33, 3,59,50,44,
54,29, 4,23, 6, 5,12,62,37,22,55,61,
30,53, 7,28,47,21,14,46,45,31,20,63},
{26, 2,50,11,36,33,49,44,18,25,35,58,
19,51,42,41,60, 9,10,17,52,43,34,57,
38,13,55, 7,53,20,63,46,21, 6,39,45,
14,37,54,12,31, 5,61,30,29,15, 4,47},
{18,59,42, 3,57,25,41,36,10,17,27,50,
11,43,34,33,52, 1, 2, 9,44,35,26,49,
30, 5,47,62,45,12,55,38,13,61,31,37,
6,29,46, 4,23,28,53,22,21, 7,63,39}
};

/******************************************************************************
* *
* CONVERSION TABLE FOR MAKING L(0) FROM ORIGINAL MESSAGE *
* *
******************************************************************************/
char L_tbl[33] = { 58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8};
/******************************************************************************
* *
* CONVERSION TABLE FOR MAKING R(0) FROM ORIGINAL MESSAGE *
* *
******************************************************************************/
char R_tbl[33] = { 57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,
61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7};
/******************************************************************************
* *
* CONVERSION TABLE FOR EXPANDING R(i) *
* *
******************************************************************************/
char E_R_tbl[49]= { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10,11,
12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,
22,23,24,25,24,25,26,27,28,29,28,29,30,31,32, 1};
/******************************************************************************
* *
* CONVERSION TABLE FOR PERMUTATION P(B) *
* *
******************************************************************************/
char P_tbl[33] = { 16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25};
/******************************************************************************
* *
* CONVERSION TABLE FOR INVERSE INITIAL PERMUTATION *
* *
******************************************************************************/
char IP_tbl[65] = { 40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,
38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,
36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,
34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25};

/******************************************************************************
* *
* S - BOX *
* *
******************************************************************************/
char S_box[32][17] = {
{14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7},
{ 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8},
{ 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0},
{15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13},
{15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10},
{ 3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5},
{ 0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15},
{13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9},
{10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8},
{13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1},
{13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7},
{ 1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12},
{ 7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15},
{13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9},
{10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4},
{ 3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14},
{ 2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9},
{14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6},
{ 4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14},
{11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3},
{12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11},
{10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8},
{ 9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6},
{ 4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13},
{ 4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1},
{13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6},
{ 1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2},
{ 6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12},
{13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7},
{ 1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2},
{ 7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8},
{ 2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11}
};
/******************************************************************************
* *
* GENERAL PURPOSE BUFFERS *
* *
******************************************************************************/
char conv_TBL[17] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

char swap_TBL[17] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, 0x3E, 0x3F};


#pragma hdrstop

USERES("DesBC.res");
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
//---------------------------------------------------------------------------
/*******************************************************************************/
/* Function : DESencrypt */
/* Description : Single Des */
/* orginal_msg : ¾Ïȣȭ ÇÒ Data */
/* encrypt_msg : ¾ÏȣȭµÈ Data ( return value ) */
/* en_key : ¾ÏȣȭÇÒ ±âº» Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
int DESencrypt(char *orginal_msg, char *encrypt_msg, char *en_key)
{

int i, lm;
char two_char[3];
char input_key[17], input_msg[17], org_msg[17], out_msg[17];
char NKEY[9];

char NR[4]; /* result of R(i) */
char NL[4]; /* result of L(i) */
char NE[6]; /* result of E(R(i-1)) */
char NA[6]; /* result of E(R(i-1)) XOR K(i) */
char NB[6]; /* result of looking up S-BOX */
char NP[6]; /* result of P(B) */


memset(input_key, 0x00, sizeof(input_key));
memset(input_msg, 0x00, sizeof(input_msg));
memset(org_msg, 0x00, sizeof(org_msg));
memset(out_msg, 0x00, sizeof(out_msg));

memset(NKEY, 0x00, sizeof(NKEY));
memset(NR, 0x00, sizeof(NR));
memset(NL, 0x00, sizeof(NL));
memset(NE, 0x00, sizeof(NE));
memset(NA, 0x00, sizeof(NA));
memset(NB, 0x00, sizeof(NB));
memset(NP, 0x00, sizeof(NP));

if (strlen(orginal_msg) < 16) return 1;
if (strlen(en_key) < 16) return 2;

memset(encrypt_msg, 0x00, 16);

strncpy(input_key, &en_key[0], 16);

for (i=0;i<16;i++) {
if (input_key[i] > '@') input_key[i] = input_key[i] - 7;
}
for (i=0;i<8;i++ ) {
input_key[i] = ((input_key[i*2] << 4) & 0x00f0) |
(input_key[i*2+1] & 0x000f);
}

strncpy(input_msg, orginal_msg, 16);
for (i=0;i<16;i++) {
if (input_msg[i] > '@') input_msg[i] = input_msg[i] - 7;
}
for (i=0;i<8;i++ ) {
input_msg[i] = ((input_msg[i*2] << 4) & 0x00f0) |
(input_msg[i*2+1] & 0x000f);
}
for (i=0;i<3;i++) {
new_encrypt(input_msg, out_msg, input_key,
NKEY, NR, NL, NE, NA , NB, NP);
}
for (i=0; i <8; i++)
{
lm = (int) out_msg[i] & 0x00ff;
sprintf(two_char,"%02X", lm);
org_msg[(i*2)] = two_char[0];
org_msg[(i*2)+1] = two_char[1];
org_msg[(i*2)+2] = 0x00;
}
strncpy(encrypt_msg, org_msg, 16);
return 0;
}

/*******************************************************************************/
/* Function : DESdecrypt */
/* Descryption : Single Des */
/* sub_msg : ¾Ïȣȭ µÈ Data */
/* decrypt_msg : ¾Ïȣȭ¸¦ Ǭ Data ( return value ) */
/* en_key : ¾Ïȣȭ µÈ ±âº» Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
int DESdecrypt(char *sub_msg, char *decrypt_msg, char *en_key)
{

int i, lm, mm;
char two_char[3];
char input_key[17], input_msg[17], org_msg[17], out_msg[17];
char NKEY[9];

char NR[4]; /* result of R(i) */
char NL[4]; /* result of L(i) */
char NE[6]; /* result of E(R(i-1)) */
char NA[6]; /* result of E(R(i-1)) XOR K(i) */
char NB[6]; /* result of looking up S-BOX */
char NP[6]; /* result of P(B) */


memset(input_key, 0x00, sizeof(input_key));
memset(input_msg, 0x00, sizeof(input_msg));
memset(org_msg, 0x00, sizeof(org_msg));
memset(out_msg, 0x00, sizeof(out_msg));

memset(NKEY, 0x00, sizeof(NKEY));
memset(NR, 0x00, sizeof(NR));
memset(NL, 0x00, sizeof(NL));
memset(NE, 0x00, sizeof(NE));
memset(NA, 0x00, sizeof(NA));
memset(NB, 0x00, sizeof(NB));
memset(NP, 0x00, sizeof(NP));

if (strlen(sub_msg) < 16) return 1;
if (strlen(en_key) < 16) return 2;

memset(decrypt_msg, 0x00, 16);

strncpy(input_key, &en_key[0], 16);

for (i=0;i<16;i++) {
if (input_key[i] > '@') input_key[i] = input_key[i] - 7;
}
for (i=0;i<8;i++ ) {
input_key[i] = ((input_key[i*2] << 4) & 0x00f0) |
(input_key[i*2+1] & 0x000f);
}

strncpy(out_msg, sub_msg, 16);
out_msg[16] = 0x00;

for (i=0;i<16;i++) {
for (mm=0;mm<16; mm++){
if (out_msg[i] == conv_TBL[mm]) out_msg[i]=swap_TBL[mm];
}
}
for (i=0;i<8;i++ ) {
out_msg[i] = ((out_msg[i*2] << 4) & 0x00f0) |
(out_msg[i*2+1] & 0x000f);
}

new_decrypt(input_msg, out_msg, input_key,
NKEY, NR, NL, NE, NA , NB, NP);

for (i=0; i <8; i++)
{
lm = (int) out_msg[i] & 0x00ff;
sprintf(two_char,"%02X", lm);
org_msg[(i*2)] = two_char[0];
org_msg[(i*2)+1] = two_char[1];
org_msg[(i*2)+2] = 0x00;
}
strncpy(decrypt_msg, org_msg, 16);

return 0;
}
/*******************************************************************************/
/* Function : TripleDESencrypt */
/* Description : Triple Des */
/* orginal_msg : ¾Ïȣȭ ÇÒ Data */
/* encrypt_msg : ¾ÏȣȭµÈ Data ( return value ) */
/* en_key : ¾ÏȣȭÇÒ ±âº» encrypt key */
/* dec_key : ¾ÏȣȭÇÒ ±âº» decrypt Key */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
int TripleDESencrypt(char *orginal_msg, char *encrypt_msg,
char *en_key, char *dec_key)
{

char temp_decrypt[17];
char temp_encrypt[17];

if (strlen(orginal_msg) < 16) return 1;
if (strlen(en_key) < 16) return 2;
if (strlen(dec_key) < 16) return 2;

memset(encrypt_msg, 0x00, 16);

memset(temp_decrypt, 0x00, sizeof(temp_decrypt));
memset(temp_encrypt, 0x00, sizeof(temp_encrypt));

local_encrypt(orginal_msg, temp_encrypt, 16, en_key);
local_decrypt(temp_encrypt, temp_decrypt, 16, dec_key);
local_encrypt(temp_decrypt, encrypt_msg, 16, en_key);

return 0;

}
/*******************************************************************************/
/* Function : TripleDESdecrypt */
/* Descryption : Triple Des */
/* sub_msg : ¾Ïȣȭ µÈ Data */
/* decrypt_msg : ¾Ïȣȭ¸¦ Ǭ Data ( return value ) */
/* dec_key : ¾Ïȣȭ µÈ ±âº» Key (decryption key) */
/* en_key : ¾Ïȣȭ µÈ ±âº» Key (encryption key) */
/* Remark : Data Length Hexa String 16 Byte */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/*******************************************************************************/
int TripleDESdecrypt(char *sub_msg, char *decrypt_msg,
char *dec_key, char *en_key)
{


char temp_decrypt[17];
char temp_encrypt[17];

if (strlen(sub_msg) != 16) return 1;
if (strlen(dec_key) != 16) return 2;
if (strlen(en_key) != 16) return 2;

memset(decrypt_msg, 0x00, 16);

memset(temp_decrypt, 0x00, sizeof(temp_decrypt));
memset(temp_encrypt, 0x00, sizeof(temp_encrypt));

local_decrypt(sub_msg, temp_decrypt, 16, dec_key);
local_encrypt(temp_decrypt, temp_encrypt, 16, en_key);
local_decrypt(temp_encrypt, decrypt_msg, 16, dec_key);

return 0;
}

/*******************************************************************************/
/* Function : local_encrypt */
/* */
/* orginal_msg : ¾Ïȣȭ ÇÒ Data */
/* encrypt_msg : ¾ÏȣȭµÈ Data ( return value ) */
/* wk_length : ¾Ïȣȭ¸¦ ÇÒ DataÀÇ ±æÀÌ */
/* en_key : ¾ÏȣȭÇÒ ±âº» Key */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/* 3: length°¡ 16ÀÇ ¹è¼ö°¡ ¾Æ´Ô */
/*******************************************************************************/
int local_encrypt(char *orginal_msg, char *encrypt_msg, UINT wk_length, char *en_key)
{

int i, j, k, lm, length;
char two_char[3];
char input_key[17], input_msg[17], org_msg[17], out_msg[17];
char NKEY[9];

char NR[4]; /* result of R(i) */
char NL[4]; /* result of L(i) */
char NE[6]; /* result of E(R(i-1)) */
char NA[6]; /* result of E(R(i-1)) XOR K(i) */
char NB[6]; /* result of looking up S-BOX */
char NP[6]; /* result of P(B) */


memset(encrypt_msg, 0x00, wk_length+1);
memset(input_key, 0x00, sizeof(input_key));
memset(input_msg, 0x00, sizeof(input_msg));
memset(org_msg, 0x00, sizeof(org_msg));
memset(out_msg, 0x00, sizeof(out_msg));

memset(NKEY, 0x00, sizeof(NKEY));
memset(NR, 0x00, sizeof(NR));
memset(NL, 0x00, sizeof(NL));
memset(NE, 0x00, sizeof(NE));
memset(NA, 0x00, sizeof(NA));
memset(NB, 0x00, sizeof(NB));
memset(NP, 0x00, sizeof(NP));


if (strlen(orginal_msg) < wk_length) return 1;
if (strlen(en_key) < 16) return 2;
if ((wk_length % 16) != 0) return 3;

memset(encrypt_msg, 0x00, wk_length);

strncpy(input_key, &en_key[0], 16);
length = (int)wk_length;

for (i=0;i<16;i++) {
if (input_key[i] > '@') input_key[i] = input_key[i] - 7;
}
for (i=0;i<8;i++ ) {
input_key[i] = ((input_key[i*2] << 4) & 0x00f0) |
(input_key[i*2+1] & 0x000f);
}

j = length/16;
for(k=0; k < j; k++)
{
strncpy(input_msg, &orginal_msg[k*16], 16);

for (i=0;i<16;i++) {
if (input_msg[i] > '@') input_msg[i] = input_msg[i] - 7;
}
for (i=0;i<8;i++ ) {
input_msg[i] = ((input_msg[i*2] << 4) & 0x00f0) |
(input_msg[i*2+1] & 0x000f);
}
for (i=0;i<3;i++) {
new_encrypt(input_msg, out_msg, input_key,
NKEY, NR, NL, NE, NA , NB, NP);
}

for (i=0; i <8; i++)
{
lm = (int) out_msg[i] & 0x00ff;
sprintf(two_char,"%02X", lm);
org_msg[(i*2)] = two_char[0];
org_msg[(i*2)+1] = two_char[1];
org_msg[(i*2)+2] = 0x00;
}
strncpy(&encrypt_msg[k*16], org_msg, 16);
}
return ( 0 );
}
/*******************************************************************************/
/* Function : local_decrypt */
/* */
/* sub_msg : ¾Ïȣȭ µÈ Data */
/* decrypt_msg : ¾Ïȣȭ¸¦ Ǭ Data ( return value ) */
/* wk_length : ¾Ïȣȭ¸¦ Ç® ±æÀÌ */
/* en_key : ¾Ïȣȭ µÈ ±âº» Key */
/* return value: 0: OK */
/* 1: Source Data length error */
/* 2: initial key length error */
/* 3: length°¡ 16ÀÇ ¹è¼ö°¡ ¾Æ´Ô */
/*******************************************************************************/
int local_decrypt(char *sub_msg, char *decrypt_msg, UINT wk_length, char *en_key)
{


int i, j, k, lm, mm, length;
char two_char[3];
char input_key[17], input_msg[17], org_msg[17], out_msg[17];
char NKEY[9];

char NR[4]; /* result of R(i) */
char NL[4]; /* result of L(i) */
char NE[6]; /* result of E(R(i-1)) */
char NA[6]; /* result of E(R(i-1)) XOR K(i) */
char NB[6]; /* result of looking up S-BOX */
char NP[6]; /* result of P(B) */


memset(decrypt_msg, 0x00, wk_length+1);
memset(input_key, 0x00, sizeof(input_key));
memset(input_msg, 0x00, sizeof(input_msg));
memset(org_msg, 0x00, sizeof(org_msg));
memset(out_msg, 0x00, sizeof(out_msg));

memset(NKEY, 0x00, sizeof(NKEY));
memset(NR, 0x00, sizeof(NR));
memset(NL, 0x00, sizeof(NL));
memset(NE, 0x00, sizeof(NE));
memset(NA, 0x00, sizeof(NA));
memset(NB, 0x00, sizeof(NB));
memset(NP, 0x00, sizeof(NP));

if (strlen(sub_msg) < wk_length) return 1;
if (strlen(en_key) < 16) return 2;
if ((wk_length % 16) != 0) return 3;

memset(decrypt_msg, 0x00, wk_length);

length = (int)wk_length;
strncpy(input_key, &en_key[0], 16);

for (i=0;i<16;i++) {
if (input_key[i] > '@') input_key[i] = input_key[i] - 7;
}
for (i=0;i<8;i++ ) {
input_key[i] = ((input_key[i*2] << 4) & 0x00f0) |
(input_key[i*2+1] & 0x000f);
}

j = length/16;
for(k=0; k<j; k++)
{
strncpy(out_msg, &sub_msg[k*16], 16);
out_msg[16] = 0x00;

for (i=0;i<16;i++) {
for (mm=0;mm<16; mm++){
if (out_msg[i] == conv_TBL[mm]) out_msg[i]=swap_TBL[mm];
}
}
for (i=0;i<8;i++ ) {
out_msg[i] = ((out_msg[i*2] << 4) & 0x00f0) |
(out_msg[i*2+1] & 0x000f);
}

new_decrypt(input_msg, out_msg, input_key,
NKEY, NR, NL, NE, NA , NB, NP);

for (i=0; i <8; i++)
{
lm = (int) out_msg[i] & 0x00ff;
sprintf(two_char,"%02X", lm);
org_msg[(i*2)] = two_char[0];
org_msg[(i*2)+1] = two_char[1];
org_msg[(i*2)+2] = 0x00;
}
strncpy(&decrypt_msg[k*16], org_msg, 16);
}
return 0;
}

/******************************************************************************
* *
* encrypt() internal function *
* New Version *
******************************************************************************/
void new_encrypt(char *inputmsg, char *outmsg, char *inputkey,
char *nKEY, char *nR, char *nL, char *nE,
char *nA, char *nB, char *nP)
{
int i;

new_make_R_and_L_zero(inputmsg, nR, nL); /* make R(0) and L(0) */

for (i=1;i<17;i++) { /* from round 1 to round 16 */
new_expand_R(nR, nE); /* expanding R(i) */
new_modulo_2_add(inputkey, nKEY, nE, nA, i);
new_make_PB(nR, nL, nB, nP, nA); /* make R(i) and L(i) */
}

new_inverse_IP(nR, nL, outmsg); /* inverse INITIAL PERMUTATION */
}

/******************************************************************************
* *
* decrypt() internal function *
* *
******************************************************************************/
void new_decrypt(char *inputmsg, char *outmsg, char *inputkey,
char *nKEY, char *nR, char *nL, char *nE,
char *nA, char *nB, char *nP)
{
int i;
//char temp[9];

//memset(temp, 0x00, sizeof(temp));

//strncpy(temp, inputmsg, 8);

for (i=0;i<8;i++) inputmsg[i] = outmsg[i];

new_make_R_and_L_zero(inputmsg, nR, nL); /* make R(0) and L(0) */
for (i=16;i>0;i--) { /* from round 16 to round 1 */
new_expand_R(nR, nE); /* expanding R(i) */
new_modulo_2_add(inputkey, nKEY, nE, nA, i);
new_make_PB(nR, nL, nB, nP, nA); /* make R(i) and L(i) */
}
new_inverse_IP(nR, nL, outmsg); /* inverse INITIAL PERMUTATION */
}

/******************************************************************************
* *
* make_R_and_L_zero() internal function *
* *
******************************************************************************/
void new_make_R_and_L_zero(char *inputmsg, char *nR, char *nL)
{
new_bit_permutation(inputmsg,nR,4,R_tbl);
new_bit_permutation(inputmsg,nL,4,L_tbl);
}

/******************************************************************************
* *
* bit_permutation internal function *
* bit_permutation(char *in_data,char *out_data,int data_length, *
* char *table_name) *
******************************************************************************/
void new_bit_permutation(char *in_data,char *out_data,int data_length,char *table_name)
{
char c;
int i,j,k,l,m,o;
unsigned char bit_table[9] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01 };

for (i=0;i<data_length;i++) {
c = 0;
for (j=0;j<8;j++) {
k = (int) table_name[i*8+j] - 1;
l = k / 8;
m = k % 8;
if ((in_data[l] & bit_table[m]) != 0) o = 0x01;
else o = 0x00;
c = (int) ((c << 1) & 0xfe) | o;
}
out_data[i] = c;
}
}

/******************************************************************************
* *
* expand_R() internal function *
* *
******************************************************************************/
void new_expand_R(char *nR, char *nE)
{

new_bit_permutation(nR,nE,6,E_R_tbl);
}

/******************************************************************************
* *
* modulo_2_add(int round) internal function *
* *
******************************************************************************/
void new_modulo_2_add(char *inputkey, char *nKey,
char *nE, char *nA, int round)
{
int i;

new_bit_permutation(inputkey,nKey,6,key_tbl[round-1]);
for (i=0;i<6;i++) nA[i] = (nE[i] ^ nKey[i]);
}

/******************************************************************************
* *
* make_PB() internal function *
* *
******************************************************************************/
void new_make_PB(char *nR, char *nL, char *nB, char *nP, char *nA)
{
int i;
char temp[5];

new_look_up_S_box(nA, nB);
new_bit_permutation(nB,nP,4,P_tbl);
for (i=0;i<4;i++) temp[i] = nR[i];
for (i=0;i<4;i++) nR[i] = (nP[i] ^ nL[i]);
for (i=0;i<4;i++) nL[i] = temp[i];
}

/******************************************************************************
* *
* look_up_S_box() internal function *
* *
******************************************************************************/
void new_look_up_S_box(char *nA, char *nB)
{
char c,d;

c = nA[0];
c = (c >> 2) & 0x3f;
d = new_get_S_box(0,c);
nB[0] = d << 4;
c = nA[0];
c = (c << 4) & 0x30;
d = nA[1];
d = (d >> 4) & 0x0f;
c = (c | d) & 0x3f;
d = new_get_S_box(1,c);
nB[0] = nB[0] | d;
c = nA[1];
c = (c << 2) & 0x3c;
d = nA[2];
d = (d >> 6) & 0x03;
c = (c | d) & 0x3f;
d = new_get_S_box(2,c);
nB[1] = d << 4;
c = nA[2] & 0x3f;
nB[1] = nB[1] | new_get_S_box(3,c);
c = nA[3];
c = (c >> 2) & 0x3f;
d = new_get_S_box(4,c);
nB[2] = d << 4;
c = nA[3];
c = (c << 4) & 0x30;
d = nA[4];
d = (d >> 4) & 0x0f;
c = (c | d) & 0x3f;
d = new_get_S_box(5,c);
nB[2] = nB[2] | d;
c = nA[4];
c = (c << 2) & 0x3c;
d = nA[5];
d = (d >> 6) & 0x03;
c = (c | d) & 0x3f;
d = new_get_S_box(6,c);
nB[3] = d << 4;
c = nA[5] & 0x3f;
nB[3] = nB[3] | new_get_S_box(7,c);
}
/******************************************************************************
* *
* get_S_box(int round, char c) internal function *
* *
******************************************************************************/
char new_get_S_box(int round, char c)
{
short int j,k;
char d;

j = (c >> 4) & 0x02;
j = j | (c & 0x01) + round *4;
k = (c >> 1) & 0x0f;
d = S_box[j][k];
return (d);
}
/******************************************************************************
* *
* inverse_IP() internal function *
* *
******************************************************************************/
void new_inverse_IP(char *nR, char *nL, char *outmsg)
{
int i;
char temp[9];

for (i=0;i<4;i++) temp[i] = nR[i];
for (i=0;i<4;i++) temp[i+4] = nL[i];
new_bit_permutation(temp,outmsg,8,IP_tbl);
}[/syntax]DES DLL(Single/Triple DES) Source/Sample/Document
Cordialement

Stéphane Maillard
Avatar de l’utilisateur
Stephane Maillard
Lord of DominoArea
Lord of DominoArea
 
Message(s) : 8695
Inscrit(e) le : 16 Déc 2004 à 01:10
Localisation : Bretagne

Retour vers Divers

cron