📄 xtunnelscvsdes.cpp
字号:
~STDESPrivateData();public: CVsInternalDES m_cDES; unsigned long m_dwLength; unsigned long m_dwBlockWidth; //unsigned char m_pBuffer[24]; char m_pBuffer[24]; unsigned long m_dwFilledSize; bool m_bNextIoFails;};STDESPrivateData::STDESPrivateData( const char * szKey, bool bEncrypt, unsigned long dwDES123, unsigned long dwLength123){ m_bNextIoFails = false; char pHashKey[24]; // room for largest DES mode memset(pHashKey, 0, sizeof(pHashKey)); m_dwFilledSize = 0; memset(m_pBuffer, 0, sizeof(m_pBuffer)); const char * szStr = szKey; char * szDupStr = new char[strlen(szStr) + 1]; strcpy(szDupStr, szStr); switch (dwDES123) { case 1: { m_cDES.makekey(szDupStr, pHashKey); /* password NULL terminated, triple-length key[8] */ m_cDES.deskey(pHashKey, bEncrypt ? EN0 : DE1); } case 2: { m_cDES.make2key(szDupStr, pHashKey); /* password NULL terminated, triple-length key[16] */ m_cDES.des2key(pHashKey, bEncrypt ? EN0 : DE1); break; } case 3: { m_cDES.make3key(szDupStr, pHashKey); /* password NULL terminated, triple-length key[24] */ m_cDES.des3key(pHashKey, bEncrypt ? EN0 : DE1); break; } default: { //VS_ASSERT(false) cout << "WARNING: Internal error in STDESPrivateData!!" << endl; } } m_dwLength = dwLength123; m_dwBlockWidth = (dwLength123 * 8); delete szDupStr;}STDESPrivateData::~STDESPrivateData(){}void CVsDES::Init(){ m_pData = NULL; m_iStream = NULL; m_oStream = NULL;}void CVsDES::Destroy(){ Close();}void CVsDES::OpenRead( const char * szKey, istrstream *ipInputStream, DESMode eMode, DESLength eLength){ Close(); if (NULL == ipInputStream) return; m_pData = new STDESPrivateData( szKey, false, (unsigned long)eMode, (unsigned long)eLength ); if (NULL == m_pData) return; m_iStream = ipInputStream; m_bReadMode = true;}void CVsDES::OpenWrite( const char * szKey, ostrstream *ipOuputStream, DESMode eMode, DESLength eLength){ Close(); if (NULL == ipOuputStream) return; m_pData = new STDESPrivateData( szKey, true, (unsigned long)eMode, (unsigned long)eLength ); if (NULL == m_pData) return; m_oStream = ipOuputStream; m_bReadMode = false;}bool CVsDES::Read( char *pBuffer, unsigned long dwBytesToRead, unsigned long *pBytesRead){ bool bReturnResult = false; unsigned long dwTotalBytesRead = 0; if (NULL == m_pData) return bReturnResult; if (NULL == m_iStream) return bReturnResult; if (!m_bReadMode) return bReturnResult; if (0 == dwBytesToRead) goto NEXT; while (0 != dwBytesToRead) { // still some stuff in the buffer from the last read while ((m_pData->m_dwFilledSize > 0) && (0 != dwBytesToRead)) { unsigned long dwReadSize = min<unsigned long>(dwBytesToRead, m_pData->m_dwFilledSize); // okay lets reads what's left in the buffer memcpy(pBuffer, m_pData->m_pBuffer, dwReadSize); pBuffer += dwReadSize; dwTotalBytesRead += dwReadSize; m_pData->m_dwFilledSize -= dwReadSize; if (m_pData->m_dwFilledSize < m_pData->m_dwBlockWidth) { // move any unread data in buffer over now... memcpy( m_pData->m_pBuffer, &(m_pData->m_pBuffer[dwReadSize]), m_pData->m_dwFilledSize ); } // decrease amount of buffer left in stream dwBytesToRead -= dwReadSize; } if (0 == dwBytesToRead) goto NEXT; if ((0 == dwTotalBytesRead) && (m_pData->m_bNextIoFails)) return bReturnResult; if (m_pData->m_bNextIoFails) goto NEXT; // fill the entire buffer all over again while (m_pData->m_dwFilledSize < m_pData->m_dwBlockWidth) { unsigned long dwActualBytesRead = 0; bool bResult = false; streamsize tAvailable = m_iStream->rdbuf()->in_avail(); if (tAvailable > 0) { streamsize tToRead = m_pData->m_dwBlockWidth - m_pData->m_dwFilledSize; tToRead = min<streamsize>(tToRead, tAvailable); m_iStream->read( &(m_pData->m_pBuffer[m_pData->m_dwFilledSize]), tToRead ); if (m_iStream->good()) { bResult = true; dwActualBytesRead = tToRead; m_pData->m_dwFilledSize += dwActualBytesRead; }#if DEBUG else cout << "WARNING: CVsDES::Read(): m_iStream->good() failed!" << endl;#endif // DEBUG } // zero out the rest of the buffer memset(&m_pData->m_pBuffer[m_pData->m_dwFilledSize], 0, m_pData->m_dwBlockWidth - m_pData->m_dwFilledSize); // the next read should fail as well if (!bResult) { m_pData->m_bNextIoFails = true; if ((0 == dwTotalBytesRead) && (0 == m_pData->m_dwFilledSize)) goto FINALLY; // this write fails NOW! else { if (0 == m_pData->m_dwFilledSize) goto NEXT; // no more data in DES buffer, but some data was read, next read will fail break; // some stuff in DES buffer, decrypt, next read will fail } } if ((0 == dwActualBytesRead) && (0 == m_pData->m_dwFilledSize)) goto NEXT; // at end of file if (0 == dwActualBytesRead) // at end of file, but stuff in DES buffer, decrypt break; } switch (m_pData->m_dwLength) { case 1: { m_pData->m_cDES.Ddes(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 2: { m_pData->m_cDES.D2des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 3: { m_pData->m_cDES.D3des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } } } NEXT: { if (NULL != pBytesRead) *pBytesRead = dwTotalBytesRead; bReturnResult = true; } FINALLY: { } return bReturnResult;}bool CVsDES::Write( const char *pBuffer, unsigned long dwBufferLength, unsigned long *pBytesWritten){ bool bReturnResult = false; if (NULL == m_pData) return bReturnResult; if (NULL == m_oStream) return bReturnResult; if (m_bReadMode) return bReturnResult; if (m_pData->m_bNextIoFails) return bReturnResult; // keep writting while there is more buffer to write while (dwBufferLength > 0) { // fill the DES buffer to max capacity unsigned long dwFillAmount = min<unsigned long>(dwBufferLength, m_pData->m_dwBlockWidth - m_pData->m_dwFilledSize); memcpy(&(m_pData->m_pBuffer[m_pData->m_dwFilledSize]), pBuffer, dwFillAmount); pBuffer += dwFillAmount; dwBufferLength -= dwFillAmount; m_pData->m_dwFilledSize += dwFillAmount; if (m_pData->m_dwFilledSize < m_pData->m_dwBlockWidth) goto NEXT; // not enough data to fill the entire buffer with, wait until next fill // encrypt the data now switch (m_pData->m_dwLength) { case 1: { m_pData->m_cDES.Ddes(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 2: { m_pData->m_cDES.D2des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 3: { m_pData->m_cDES.D3des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } } // keep trying to output the buffer into the output stream while (m_pData->m_dwFilledSize > 0) { unsigned long dwActualBytesWritten = 0; dwActualBytesWritten = m_pData->m_dwFilledSize; bool bResult = NULL != m_oStream->write(m_pData->m_pBuffer, dwActualBytesWritten); if (pBytesWritten) *pBytesWritten += dwActualBytesWritten; if ((!bResult) || (0 == dwActualBytesWritten)) { m_pData->m_bNextIoFails = true; goto FINALLY; } m_pData->m_dwFilledSize -= dwActualBytesWritten; if (0 != m_pData->m_dwFilledSize) { // shift the buffer over memcpy(m_pData->m_pBuffer, &(m_pData->m_pBuffer[dwActualBytesWritten]), m_pData->m_dwFilledSize); } } } NEXT: { bReturnResult = true; } FINALLY: { } return bReturnResult;}void CVsDES::Close(unsigned long *pBytesWritten){ if (NULL != m_pData) { if (!m_bReadMode) { if (!m_pData->m_bNextIoFails) { // still some data left in DES buffer to write? if (m_pData->m_dwFilledSize > 0) { // zero out any left over bits... memset(&(m_pData->m_pBuffer[m_pData->m_dwFilledSize]), 0, m_pData->m_dwBlockWidth - m_pData->m_dwFilledSize); m_pData->m_dwFilledSize += (m_pData->m_dwBlockWidth - m_pData->m_dwFilledSize); // encrypt the data now switch (m_pData->m_dwLength) { case 1: { m_pData->m_cDES.Ddes(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 2: { m_pData->m_cDES.D2des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } case 3: { m_pData->m_cDES.D3des(m_pData->m_pBuffer, m_pData->m_pBuffer); break; } } // keep trying to output the buffer into the output stream while (m_pData->m_dwFilledSize > 0) { unsigned long dwActualBytesWritten = 0; dwActualBytesWritten = m_pData->m_dwFilledSize; bool bResult = NULL != m_oStream->write(m_pData->m_pBuffer, dwActualBytesWritten); if (pBytesWritten) *pBytesWritten += dwActualBytesWritten; if ((!bResult) || (0 == dwActualBytesWritten)) { break; // ohh well... } m_pData->m_dwFilledSize -= dwActualBytesWritten; if (0 != m_pData->m_dwFilledSize) { // shift the buffer over memcpy(m_pData->m_pBuffer, &(m_pData->m_pBuffer[dwActualBytesWritten]), m_pData->m_dwFilledSize); } } } } } delete m_pData; m_pData = NULL; } m_oStream = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -