📄 yxydes.cpp
字号:
char sz_Ri[32] = {0};
char sz_Final64[64] = {0};
char szPlaintextBinary[64] = {0};
//IP --- return is sz_IP
InitialPermuteData(s,sz_IP,false);
//divide the 64 bits data to two parts
memcpy(sz_Ri,sz_IP,32); //exchange L to R
memcpy(sz_Li,sz_IP + 32,32); //exchange R to L
//16 rounds F and xor and exchange
for(int i=0;i<16;i++)
{
FunctionF(sz_Ri,sz_Li,15-i);//原版解密时FunctionF(sz_Li,sz_Ri,15-i)左右顺序弄反了
}
//the round 16 will not exchange L and R
memcpy(sz_Final64,sz_Li,32);//原版 memcpy(sz_Final64,sz_Ri,32)左右顺序弄反
memcpy(sz_Final64 + 32,sz_Ri,32);//原版 memcpy(sz_Final64 + 32,sz_Li,32)左右顺序弄反
// ~IP
for(int j=0;j<64;j++)
{
szPlaintextBinary[j] = sz_Final64[IPR_Table[j]-1];
}
memcpy(szPlaintext,BinaryToString(szPlaintextBinary,64,false).c_str(),8);
}
void yxyDES::FunctionF(char* sz_Li,char* sz_Ri,int iKey)
{
char sz_48R[48] = {0};
char sz_xor48[48] = {0};
char sz_P32[32] = {0};
char sz_Rii[32] = {0};
char sz_Key[48] = {0};
memcpy(sz_Key,SubKeys[iKey],48);
ExpansionR(sz_Ri,sz_48R);
XOR(sz_48R,sz_Key,48,sz_xor48);
string s_Compress32 = CompressFuncS(sz_xor48);
PermutationP(s_Compress32,sz_P32);
XOR(sz_P32,sz_Li,32,sz_Rii);
memcpy(sz_Li,sz_Ri,32);
memcpy(sz_Ri,sz_Rii,32);
}
void yxyDES::InitialPermuteData(string s,char* Return_value,bool bType)
{
//if bType==true is encrypt
//else is decrypt
if(bType)
{
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)]);
//IP
char sz_IPData[64] = {0};
for(int k=0;k<64;k++)
{
sz_IPData[k] = sz_64data[IP_Table[k]-1];
}
memcpy(Return_value,sz_IPData,64);
}
else
{
string sz_64data;
for(unsigned int ui=0;ui<s.length();ui++)
{
char ch = s[ui];
sz_64data += HexCharToBinary(tolower(ch));
}
char sz_IPData[64] = {0};
for(int i=0;i<64;i++)
{
sz_IPData[i] = sz_64data[IP_Table[i]-1];
}
memcpy(Return_value,sz_IPData,64);
}
}
void yxyDES::ExpansionR(char* sz,char* Return_value)
{
char sz_48ER[48] = {0};
for(int i=0;i<48;i++)
{
sz_48ER[i] = sz[E_Table[i]-1];
}
memcpy(Return_value,sz_48ER,48);
}
void yxyDES::XOR(char* sz_P1,char* sz_P2,int len,char* Return_value)
{
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)
{
int iLength = szSource.length();
//if the length is 8 , call EncyptData
if(iLength == 8)
{
EncryptData(szSource);
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);
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);
//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);
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
char temp[8]={0};
for(int i = 0;i<sz.length();i++)//把SZ的内容存储到临时数组中
{
temp[i] = sz[i];
}
for(int j = sz.length();j<8;j++) //增加SZ长度,
{
sz +="0";
}
for(int k = 0;k<8;k++)//把数组中的字符全部给SZ,相当于不足的填0
{
sz[k] = temp[k];
}
return sz;
}
char* yxyDES::GetCiphertextAnyLength()
{
return szFCiphertextAnyLength;
}
void yxyDES::DecryptAnyLength(string szSource)
{
int iLength = szSource.length();
int iRealLengthOfPlaintext = 0;
//if the length is 16 , call DecyptData
if(iLength == 16)
{
DecryptData(szSource);
memcpy(szFPlaintextAnyLength,szPlaintext,8);
iRealLengthOfPlaintext = 8;
}
//it's not impossible the length less than 16
else if(iLength < 16)
{
sprintf(szFPlaintextAnyLength,"待解密字符长度必须为16的倍数!");
}
//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,"待解密字符长度必须为16的倍数!");
return;
}
iRealLengthOfPlaintext = iParts * 8;
//Decrypt the data 16 by 16
for(int i=0;i<iParts;i++)
{
string szTmp = szSource.substr(i*16,16);
DecryptData(szTmp);
//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,'$');//原版的清除美元符号
char* pDest =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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -