各位大侠,我有段加密解密程序如下,vc++6.0编写,请帮忙分析一下原理和主要算法,谢了先
#include "stdafx.h"
#include "Crypt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCrypt
CCrypt::CCrypt()
:
m_LFSR_A( 0x13579BDF ),
m_LFSR_B( 0x2468ACE0 ),
m_LFSR_C( 0xFDB97531 ),
m_Mask_A( 0x80000062 ),
m_Mask_B( 0x40000020 ),
m_Mask_C( 0x10000002 ),
m_Rot0_A( 0x7FFFFFFF ),
m_Rot0_B( 0x3FFFFFFF ),
m_Rot0_C( 0x0FFFFFFF ),
m_Rot1_A( 0x80000000 ),
m_Rot1_B( 0xC0000000 ),
m_Rot1_C( 0xF0000000 )
{
m_csKey = _T("");
}
// Everything is on the frame.
CCrypt::~CCrypt()
{
}
/////////////////////////////////////////////////////////////////////////////
// CCrypt operations
void CCrypt::SetKey(LPCTSTR csKey)
{
CString csSeed;
m_csKey = csKey;
if (m_csKey.IsEmpty())
{
csSeed = "\x43\xC8\x21\xD3\xF4\xB3\x10\x27\x09\xAA\x18\x56";
}
else
{
csSeed = m_csKey;
}
int nIdx = 0;
for (nIdx = 0; csSeed.GetLength() < 12; nIdx++)
{
csSeed += csSeed[nIdx];
}
for (nIdx = 0; nIdx < 4; nIdx++)
{
m_LFSR_A = ((m_LFSR_A <<= 8) |
((unsigned long int) csSeed[nIdx + 0]));
m_LFSR_B = (( m_LFSR_B <<= 8) |
((unsigned long int) csSeed[nIdx + 4]));
m_LFSR_C = (( m_LFSR_C <<= 8) |
((unsigned long int) csSeed[nIdx + 8]));
}
if (0x00000000 == m_LFSR_A)
m_LFSR_A = 0x13579BDF;
if (0x00000000 == m_LFSR_B)
m_LFSR_B = 0x2468ACE0;
if (0x00000000 == m_LFSR_C)
m_LFSR_C = 0xFDB97531;
}
void CCrypt::GetKey(CString& csKey)
{
csKey = m_csKey;
}
void CCrypt::TransformChar(unsigned char& csTarget)
{
int Counter = 0;
unsigned char Crypto = '\0';
unsigned long int Out_B = (m_LFSR_B & 0x00000001);
unsigned long int Out_C = (m_LFSR_C & 0x00000001);
for (Counter = 0; Counter < 8; Counter++)
{
if (m_LFSR_A & 0x00000001)
{
m_LFSR_A = (((m_LFSR_A ^ m_Mask_A) >> 1) | m_Rot1_A);
if ( m_LFSR_B & 0x00000001 )
{
m_LFSR_B = (((m_LFSR_B ^ m_Mask_B) >> 1) | m_Rot1_B);
Out_B = 0x00000001;
}
else
{
m_LFSR_B = (( m_LFSR_B >> 1) & m_Rot0_B);
Out_B = 0x00000000;
}
}
else
{
m_LFSR_A = (( m_LFSR_A >> 1) & m_Rot0_A);
if ( m_LFSR_C & 0x00000001 )
{
m_LFSR_C = ((( m_LFSR_C ^ m_Mask_C) >> 1) | m_Rot1_C);
Out_C = 0x00000001;
}
else
{
m_LFSR_C = ((m_LFSR_C >> 1) & m_Rot0_C);
Out_C = 0x00000000;
}
}
#pragma warning(disable : 4244)
Crypto = ((Crypto << 1) | (Out_B ^ Out_C));
#pragma warning(default : 4244)
}
#pragma warning(disable : 4244)
csTarget = ( csTarget ^ Crypto );
if (csTarget == NULL)
csTarget = ( csTarget ^ Crypto );
#pragma warning( default : 4244 )
}
void CCrypt::TransformString(LPCTSTR csKey, CString& csTarget)
{
SetKey(csKey);
int nLen = csTarget.GetLength();
#pragma warning(disable : 4244)
for (int nPos = 0; nPos < nLen; nPos++)
{
unsigned char cBuff = csTarget.GetAt(nPos);
TransformChar((unsigned char&) cBuff);
csTarget.SetAt(nPos, cBuff);
}
#pragma warning(default : 4244)
}
CString CCrypt::StrToHex(LPCTSTR strStr)
{
CString strRet;
for(UINT i = 0; i < strlen(strStr); i++)
{
CString strTmp;
strTmp.Format("%02X",(BYTE)strStr[i]);
strRet += strTmp;
}
return strRet;
}
CString CCrypt::HexToStr(LPCTSTR strHex)
{
UINT uLen = strlen(strHex);
if(uLen % 2 != 0)
return "";
CString strRet;
for(UINT i = 0; i < uLen; i = i + 2)
{
char cTmp = (strHex[i] - ((strHex[i] >= 'A') ? ('A' - 10) : '0')) * 16 + (strHex[i + 1] - ((strHex[i + 1] >= 'A') ? ('A' - 10) : '0'));
strRet += cTmp;
}
return strRet;
}
void CCrypt::Encode(LPCTSTR csKey, CString& csTarget)
{
TransformString(csKey,csTarget);
csTarget = StrToHex(csTarget);
}
void CCrypt::Decode(LPCTSTR csKey, CString& csTarget)
{
csTarget = HexToStr(csTarget);
TransformString(csKey,csTarget);
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)