⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirtypaddoc.cpp

📁 本代码应用先验式启发算法再对文挡进行加密处理
💻 CPP
字号:
// dirtypadDoc.cpp : implementation of the CDirtypadDoc class
//

#include "stdafx.h"
#include "dirtypad.h"

#include "dirtypadDoc.h"

#pragma warning( push, 3 )
#include "sha.h"
#pragma warning( pop )

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDirtypadDoc

IMPLEMENT_DYNCREATE(CDirtypadDoc, CDocument)

BEGIN_MESSAGE_MAP(CDirtypadDoc, CDocument)
	//{{AFX_MSG_MAP(CDirtypadDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDirtypadDoc construction/destruction

CDirtypadDoc::CDirtypadDoc()
{
	// TODO: add one-time construction code here

}

CDirtypadDoc::~CDirtypadDoc()
{
}

BOOL CDirtypadDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);

    CString szText;
    GetText( szText );

    CalculateDigest( szText, szText.GetLength(), m_pcbDigest );
    m_ulLength = szText.GetLength();

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CDirtypadDoc serialization

void CDirtypadDoc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CDirtypadDoc diagnostics

#ifdef _DEBUG
void CDirtypadDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDirtypadDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDirtypadDoc commands


BOOL CDirtypadDoc::SaveModified() 
{
    BYTE pcbDigest[ CryptoPP::SHA256::DIGESTSIZE ];
    CString szText;
    
    // Get a copy of the Documet from
    //   the View Class
    if( FALSE == GetText( szText ) ){
	
	    return CDocument::SaveModified();
    }
    
    // The document must have changed if
    //   the lengths are different
    if( m_ulLength != szText.GetLength() ) {

	    return CDocument::SaveModified();    
    }

    // Attempt to Calculate the Digest
    if( FALSE == CalculateDigest( szText, szText.GetLength(), pcbDigest ) ){
	
	    return CDocument::SaveModified();
    }

   // Compare Hashes
    if( 0 != memcmp( m_pcbDigest, pcbDigest, CryptoPP::SHA256::DIGESTSIZE ) ) {

        return CDocument::SaveModified();
    }

    // Any non-zero will do
    return TRUE;
}

BOOL CDirtypadDoc::CalculateDigest( LPCTSTR pszText, UINT nLength, UCHAR *pcbDigest )
{
    try {

        CryptoPP::SHA256 hash;
        hash.Update( (UCHAR*)pszText, nLength * sizeof(TCHAR) );
        hash.Final( pcbDigest );

    } catch( ... ) {

        return FALSE;
    }

    return TRUE;
}

BOOL CDirtypadDoc::GetText(CString& szText) {

    POSITION pos = GetFirstViewPosition();
    ASSERT( NULL != pos );
    if( NULL == pos ) { return FALSE; }

    CEditView* pView = static_cast<CEditView*>( GetNextView( pos ) );
    ASSERT( NULL != pView );
    if( NULL == pView ) { return FALSE; }

    pView->GetEditCtrl().GetWindowText( szText );

    return TRUE;
}

BOOL CDirtypadDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

    CString szText;
    GetText( szText );

    CalculateDigest( szText, szText.GetLength(), m_pcbDigest );
    m_ulLength = szText.GetLength();
	
	return TRUE;
}

BOOL CDirtypadDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
    CString szText;
    GetText( szText );

    CalculateDigest( szText, szText.GetLength(), m_pcbDigest );
    m_ulLength = szText.GetLength();
	
	return CDocument::OnSaveDocument(lpszPathName);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -