информационная безопасность
без паники и всерьез
 подробно о проектеRambler's Top100
Портрет посетителяВсе любят медЗа кого нас держат?
BugTraq.Ru
Русский BugTraq
 Анализ криптографических сетевых... 
 Модель надежности двухузлового... 
 Специальные марковские модели надежности... 
 Фишинговая атака на Python-разработчиков 
 ФБР нашла русский след в атаках... 
 Массовый взлом SharePoint 
главная обзор RSN блог библиотека закон бред форум dnet о проекте
bugtraq.ru / форум / programming
Имя Пароль
ФОРУМ
если вы видите этот текст, отключите в настройках форума использование JavaScript
регистрация





Легенда:
  новое сообщение
  закрытая нитка
  новое сообщение
  в закрытой нитке
  старое сообщение
  • Напоминаю, что масса вопросов по функционированию форума снимается после прочтения его описания.
  • Новичкам также крайне полезно ознакомиться с данным документом.
CryptoAPI не катит? 09.04.03 15:08  Число просмотров: 1404
Автор: IgorR <Igor Razin> Статус: Member
<"чистая" ссылка>
<programming>
[C++] Шифрование по алгоритму DES 09.04.03 07:42  
Автор: alien <Андрей> Статус: Member
<"чистая" ссылка>
Подкиньте, плиз, ссылочку на библиотеку (а лучше на компоненту для BCB 6) для шифрования по алгоритму DES.

Нужна такая же функциональность, как и у функции crypt в *nix. Ничего больше!

Если же в Винде (XP) есть функции для шифрования по вышеуказанному алгоритму, то очень бы хотелось увидеть пример ее использования

Заранее спасибо...
Тебе какой DES то нужен ? 10.04.03 09:33  
Автор: PS <PS> Статус: Elderman
<"чистая" ссылка>
Код более чем на половину не мой. Моих только пару ф-ий, но думаю автор не обидится.

#include "stdafx.h"
#include "des.h"

#define	KEY_LEN	16

static perm64 des_keys[17];

static uc des_shl16[17] = {
    0,
    1,1,2,2,2,
    2,2,2,1,2,
    2,2,2,2,2,
    1};

static uc des_ip[65] = {
    0,	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, 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};

static uc des_ipi[65] = {
    0, 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};

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

static uc des_pc2[65] = {
    0, 14, 17, 11, 24,
    1,	5, 3, 28, 15,
    6, 21, 10, 23, 19,
    12,  4, 26,  8, 16,
    7, 27, 20, 13,  2,
    41, 52, 31, 37, 47,
    55, 30, 40, 51, 45,
    33, 48, 44, 49, 39,
    56, 34, 53, 46, 42,
    50, 36, 29, 32, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0};

static uc des_e[65] = {
    0, 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, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0};

static uc des_pe[65] = {
    0, 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, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0};


static uc des_sb[9][4][16] = {
    { { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 },
	{ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 },
	{ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 },
	{ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 } },

    { { 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 } } };

static uc last_key[100];


static uc *xor_8(uca,ucb,uc	* c )
{
    register	    i;
    for( i = 0; i < 8; i++ ) c[i] = a[i] ^ b[i];
    return c;
} 


static void iperm (perm64 y,perm64 x)
{
    perm64  z;

    register
    char    i;

    for (i = 1; i < 65; i++) {

	if (x[i]) z[i] = y[x[i]];
	else z[i] = 0;
    }

    memcpy (y, z, 65);
}


static void piperm (perm64 y,perm64 x)
{
    perm64  z;
    register char    i;
    for (i = 1; i < 65; i++) z[i] = y[x[i - 1] + 1];
    memcpy (y, z, 65);
}


static void clear64 (perm64 x)
{
    memset (x+1, 0, 64);
}


static void clear32 (perm64	x)
{
    memset (x+33, 0, 32);
}


static void  copyrl (perm64 x)
{
    memcpy (x+1, x+33, 32);
    memset (x+33, 0, 32);
}


static void copylr (perm64 x)
{
    memcpy (x+33, x+1, 32);
    memset (x+1 , 0, 32);
}


static void copyinv(perm64 x)
{
    perm64  y;
    memcpy (y +  1, x +  1, 32);
    memcpy (x +  1, x + 33, 32);
    memcpy (x + 33, y +  1, 32);
}


static void conv8to64 (uc8	x,perm64 y)
{
    register char    i, j, k;
    register uc   xx;

    for (i = 0, k = 0; i < 8; i++, k += 8) {

	xx = x[i];

	j = 8;
	while (j--) {
	    y[k + j + 1] = xx & 1;
	    xx >>= 1;
	}
    }
}


static void conv64to8 (perm64  y, uc8  x)
{
    register char    i, j, k;
    register uc   b;

    for (i = 0, k = 0; i < 8; i++, k += 8) {

	x[i] = 0;
	b = 1;

	j = 8;
	while (j--) {
	    if (y[k + j + 1] == 1) x[i] += b;
	    b <<= 1;
	}
    }
}


static void exor (perm64 x,perm64  y)
{
    register char    i;

    for (i = 1; i < 65; i++) x[i] = (x[i] + y[i]) & 1;
}


static void lshift (perm64	x, int	c)
{
    perm64  y;

    if (! c) return;

    memcpy (y+1, x+1, 56);

    memcpy (x+1,    y+1+c,  28-c);
    memcpy (x+29-c, y+1,    c);

    memcpy (x+29,   y+29+c, 28-c);
    memcpy (x+57-c, y+29,   c);
}


static void subbox (perm64 &r,uc nr)
{
    register char    i, j;
    register uc   subs;
    int     re, sp;

    iperm (r, des_e);
    exor (r, des_keys[nr]);

    for (i = 0; i < 8; i++) {

	register char z = 6 * i;

	re = (r[z + 1] << 1) + r[z + 6];
	sp = (((((r[z + 2] << 1) + r[z + 3]) << 1)
		+ r[z + 4]) << 1) + r[z + 5];

	subs = des_sb[i + 1][re][sp];

	z = i * 4;
	j = 4;
	while (j--) {

	    r[z + j + 1] = subs & 1;
	    subs >>= 1;
	}
    }

    clear32 (r);
    iperm (r, des_pe);
}


static void add (perm64  k, perm64  x,perm64   y)
{
    copylr (y);
    exor (x, y);
    memcpy (k, x, 65);
}

void OSTstdDesKey (uc8 key)
{
    perm64  key64;
    register char    i;

    conv8to64 (key, key64);

    iperm (key64, des_pc1);

    for (i = 1; i < 17; i++) {

	lshift (key64, des_shl16[i]);
	memcpy (des_keys[i], key64, 65);
	iperm (des_keys[i], des_pc2);
    }
}


void OSTstdDesEncrypt (uc8 kl,uc8 R)
{
    register int     i;
    perm64  r, l, l1, kl64;

    conv8to64 (kl, kl64);

    iperm (kl64, des_ip);

    memcpy (l, kl64, 65);
    memcpy (r, kl64, 65);
    clear32 (l);
    copyrl (r);

    for (i = 1; i < 17; i++) {
 
	memcpy (l1, r, 65);
	subbox (r, i);
	exor (r, l);
	clear32 (r);
	memcpy (l, l1, 65);
    }

    add (kl64, l, r);
    copyinv (kl64);
    iperm (kl64, des_ipi);

    conv64to8 (kl64, R);
}


void OSTstdDesDecrypt (uc8 kl,uc8  R)
{
    register int     i;
    perm64  r, l, l1, kl64;

    conv8to64 (kl, kl64);
    iperm (kl64, des_ip);
    memcpy (l, kl64, 65);
    memcpy (r, kl64, 65);
    clear32 (l);
    copyrl (r);

    i = 17;
    while (--i) {

	memcpy (l1, r, 65);
	subbox (r, i);
	exor (r, l);
	clear32 (r);
	memcpy (l, l1, 65);
    }

    add (kl64, l, r);
    copyinv (kl64);
    iperm (kl64, des_ipi);
    conv64to8 (kl64, R);
}

void des_cbc_encrypt(unsigned char *key,unsigned char *source,
					 unsigned char *destination,int l,unsigned char *icvin)
{
	int i;
	int j;
	unsigned char last_block[8];
	unsigned char code_block[8];


	static unsigned char icv[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	if(icvin!=NULL) memcpy(icv,icvin,8);
	/* take n*8 Bytes from source, crypt it (CBC) and put it to destination */

	OSTstdDesKey(key);
	memcpy(last_block,icv,8);
	for (i=0;i<(l/8);i++)
	{
		for (j=0;j<8;j++) code_block[j]=(source[i*8+j]^last_block[j]);
		OSTstdDesEncrypt(code_block,destination+(i*8));
		memcpy(last_block,destination+(i*8),8);
	}
} /* end of des_cbc_encrypt */

void des_cbc_decrypt(unsigned char *key,unsigned char *source,
					 unsigned char *destination,int l,unsigned char *icvin)
{
	int i;
	int j;
	unsigned char last_block[8];
	unsigned char code_block[8];


	static unsigned char icv[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
	if(icvin!=NULL) memcpy(icv,icvin,8);

	/* take n*8 Bytes from source, decrypt it (CBC) and put it to destination */

	OSTstdDesKey(key);
	memcpy(last_block,icv,8);
	for (i=0;i<(l/8);i++)
	{
		memcpy(code_block,source+(i*8),8);
		OSTstdDesDecrypt(source+(i*8),destination+(i*8));
		for (j=0;j<8;j++) destination[i*8+j]=(destination[i*8+j]^last_block[j]);
		memcpy(last_block,code_block,8);
	}
} /* end of des_cbc_decrypt */


void des_3cbc_encrypt(unsigned char *ks1,unsigned char *ks2,unsigned char *source,
					 unsigned char *destination,int length,unsigned char *icvin1,unsigned char *icvin2)
{
//	int off=((int)length-1)/8;
	long l8=((length+7)/8)*8;

		des_cbc_encrypt(ks1,source,destination,length,icvin1);
		des_cbc_decrypt(ks2,destination,destination,l8,icvin1);
		des_cbc_encrypt(ks1,destination,destination,l8,icvin2);
}

void des_3cbc_decrypt(unsigned char *ks1,unsigned char *ks2,unsigned char *source,
					 unsigned char *destination,int length,unsigned char *icvin1,unsigned char *icvin2)
{
//	int off=((int)length-1)/8;
	long l8=((length+7)/8)*8;

		des_cbc_decrypt(ks1,source,destination,l8,icvin2);
		des_cbc_encrypt(ks2,destination,destination,l8,icvin1);
		des_cbc_decrypt(ks1,destination,destination,length,icvin1);
}

---

des.h
#ifndef_oststd_#define_oststd_
/* Typdefinitionen */

typedef unsigned char uc;
typedef uc perm64[65];
typedef uc uc8[8];


/* Funktions-Return-Codes */

#ifndef OK
#define OK			0
#endif


#ifndef NOT_OK
#define NOT_OK		       -1	
#endif

#ifndef TRUE
#define TRUE    		1
#endif
#ifndef FALSE
#define FALSE   		0
#endif

#define OSTstdMathMax_c 		256

/* crc */

/* Achtung:
 *
 * Beim CCITT-Polynom werden die Bits von LSB-first kodiert:
 *      00 01 02 03 | 04 05 06 07  | 08 09 10 11 | 12 13 14 15
 *
 * Beim GAO-Polynom werden die Bits in folgender Reihenfolge kodiert:
 *      00 01 02 03 | 07 06 05 04  | 11 10 09 08 | 15 14 13 12
 *
 * Die Stelle x^16 ist immer gesetzt.
 */

#define OSTstdCrcCCITTAlg_c    0
#define OSTstdCrcGAOAlg_c      1

/* Polynom: x^16 + x^12 + x^5 + x^0 */
#define OSTstdCrcCCITTPol_c    0x8408
#define OSTstdCrcGAOPol_c      0x1021

/* Polynom: x^16 + x^15 + x^2 + x^0 */
#define OSTstdCrcCCITTPol2_c  0xA001
#define OSTstdCrcGAOPol2_c    0x5008


#define OSTstdCrcCCITT_c        OSTstdCrcCCITTAlg_c, OSTstdCrcCCITTPol_c
#define OSTstdCrcGAO_c          OSTstdCrcGAOAlg_c,   OSTstdCrcGAOPol_c


*****************************************************************************
*	void OSTstdDesKey(uc8 key)
*
*	Sets DES-Keys for further de- and encrypting function calls
*
*	Parameters:
*
*		key		-	8 Byte DES-Key
*****************************************************************************
void OSTstdDesKey(uc8 key);

*****************************************************************************
*	void OUSTstdDesEncrypt(uc8 kl, uc8 R)
*
*	DES-Encryption
*
*	Parameters:
*
*		kl		-	8 Byte data to encrypt
*		R		-	8 Byte encrypted data
*****************************************************************************
void OSTstdDesEncrypt(uc8 kl,uc8 R);




*****************************************************************************
*	void OSTstdDesDecrypt(uc8 kl, uc8 R)
*
*	DES-Decryption
*
*	Parameters:
*
*		kl		-	8 Byte data to decrypt
*		R		-	8 Byte decrypted data
*****************************************************************************
void OSTstdDesDecrypt(uc8 kl,uc8 R);

----------------------------------------------------------------------------
/*                     DES CBC De&En cryption Functions        				  */
----------------------------------------------------------------------------

void des_cbc_decrypt(unsigned char *key,unsigned char *source,
					 unsigned char *destination,int l,unsigned char *icvin=NULL);
void des_cbc_encrypt(unsigned char *key,unsigned char *source,
					 unsigned char *destination,int l,unsigned char *icvin=NULL);

void des_3cbc_decrypt(unsigned char *ks1,unsigned char *ks2,unsigned char *source,
					 unsigned char *destination,int length,unsigned char *icvin1=NULL,unsigned char *icvin2=NULL);
void des_3cbc_encrypt(unsigned char *ks1,unsigned char *ks2,unsigned char *source,
					 unsigned char *destination,int length,unsigned char *icvin1=NULL,unsigned char *icvin2=NULL);
#endif

---
CryptoAPI не катит? 09.04.03 15:08  
Автор: IgorR <Igor Razin> Статус: Member
<"чистая" ссылка>
Да может и катит... 10.04.03 07:24  
Автор: alien <Андрей> Статус: Member
<"чистая" ссылка>
...только некогда разбираться, времени в обрез, а строку зашифровать надо.

Вот ты бы взял и примерчик мне подкинул...
Да может и катит... 10.04.03 08:53  
Автор: IgorR <Igor Razin> Статус: Member
<"чистая" ссылка>
> Вот ты бы взял и примерчик мне подкинул...

MSDN Library -> Security -> Cryptography -> ...
МСДН -> поиск по CryptEncrypt -> Example C Program: Encrypting a File
www.rsdn.ru -> статьи -> криптография -> CrypoAPI -> Хеширование, шифрование и цифровая подпись с использованием CryptoAPI и .NET
1




Rambler's Top100
Рейтинг@Mail.ru


  Copyright © 2001-2025 Dmitry Leonov   Page build time: 1 s   Design: Vadim Derkach