本文整理汇总了C++中DES类的典型用法代码示例。如果您正苦于以下问题:C++ DES类的具体用法?C++ DES怎么用?C++ DES使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DES类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: singleEncrypt
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: desTest
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: fopen
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: memcpy
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: EnCodeFile
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);
}
}
示例6: main
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;
}
示例7: DES_ecb_encrypt
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);
}
}
示例8: if
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");}
示例9: singleDecrypt
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);
}
示例10: EncryString
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;
}
示例11: tdesTest
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);
}
示例12: DeCodeFile
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);
}
}
示例13: while
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;
}