📄 yxydes.cpp
字号:
char sz_Buffer[256] = {0};
for(int i=0;i<len;i++)
{
sz_Buffer[i] = SingleBinaryToChar(SingleCharToBinary(sz_P1[i]) ^ SingleCharToBinary(sz_P2[i]));
}
memcpy(Return_value,sz_Buffer,len);
}
string yxyDES::CompressFuncS(char* sz_48)
{
char sTmp[8][6] = {0};
string sz_32 = "";
for(int i=0;i<8;i++)
{
memcpy(sTmp[i],sz_48 + 6 * i,6);
int iX = SingleCharToBinary(sTmp[i][0]) * 2 + SingleCharToBinary(sTmp[i][5]);
int iY = 0;
for(int j=1;j<5;j++)
{
iY += SingleCharToBinary(sTmp[i][j]) * pow(2.0,4-j);
}
sz_32 += HexIntToBinary(S_Box[i][iX][iY]);
}
return sz_32;
}
void yxyDES::PermutationP(string s,char* Return_value)
{
char sz_32bits[32] = {0};
for(int i=0;i<32;i++)
{
sz_32bits[i] = s[P_Table[i]-1];
}
memcpy(Return_value,sz_32bits,32);
}
int yxyDES::SingleCharToBinary(char ch)
{
if(ch == '1')
return 1;
else
return 0;
}
char yxyDES::SingleBinaryToChar(int i)
{
if(i == 1)
return '1';
else
return '0';
}
void yxyDES::SetCiphertext(char* value)
{
memcpy(szCiphertext,value,16);
}
char* yxyDES::GetCiphertext()
{
return szCiphertext;
}
void yxyDES::SetPlaintext(char* value)
{
memcpy(szPlaintext,value,8);
}
char* yxyDES::GetPlaintext()
{
return szPlaintext;
}
//any length
void yxyDES::EncryptAnyLength(string szSource,bool bIsUseSecondKey)
{
int iLength = szSource.length();
//if the length is 8 , call EncyptData
if(iLength == 8)
{
EncryptData(szSource,bIsUseSecondKey,true);
memcpy(szFCiphertextAnyLength,szCiphertext,16);
//set the last char to '\0'
szFCiphertextAnyLength[16] = '\0';
}
//else if the length less than 8
//call FillToEightBits
else if(iLength < 8)
{
szSource = FillToEightBits(szSource);
EncryptData(szSource,bIsUseSecondKey,true);
memcpy(szFCiphertextAnyLength,szCiphertext,16);
//set the last char to '\0'
szFCiphertextAnyLength[16] = '\0';
}
//else if then lenth bigger than 8
//divide the string to multi-parts
else if(iLength > 8)
{
int iParts = ceil(iLength/8.0);
int iResidue = iLength % 8;
string szLast8Bits;
//encrypt the data 8 by 8 except the last part
for(int i=0;i<iParts-1;i++)
{
string szTmp = szSource.substr(i*8,8);
EncryptData(szTmp,bIsUseSecondKey,true);
//after call EncryptData
//cpoy the temp result to szFCiphertextAnyLength
memcpy(szFCiphertextAnyLength + 16*i,szCiphertext,16);
}
//now , the last part
if(iResidue != 0) //can't be divided exactly by 8
{
szLast8Bits = szSource.substr((iParts-1)*8,iResidue);
szLast8Bits = FillToEightBits(szLast8Bits);
}
else //be divided exactly by 8
{
szLast8Bits = szSource.substr((iParts-1)*8,8);
}
//encrypt the last part
EncryptData(szLast8Bits,bIsUseSecondKey,true);
memcpy(szFCiphertextAnyLength + 16*(iParts - 1),szCiphertext,16);
//set the last char to '\0'
szFCiphertextAnyLength[(iParts)*16] = '\0';
}
}
//fill the data to 8 bits
string yxyDES::FillToEightBits(string sz)
{
//length less than 8 , add zero(s) to tail
switch(sz.length())
{
case 7:
sz += "$";
break;
case 6:
sz += "$$";
break;
case 5:
sz += "$$$";
break;
case 4:
sz += "$$$$";
break;
case 3:
sz += "$$$$$";
break;
case 2:
sz += "$$$$$$";
break;
case 1:
sz += "$$$$$$$";
break;
default:
break;
}
return sz;
}
char* yxyDES::GetCiphertextAnyLength()
{
return szFCiphertextAnyLength;
}
void yxyDES::DecryptAnyLength(string szSource,bool bIsUseSecondKey)
{
int iLength = szSource.length();
int iRealLengthOfPlaintext = 0;
//if the length is 16 , call DecyptData
if(iLength == 16)
{
DecryptData(szSource,bIsUseSecondKey,true);
memcpy(szFPlaintextAnyLength,szPlaintext,8);
iRealLengthOfPlaintext = 8;
}
//it's not impossible the length less than 16
else if(iLength < 16)
{
sprintf(szFPlaintextAnyLength,"Please enter your correct cipertext!");
}
//else if then lenth bigger than 16
//divide the string to multi-parts
else if(iLength > 16)
{
int iParts = ceil(iLength/16.0);
int iResidue = iLength % 16;
//if the data can't be divided exactly by 16
//it's meaning the data is a wrong !
if(iResidue != 0)
{
sprintf(szFPlaintextAnyLength,"Please enter your correct cipertext!");
return;
}
iRealLengthOfPlaintext = iParts * 8;
//encrypt the data 16 by 16
for(int i=0;i<iParts;i++)
{
string szTmp = szSource.substr(i*16,16);
DecryptData(szTmp,bIsUseSecondKey,true);
//after call DecryptData
//cpoy the temp result to szFPlaintextAnyLength
memcpy(szFPlaintextAnyLength + 8*i,szPlaintext,8);
}
}
//find and clear the mark
//which is add by program when ciphertext is less than 8
CleanPlaintextMark(iRealLengthOfPlaintext);
}
char* yxyDES::GetPlaintextAnyLength()
{
return szFPlaintextAnyLength;
}
void yxyDES::CleanPlaintextMark(int iPlaintextLength)
{
if(iPlaintextLength > 0 && iPlaintextLength < 8192)
{
szFPlaintextAnyLength[iPlaintextLength] = '\0';
}
char szLast7Chars[7] = {0};
memcpy(szLast7Chars,szFPlaintextAnyLength + iPlaintextLength - 7,7);
for(int i=0;i<7;i++)
{
char* pDest = strrchr(szLast7Chars,'$');
if(pDest == NULL)
{
break;
}
else
{
int iPos = (int)(pDest - szLast7Chars + 1);
if(iPos != 7 - i)
{
break;
}
else
{
szLast7Chars[6-i]='\0';
continue;
}
}
}
memcpy(szFPlaintextAnyLength + iPlaintextLength - 7,szLast7Chars,7);
}
void yxyDES::TripleEncryptAnyLength(string szSource)
{
//E(key1)-D(key2)-E(key1)
EncryptAnyLength(szSource,false);
DecryptAnyLength(szFCiphertextAnyLength,true);
EncryptAnyLength(szFPlaintextAnyLength,false);
}
void yxyDES::TripleDecryptAnyLength(string szSource)
{
//D(key1)-E(key2)-D(key1)
DecryptAnyLength(szSource,false);
EncryptAnyLength(szFPlaintextAnyLength,true);
DecryptAnyLength(szFCiphertextAnyLength,false);
}
void yxyDES::TranslateBytes2Bits(string s, char* ReturnValue)
{
char sz_64data[64] = {0};
int iTmpBit[64] = {0};
for(int i=0;i<64;i++)
{
iTmpBit[i] = (s[i>>3]>>(i&7)) & 1;
//a = 0x61 = 0110,0001
//after this , a is 1000,0110
}
//let me convert it to right
for(int j=0;j<8;j++)
for(int k=0;k<8;k++)
sz_64data[8*j+k] = SingleBinaryToChar(iTmpBit[8*(j+1)-(k+1)]);
/*for(int k=0;k<64;k++)
{
sz_64data[k] = SingleBinaryToChar(iTmpBit[k]);
}*/
memcpy(ReturnValue,sz_64data,64);
}
void yxyDES::TranslateBits2Bytes(char* sz64data, char* ReturnValue)
{
//a = 0x61 = 0110,0001
//convert a to 1000,0110 first
int iTmpBit[64] = {0};
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
iTmpBit[8*i+j] = SingleCharToBinary(sz64data[8*(i+1)-(j+1)]);
/*for(int m=0;m<64;m++)
{
iTmpBit[m] = SingleCharToBinary(sz64data[m]);
}*/
memset(ReturnValue, 0, 8);
for(int k=0; k<64; k++)
ReturnValue[k>>3] |= iTmpBit[k]<<(k&7);
}
void yxyDES::TripleEncrypt(string szSource,bool bShowResultInHex)
{
//E(key1)-D(key2)-E(key1)
EncryptData(szSource,false,bShowResultInHex);
DecryptData(szCiphertext,true,bShowResultInHex);
EncryptData(szPlaintext,false,bShowResultInHex);
}
void yxyDES::TripleDecrypt(string szSource,bool bDecryptHex)
{
//D(key1)-E(key2)-D(key1)
DecryptData(szSource,false,bDecryptHex);
EncryptData(szPlaintext,true,bDecryptHex);
DecryptData(szCiphertext,false,bDecryptHex);
}
bool yxyDES::FileEncrypt(string szFilePath,bool bUse3DES)
{
fstream file_source;
fstream file_dest;
string szDestFilePath = szFilePath + ".YXYDES";
//open source file
file_source.open(szFilePath.c_str(),ios::binary|ios::in);
if(file_source.fail())
return false;
//open dest file
file_dest.open(szDestFilePath.c_str(),ios::binary|ios::out);
if(file_dest.fail())
{
file_source.close();
return false;
}
//read the source file size
file_source.seekg(0,ios::end);
long lFileLength = file_source.tellg();
file_source.seekg(0,ios::beg);
long iParts = lFileLength / 8;
int iResidue = lFileLength % 8;
char buf_read[8] = {0};
for(int i=0;i<iParts;i++)
{
//read data from source file
file_source.read(buf_read,8);
string tmp = buf_read;
if(bUse3DES) //if 3DES
{
TripleEncrypt(tmp,false);
}
else
{
EncryptData(tmp,false,false);
}
//write data to dest file
file_dest.write(szCiphertext,8);
}
//the last part
char ch_tmp;
for(int j=0;j<iResidue;j++)
{
file_source.get(ch_tmp);
file_dest.put(ch_tmp);
}
file_source.close();
file_dest.close();
return true;
}
bool yxyDES::FileDecrypt(string szFilePath,bool bUse3DES)
{
fstream file_source;
fstream file_dest;
string szDestFilePath = szFilePath.substr(0,szFilePath.length() - 6);
//open source file
file_source.open(szFilePath.c_str(),ios::binary|ios::in);
if(file_source.fail())
return false;
//open dest file
file_dest.open(szDestFilePath.c_str(),ios::binary|ios::out);
if(file_dest.fail())
{
file_source.close();
return false;
}
//read the source file size
file_source.seekg(0,ios::end);
long lFileLength = file_source.tellg();
file_source.seekg(0,ios::beg);
long iParts = lFileLength / 8;
int iResidue = lFileLength % 8;
char buf_read[8] = {0};
for(int i=0;i<iParts;i++)
{
//read data from source file
file_source.read(buf_read,8);
string tmp = buf_read;
if(bUse3DES) //if 3DES
{
TripleDecrypt(tmp,false);
}
else
{
DecryptData(tmp,false,false);
}
//write data to dest file
file_dest.write(szPlaintext,8);
}
//the last part
char ch_tmp;
for(int j=0;j<iResidue;j++)
{
file_source.get(ch_tmp);
file_dest.put(ch_tmp);
}
file_source.close();
file_dest.close();
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -