示例#1
0
//参数:buf1~buf3,字符串指针,原始字符串地址,长度8位
//key,密钥明文的地址
//返回值:0,成功;其它失败
int	__stdcall gen_work_key(unsigned char* buf1,unsigned char* buf2,unsigned char*buf3,unsigned char* key)
{
	unsigned int  i;
	unsigned long ul;
	char temp[3];
	unsigned char szKey[9];
	unsigned char statkey[9];

	memset(temp,0,sizeof(temp));
	memset(statkey,0,sizeof(statkey));
	memset(szKey,0,sizeof(szKey));

	for(i=0;i<8;i++)
	{
		szKey[i]=buf1[i]^buf2[i]^buf3[i];
		szKey[i]=~szKey[i];
	}
	for(i=0;i<8;i++)
	{
		memcpy(temp,&workkey[2*i],2);
		ul=strtoul(temp,NULL,16);
		statkey[i]=ul;
	}
	encrypt_des(statkey,8,szKey,8,key);
	return 0;
}
示例#2
0
static int gen_user_card_keyA(unsigned char *key,unsigned char *text,unsigned char *keyA)
{
	// gen_user_card_keyA算出来的keyA和des_code算出来的ucDesData是一样的东西
	unsigned int i;
	/*	unsigned int j;*/
	unsigned char sPlain[9];	
	unsigned char sCipher[17]; 
	unsigned int  startpos = 0;
	unsigned char ucRawData[256];
	unsigned char ucDesData[256] = "";
	unsigned char ucdata[17];
	int iTemp = 0;
	memset(sPlain, 0, sizeof(sPlain));	
	memset(sCipher, 0, sizeof(sCipher));
	memset(ucDesData, 0, sizeof(ucDesData));
	memset(ucdata, 0, sizeof(ucdata));
	memset(ucRawData, 0, sizeof(ucRawData));
	memset(ucDesData, 0, sizeof(ucDesData));
	memcpy(ucRawData, text, 4);
	for(i = 0; i < 4; i++)
	{
		sPlain[i] = text[i];
		sPlain[i+4] = ~text[i];
	}

	encrypt_des(key, 8, sPlain, 8, sCipher);
	memcpy(sCipher + 8, sCipher, 8);
	startpos=(sCipher[0] + sCipher[1] + sCipher[2] + sCipher[3] + sCipher[4] + sCipher[5] + sCipher[6] + sCipher[7]) ;
	startpos%=7;      // sCipher[17]6079813C4F1A2F92, startpos = 4
	memcpy(keyA, sCipher + startpos, 6);
	return 0;
}
示例#3
0
//////////////////////////////////////////////////////////////////////////
// 泰州这套没有修改偏移算法
int	__stdcall gen_user_card_keyA(unsigned char *key,unsigned char *text,unsigned char *keyA)
{
	// gen_user_card_keyA算出来的keyA和des_code算出来的ucDesData是一样的东西
	unsigned int i;
/*	unsigned int j;*/
	unsigned char sPlain[9];	
	unsigned char sCipher[17];
	unsigned int  startpos = 0;
	unsigned char ucRawData[256];
 	unsigned char ucDesData[256] = "";
	unsigned char ucdata[17];
	int iTemp = 0;
	memset(sPlain, 0, sizeof(sPlain));	
	memset(sCipher, 0, sizeof(sCipher));
	memset(ucDesData, 0, sizeof(ucDesData));
	memset(ucdata, 0, sizeof(ucdata));
	memset(ucRawData, 0, sizeof(ucRawData));
	memset(ucDesData, 0, sizeof(ucDesData));
	memcpy(ucRawData, text, 4);
	for(i = 0; i < 4; i++)
	{
		sPlain[i] = text[i];
		sPlain[i+4] = ~text[i];
/*		ucRawData[i + 4] = ~ucRawData[i];*/
	}
	//////////////////////////////////////////////////////////////////////////
//	des_code(key, ucRawData, ucDesData, '0');
	//////////////////////////////////////////////////////////////////////////
// 	memcpy(ucdata, ucDesData, 8);
// 	memcpy(ucdata + 8, ucDesData, 8);
// 	for (i = 0; i < 8; i++)
// 	{
// 		iTemp += ucdata[i] * i;
// 	}
// 	i = iTemp % 10;
// 	memcpy(keyA, ucdata + i, 6);
	encrypt_des(key, 8, sPlain, 8, sCipher);
	memcpy(sCipher + 8, sCipher, 8);
	startpos=(sCipher[0] + sCipher[1] + sCipher[2] + sCipher[3] + sCipher[4] + sCipher[5] + sCipher[6] + sCipher[7]) % 7;      // sCipher[17]6079813C4F1A2F92, startpos = 4
	memcpy(keyA, sCipher + startpos, 6);
	//	memcpy(ucDesData, sCipher+startpos, 6);
	//////////////////////////////////////////////////////////////////////////
	// 加入偏移的算法
	//////////////////////////////////////////////////////////////////////////
//	memcpy(ucDesData, sCipher, 16);
// 	memcpy(ucdata, sCipher, 8);

// 	memcpy(ucdata + 8, sCipher, 8);
// 	for (j = 0; j < 8; j++)
// 	{
// 		iTemp += sCipher[j] * j;
// 	}
// 	j = iTemp % 10;
// 	memcpy(keyA, ucdata + j, 6);
	return 0;
}
示例#4
0
static int encrypt_3des(unsigned char* key,long keylen,unsigned char *data,short datalen,unsigned char *Des_result)
{
	unsigned char szkey[9];
	unsigned char szplain[9];
	unsigned char szcipher[9];
	if(keylen != 16)
		return INVALID_KEY_LEN;
	if(datalen != 8)
		return INVALID_DATA_LEN;


	memset(szkey,0,sizeof(szkey));
	memset(szplain,0,sizeof(szplain));
	memset(szcipher,0,sizeof(szcipher));

	memcpy(szkey,key,8);
	memcpy(szplain,data,8);

	/*
	deskey(szkey,0);
	Ddes(szplain,szcipher);
	*/
	encrypt_des(szkey,8,szplain,8,szcipher);

	memcpy(szkey,key+8,8);
	/*
	deskey(szkey,1);
	Ddes(szcipher,szplain);
	*/
	dencrypt_des(szkey,8,szcipher,8,szplain);

	memcpy(szkey,key,8);
	/*
	deskey(szkey,0);
	Ddes(szplain,szcipher);
	*/
	encrypt_des(szkey,8,szplain,8,szcipher);

	memcpy(Des_result,szcipher,8);
	return 0;
}
示例#5
0
int	__stdcall gen_auth_card_keyA(unsigned char *text,unsigned char *keyA)
{
	unsigned int  i=0;
	unsigned long ul;
	char temp[3]="";
	unsigned char statkey[9]="";

	memset(temp,0,sizeof(temp));
	memset(statkey,0,sizeof(statkey));
	for(i=0;i<8;i++)
	{
		memcpy(temp,&workkey[2*i],2);
		ul=strtoul(temp,NULL,16);
		statkey[i]=ul;
	}
	encrypt_des(statkey,8,text,8,keyA);
	return 0;
}
示例#6
0
uint32_t encrypt_with_mode(const BYTE *password, const BYTE* data, uint32_t len, BYTE* ans, encrypt_mode mode, encrypt_method method) {
	uint32_t outl;
	switch (mode) {
		case AES128:
			outl = encrypt_aes128(password, data, len, ans, method);
			break;
		case AES192:
			outl = encrypt_aes192(password, data, len, ans, method);
			break;
		case AES256:
			outl = encrypt_aes256(password, data, len, ans, method);
			break;
		case DES:
			outl = encrypt_des(password, data, len, ans, method);
			break;
	}
	return outl;
}
示例#7
0
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, bbbxxxxxxxxxx, gjeogjeogjeojsb";
   int  ret = -1;
   // ---------------------------------------------
   // I use sizeof instead of strlen because I want
   // to count the '\0' at the end, strlen would
   // not count it
   int stringLen = strlen(string) + 1;
   int cipherLen = stringLen *2;

   printf("Plain Text : %s\n",string);
   char *cipher = (char*)malloc(cipherLen);
   char *text = (char*)malloc(stringLen);
   memset(cipher, 0, cipherLen);
   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;
   }
   // ks1的值是根据解密是的数据变化的,所以该方法失败。
   ret = encrypt_des(&cblock, &ks1, &ks2, &ks3, cipher, string, stringLen);
   if (ret < 0) {
       return ret;
   }

   ret = decrypt_des(cipher, text, &ks1, &ks2, &ks3, &cblock, cipherLen);
   if (ret < 0) {
       return ret;
   }

   return 0;
}
示例#8
0
int __stdcall calc_oper_pwd(const char *oper_code,const char *pwd,char *text)
{
	unsigned char key[8];
	unsigned char plaintext[8];
	unsigned char ciphertext[8];
	int i,j;
	if(strlen(oper_code) > 8)
		return -1;
	if(strlen(pwd) > 8)
		return -2;
	memset(key,0,sizeof key);
	memcpy(key,oper_code,strlen(oper_code));
	memset(plaintext,0,sizeof plaintext);
	memcpy(plaintext,pwd,strlen(pwd));
	encrypt_des(key,8,plaintext,8,ciphertext);
	for(i =0,j=0;i < 8;++i)
		j += sprintf(text+j,"%02X",ciphertext[i]);
	return 0;
}
示例#9
0
int	__stdcall gen_user_card_keyA(unsigned char *key,unsigned char *text,unsigned char *keyA)
{
	unsigned int  i;
	unsigned char sPlain[9];
	unsigned char sCipher[17];
	unsigned int  startpos=0;

	memset(sPlain,0,sizeof(sPlain));
	memset(sCipher,0,sizeof(sCipher));
	for(i=0;i<4;i++)
	{
		sPlain[i]=text[i];
		sPlain[i+4]=~text[i];
	}
	encrypt_des(key,8,sPlain,8,sCipher);
	memcpy(sCipher+8,sCipher,8);
	startpos=(sCipher[0]+sCipher[1]+sCipher[2]+sCipher[3]+sCipher[4]+sCipher[5]+sCipher[6]+sCipher[7])%7;
	memcpy(keyA,sCipher+startpos,6);
	return 0;
}
示例#10
0
文件: main.c 项目: daolong/cencrypt
static void des_test_withmode(int mode)
{
  DEBUG("\n\n== des_test_withmode (%s) ==\n", des_getmode(mode));  
  char *testData = des_padding(gTestData0);
  if (testData == NULL) return NULL;
  size_t out_len = 0;
  char *encrypted = encrypt_des((const char*)testData, strlen(testData), gPassword, mode, &out_len);
  if (encrypted != NULL) {
    des_dump(encrypted, out_len);
    char *plain = decrypt_des(encrypted, out_len, gPassword, mode, &out_len);
    if (plain != NULL) {
      DEBUG("%s : plain text =\n%s \n", __func__, plain);
      if (strncmp(plain, testData, strlen(testData)) == 0) {
        DEBUG("%s : Success \n", __func__);
      } else {
        DEBUG("%s : Fail \n", __func__);
      }
      DO_FREE(plain);
    }
    DO_FREE(encrypted);
  }
  DO_FREE(testData);
}