コード例 #1
0
string EncryptionHandler::singleEncrypt(const string& msg)
{
    DES des;
    des.SetKey(DES_KEY, 8);
    des.ProduceSubKey();
    
    
    CC_ASSERT(msg.length() <= 8);
    char byte[8] = {0};
    for (int i = 0; i < msg.length(); i++)
    {
        byte[i] = msg.c_str()[i];
    }
    
    des.SetMsg(byte, 8);
    des.Crypte();
    
   // vector<char> cipher;
   // for (int i = 0; i < 8; i++)
   // {
   //     cipher.push_back(des.cryptedmsg[i]);
   // }
    
    string cipher(des.cryptedmsg, des.cryptedmsg + 8);
    return cipher;
}
コード例 #2
0
ファイル: application.cpp プロジェクト: fcgdam/DES_Library
void desTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e };
  
  Serial.println();
  Serial.println("========= DES test ==========");
  
  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.encrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  
  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.decrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
}
コード例 #3
0
ファイル: DESEncry.cpp プロジェクト: modeyang/CardProject
bool CDESEncry::DesryFile(char *szFilename)
{
	std::locale::global(std::locale(""));
	FILE *ff;
	char inbuff[8],outbuff[8];

	ff = fopen(szFilename, "rb");

	if(ff==NULL)
	{
		//std::string szInFile =  CMarkup::UTF8ToGB2312(szFilename);
		ff = fopen(szFilename, "rb");
		if (NULL == ff)
		{
			return false;
		}
	}

	int fsize;
	fread(&fsize,sizeof(int),1,ff);			//获得明文文件大小

	int nTotalsize = fsize;
	fsize = fsize%8 ? (fsize/8+1)*8 : fsize;

	//fread(initbuff,sizeof(char),sizeof(initbuff),ff);	//获得明文件的类型、加密重数、加密模式

	m_pfileContent = (char*)malloc(sizeof(char)*fsize+1);

	//开始解密
	DES jm;
	int nStart = 0;
	while(fsize > 0)		//请参考加密部分
	{
		int nRead = (int)fread(inbuff,sizeof(char),8,ff);

		jm.Des_one(outbuff,inbuff,C_Key,DECRYPT);

		if (fsize > 8)
		{
			memcpy(m_pfileContent + nStart, outbuff, 8);
			nStart += 8;
		}
		else
		{
			memcpy(m_pfileContent + nStart, outbuff, fsize);	//输出明文,不包括最后一个分组
			nStart += nRead;
		}

		fsize -= nRead;		//保存剩余尚未输出的明文大小		
	}
	m_pfileContent[nTotalsize] = 0;
	fclose(ff);

	GetvecInfoPos();
	return true;
}
コード例 #4
0
ファイル: DESUtil.cpp プロジェクト: hustg4/CocosLua
unsigned char* DESUtil::encrypt3(const char *key, unsigned char *data, int inSize, int *outSize)
{
    DES des;
    *outSize = des.extend(inSize);
    unsigned char* buffer = (unsigned char*)malloc(*outSize);
    memcpy(buffer, data, inSize);
    des.encrypt3((unsigned char*)key, buffer, inSize);
    
    return buffer;
}
コード例 #5
0
ファイル: DESUtil.cpp プロジェクト: hustg4/CocosLua
unsigned char* DESUtil::decrypt3(const char *key, unsigned char *data, int inSize, int *outSize)
{
    
    unsigned char* buffer = (unsigned char*)malloc(inSize);
    memcpy(buffer, data, inSize);
    
    DES des;
    des.decrypt3((unsigned char*)key, buffer, inSize, outSize);
    
    return buffer;
}
コード例 #6
0
ファイル: TCCode.cpp プロジェクト: JohnWilliam1988/TCIDE
void  CTCCode::EnCodeFile(wchar_t *InFile,wchar_t* key,wchar_t* OutFile)
{
	if(InFile&&key&&OutFile)
	{
		HANDLE infile=::CreateFile(InFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
		if(infile!=INVALID_HANDLE_VALUE)
		{
			HANDLE outfile=::CreateFile(OutFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
			if(outfile!=INVALID_HANDLE_VALUE)
			{
				DES des;
				//unicode 转 anscii
				int num=::WideCharToMultiByte (CP_ACP,0,key,-1,NULL,0,NULL,NULL);
				char* key_l = new char[num];
				memset(key_l, 0, num); //初始化动作
				WideCharToMultiByte (CP_ACP,0,key, num ,key_l, num ,NULL,NULL);
				long lFileLen=::GetFileSize(infile,NULL);
				long count=lFileLen/MAXFILESIZE;	//整除,知道整个文件的块数
				long d=lFileLen%MAXFILESIZE;	//取余数
				long outLen = 0;
				char inbuff[MAXFILESIZE]={0};
				char oubuff[MAXFILESIZE]={0};
				DWORD ReadFileSize=0;
				DWORD DwWitten=0;
				for(long i=0;i<count;i++)
				{
					memset(inbuff,0,sizeof(inbuff));
					memset(oubuff,0,sizeof(oubuff));
					::ReadFile(infile,inbuff,MAXFILESIZE,&ReadFileSize ,NULL);
					des.Des_Go(oubuff, inbuff, sizeof(inbuff), key_l,sizeof(key_l), ENCRYPT1);
					::WriteFile(outfile,oubuff,MAXFILESIZE,&DwWitten,NULL);
				}
				if(d>0)
				{
					memset(inbuff,0,sizeof(inbuff));
					memset(oubuff,0,sizeof(oubuff));
					::ReadFile(infile,inbuff,d,&ReadFileSize ,NULL);
					des.Des_Go(oubuff, inbuff, MAXFILESIZE, key_l,sizeof(key_l), ENCRYPT1);
					::WriteFile(outfile,oubuff,MAXFILESIZE,&DwWitten,NULL);
				}
				delete []	key_l;
				//写入文件长度及结束符
				char endmark[2]={0xA3,0xA4};
				::WriteFile(outfile,endmark,2,&DwWitten,NULL);
				char dsize[2]={0};
				itoa(d,dsize,10);
				::WriteFile(outfile,dsize,2,&DwWitten,NULL);
			}
			CloseHandle(outfile);
		}
		CloseHandle(infile);
	}

}
コード例 #7
0
ファイル: 14.DES.cpp プロジェクト: vbv15/helloworld
int main()
{
	
	DES d;
	d.initialiseTables();
	d.key_generator( d.keyWithParties,d.RoundKeys,d.shiftTable);
	d.Cipher(d.plainBlock,d.RoundKeys,d.cipherBlock);
	d.printCipherBlock();
	
	
	
	return 1;
}
コード例 #8
0
ファイル: ssl.cpp プロジェクト: ewduong/ScheduleIt
void DES_ecb_encrypt(DES_cblock* input, DES_cblock* output,
                     DES_key_schedule* key, int enc)
{
    DES  des;

    if (enc) {
        des.set_encryptKey(*key, 0);
        des.encrypt(*output, *input, DES_BLOCK);
    }
    else {
        des.set_decryptKey(*key, 0);
        des.decrypt(*output, *input, DES_BLOCK);
    }
}
コード例 #9
0
ファイル: DESEncry.cpp プロジェクト: modeyang/CardProject
bool CDESEncry::EncryFile(char *szFilename)
{
	std::locale::global(std::locale(""));
	std::string szInFile = szFilename;

	FILE *ff, *fp;
	ff = fopen(szFilename,"rb");
	if (ff == NULL)
	{
		//szInFile =  CMarkup::UTF8ToGB2312(szFilename);
		ff = fopen(szInFile.c_str(),"rb");
		if (ff == NULL)
		{
			return false;
		}

	}

	std::string strOutFile = szInFile;
	Transform_file(strOutFile, true);	
	fp = fopen(strOutFile.c_str(),"wb");
	if (fp == NULL)
	{
		fclose(ff);
		return false;
	}

	fseek(ff,0,SEEK_END);
	int fsize=ftell(ff);						//获得文件大小(相对于文件首的位移)
	fseek(ff,0,SEEK_SET);						
	fwrite(&fsize,sizeof(int),1,fp);			//写入文件大小
	//fwrite(C_Key,sizeof(char),sizeof(C_Key),fp);	//写入文件类型

	//开始加密
	int nRead;
	DES jm;
	char inbuff[8],outbuff[8];
	while(!feof(ff))
	{
		nRead = fread(inbuff,sizeof(char),8,ff);
		jm.Des_one(outbuff,inbuff,C_Key,ENCRYPT);	
		fwrite(outbuff,sizeof(char),8,fp);		//输出分密文分组
	}
	fclose(fp);
	fclose(ff);
	return true;
}
コード例 #10
0
ファイル: dialog.cpp プロジェクト: max1234/DES
void Dialog::on_ButtonDecrypt_clicked()
{    if(!inputFileName.isEmpty()\
       && !keyFileName.isEmpty()\
       && !outputFileName.isEmpty())
      if(inputFile.open(QIODevice::ReadOnly)\
        && keyFile.open(QIODevice::ReadOnly)\
        && outputFile.open(QIODevice::WriteOnly))
        {   pinputFile = &inputFile;
            pkeyFile = &keyFile;
            poutputFile = &outputFile;
            pvectorFile = NULL;
            if(ModeIndex)
            {if(!vectorFileName.isEmpty())
             {if(vectorFile.open(QIODevice::ReadOnly))
               {pvectorFile = &vectorFile;}
               else
                {ui->textBrowser->insertPlainText("Files open error\n");
                 exit(0);}}
                else
                {ui->textBrowser->insertPlainText("One or more files are missing\n");
                 exit(0);}}
            DES desMetod;
            desMetod.EncryptDecryptFlag=0;
            desMetod.ModeIndex=ModeIndex;
            if(desMetod.MainDES(pinputFile,pkeyFile,poutputFile,pvectorFile))
                ui->textBrowser->insertPlainText(desMetod.ErrorStr);
            else
                ui->textBrowser->insertPlainText("Decrypt success\n");
            inputFile.close();
            keyFile.close();
            outputFile.close();
            inputFileName = "";
            keyFileName = "";
            outputFileName = "";
            ui->LabelSelectInputFile->setText("Select input file");
            ui->LabelSelectKeyFile->setText("Select key file");
            ui->LabelSelectOutputFile->setText("Select output file");
            if(ModeIndex)
            {   vectorFile.close();
                vectorFileName = "";
                ui->LabelSelectVectorFile->setText("Select vector file");}}
          else
          ui->textBrowser->insertPlainText("Files open error\n");
        else
        ui->textBrowser->insertPlainText("One or more files are missing\n");}
コード例 #11
0
string EncryptionHandler::singleDecrypt(const string& cryptedMsg)
{
    DES des;
    des.SetKey(DES_KEY, 8);
    des.ProduceSubKey();
    
    CC_ASSERT(cryptedMsg.size() <= 8);
    for (int i = 0; i < cryptedMsg.size(); i++)
    {
        des.cryptedmsg[i] = cryptedMsg[i];
    }
    des.Char2Bit(des.cryptedmsg, des.bcryptedmsg, 8);
    
    des.Decipher();
    
    char msg[9] = {0};
    for (int i = 0; i < 8; i++)
    {
        msg[i] = des.decipher[i];
    }
    
    return string(msg);
}
コード例 #12
0
ファイル: DESEncry.cpp プロジェクト: modeyang/CardProject
bool CDESEncry::EncryString(char *szSource, char *szDest)
{
	char inbuff[8],outbuff[8];
	int nSourceLen = (int)strlen(szSource);
	int nStart = 0;
	DES jm;
	while (nStart < nSourceLen)
	{
		memcpy(inbuff, szSource + nStart, sizeof(inbuff));
		jm.Des_one(outbuff,inbuff,C_Key,ENCRYPT);
		memcpy(szDest + nStart, outbuff, sizeof(outbuff));
		nStart += sizeof(inbuff);
	}

	if (nSourceLen % 8 != 0)
	{
		nStart -= sizeof(inbuff);
		memcpy(inbuff, szSource + nStart, nSourceLen - nStart);
		jm.Des_one(outbuff,inbuff,C_Key,ENCRYPT);
		memcpy(szDest + nStart, outbuff, nSourceLen - nStart);
	}
	return true;
}
コード例 #13
0
ファイル: application.cpp プロジェクト: fcgdam/DES_Library
void tdesTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
                  0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
                };
  
  Serial.println();
  Serial.println("====== Triple-DES test ======");
  
  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  
  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
}
コード例 #14
0
ファイル: application.cpp プロジェクト: fcgdam/DES_Library
byte *decrypt(byte *dataIn, int len ) {
    unsigned int memsize = ((len/8)+1)*8; // round to the upper 8 byte size
    unsigned int nBlocks = ((len/8)+1);   // Number of blocks. There is always one block
    unsigned int boffset = 0;
    
    // WARNING WARNING WARNING WARNING
    byte *bout = (byte *)malloc(memsize); // THIS IS a very bad idea!!!! Just for testing. Allocation should be done OUTSIDE the function.
    
    while ( nBlocks > 0 ) {
        des.tripleDecrypt(&bout[boffset], &dataIn[boffset], key);
        boffset = boffset + 8;
        nBlocks = nBlocks - 1;
    };
    
    return bout;
    
}
コード例 #15
0
ファイル: TCCode.cpp プロジェクト: JohnWilliam1988/TCIDE
void CTCCode::DeCodeFile(wchar_t *InFile,wchar_t* key,wchar_t* OutFile)
{
	if(InFile&&key&&OutFile)
	{
		HANDLE infile=::CreateFile(InFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
		if(infile!=INVALID_HANDLE_VALUE)
		{
			HANDLE outfile=::CreateFile(OutFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
			if(outfile!=INVALID_HANDLE_VALUE)
			{
				DES des;
				//unicode 转 anscii
				int num=::WideCharToMultiByte (CP_ACP,0,key,-1,NULL,0,NULL,NULL);
				char* key_l = new char[num];
				memset(key_l, 0, num); //初始化动作
				WideCharToMultiByte (CP_ACP,0,key, num ,key_l, num ,NULL,NULL);
				long lFileLen=::GetFileSize(infile,NULL);
				char inbuff[MAXFILESIZE]={0};
				char oubuff[MAXFILESIZE]={0};
				DWORD ReadFileSize=0;
				DWORD DwWitten=0;
				int writSize = MAXFILESIZE;

				////////////////////////读取长度///////////////////////////
				lFileLen -= 4;//指定到自己写的结束符位置
				SetFilePointer(infile,lFileLen,0,FILE_CURRENT);//设置位置
				char inbufftem[4]={0};
				char *p=inbufftem;
				char endmark[2]={0xA3,0xA4};
				::ReadFile(infile,inbufftem,4,&ReadFileSize,NULL);
				int dd=0;

				if(inbufftem[0]==endmark[0] && inbufftem[1]==endmark[1])
				{
					p+=2;
					dd=atoi(p);
				}

				SetFilePointer(infile,0,0,FILE_BEGIN);//指针复位
				long count=lFileLen/MAXFILESIZE;	//整除,知道整个文件的块数

				for(long i=0;i<count;i++)
				{
					memset(inbuff,0,sizeof(inbuff));
					memset(oubuff,0,sizeof(oubuff));
					::ReadFile(infile,inbuff,MAXFILESIZE,&ReadFileSize ,NULL);
					des.Des_Go(oubuff, inbuff, sizeof(inbuff), key_l,sizeof(key_l), DECRYPT);
					if(i == count-1)//最后一次进来,处理oubuff值
					{
						if(dd==0)
							dd=MAXFILESIZE;
						::WriteFile(outfile,oubuff,dd,&DwWitten,NULL);//最后一次写正确的长度
						break;
					}
					::WriteFile(outfile,oubuff,MAXFILESIZE,&DwWitten,NULL);
				}
				delete []	key_l;
			}
			CloseHandle(outfile);
		}
		CloseHandle(infile);
	}

}