密码体制MFC 第3页
// duicheng.cpp : 实现文件
//
#include "stdafx.h"
#include "0408008404.h"
#include "duicheng.h"
#include ".\duicheng.h"
// duicheng 对话框
IMPLEMENT_DYNAMIC(duicheng, CDialog)
duicheng::duicheng(CWnd* pParent /*=NULL*/)
: CDialog(duicheng::IDD, pParent)
, m_strFileName(_T(""))
, m_strFileContents(_T(""))
{
TCHAR buff[MAX_PATH];
GetModuleFileName(NULL,buff,MAX_PATH);
m_strWorkingDir=System::IO::Path::GetDirectoryName(buff);
}
duicheng::~duicheng()
{
}
void duicheng::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strFileName);
DDX_Control(pDX, IDC_EDIT1, m_edtFileName);
DDX_Text(pDX, IDC_EDIT2, m_strFileContents);
DDX_Control(pDX, IDC_LIST3, m_lbxStatus);
}
BEGIN_MESSAGE_MAP(duicheng, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_Encrypt, OnBnClickedEncrypt)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDC_Decrypt, OnBnClickedDecrypt)
END_MESSAGE_MAP()
// duicheng 消息处理程序
void duicheng::OnBnClickedButton1()
{
UpdateData();
CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrInitialDir = m_strWorkingDir;
dlg.m_ofn.lpstrFilter =
_T("Text Files (*.txt)\0*.txt\0"
"Encrypted Files (*.enc)\0*.enc\0"
"Decrypted Files (*.dec)\0*.dec\0"
"All Files (*.*)\0*.*\0"
);
if (IDOK == dlg.DoModal())
{
m_strFileName = dlg.GetPathName();
UpdateData(FALSE);
m_edtFileName.SetSel(m_strFileName.GetLength(),
m_strFileName.GetLength(),
FALSE);
}
}
void duicheng::OnBnClickedCancel()
{
DestroyWindow();
delete this;
}
void EncryptFile(String* inputFileName,String* outputFileName)
{
#pragma push_macro("new")
#undef new
CryptoStream *cryptoStream;
FileStream* outputFileStream;
FileStream* inputFileStream;
try
{
String* keyString = S"KeyAbcGG";
Byte keyByteArray[] = Text::Encoding::Default->GetBytes(keyString);
inputFileStream = new FileStream(inputFileName,
FileMode::Open,
FileAccess::Read);
outputFileStream = new FileStream(outputFileName,
FileMode::Create,
FileAccess::Write);
DESCryptoServiceProvider *serviceProvider =
new DESCryptoServiceProvider();
ICryptoTransform* encryptor =
serviceProvider->CreateEncryptor(keyByteArray,keyByteArray);
cryptoStream = new CryptoStream(outputFileStream,
encryptor,
CryptoStreamMode::Write);
Byte bytesread[] = new Byte[129];
while(int n = inputFileStream->Read(bytesread, 0, 128))
{
cryptoStream->Write(bytesread, 0, n);
}
}
catch(Exception* e)
{
throw e;
}
__finally
{
cryptoStream->Close();
outputFileStream->Close();
inputFileStream->Close();
}
#pragma pop_macro("new")
}
void duicheng::OnBnClickedEncrypt()
{
#pragma push_macro("new")
#undef new
CWaitCursor wc;
UpdateData();
if (0 < m_strFileName.GetLength())
{
FileStream* pFS;
try
{
CString strStatus;
m_strFileContents = _T("");
CString strOutputFile = m_strFileName;
if (0 == strOutputFile.Right(4).CompareNoCase(_T(".dec")))
{
strOutputFile =
strOutputFile.Left(strOutputFile.GetLength() - 4)
+ _T(".enc");
}
else
{
strOutputFile += _T(".enc");
}
strStatus.Format(_T("Encrypting file %s"), m_strFileName);
EncryptFile(m_strFileName, strOutputFile);
strStatus.Format(_T("%s successfully encrypted to file %s"),
m_strFileName,
strOutputFile);
m_lbxStatus.InsertString(0, strStatus);
m_lbxStatus.InsertString(0, _T("Attempting to read newly "
"encrypted file"));
pFS = new FileStream(strOutputFile,
FileMode::Open,
FileAccess::Read);
if (pFS->CanRead)
{
Byte buffer __gc[] = new Byte __gc[pFS->Length];
pFS->Read(buffer, 0, buffer->Length);
for (int i = 0; i < buffer->Length; i++)
{
CString c;
c.Format(_T("%02x "), buffer[i]);
m_strFileContents += c;
}
m_lbxStatus.InsertString(0, _T("Data read back and displayed"));
m_strFileName = strOutputFile;
}
}
catch(Exception* e)
{
m_lbxStatus.InsertString(0, (CString)e->Message);
}
__finally
{
pFS->Close();
}
UpdateData(FALSE);
}
else
{
MessageBox::Show(S"You must supply an input file name.");
}
#pragma pop_macro("new")
}
void DecryptFile(String* inputFileName,String* outputFileName)
{
#pragma push_macro("new")
#undef new
CryptoStream *cryptoStream;
FileStream* outputFileStream;
FileStream* inputFileStream;
try
{
String* keyString = S"KeyAbcGG";
Byte keyByteArray[] = Text::Encoding::Default->GetBytes(keyString);
inputFileStream = new FileStream(inputFileName,
FileMode::Open,
FileAccess::Read);
outputFileStream = new FileStream(outputFileName,
FileMode::Create,
FileAccess::Write);
DESCryptoServiceProvider* serviceProvider =
new DESCryptoServiceProvider();
ICryptoTransform* decryptor =
serviceProvider->CreateDecryptor(keyByteArray, keyByteArray);
cryptoStream = new CryptoStream(inputFileStream,
decryptor,
CryptoStreamMode::Read);
Byte bytesRead[] = new Byte[129];
while(int n = cryptoStream->Read(bytesRead, 0, 128))
{
outputFileStream->Write(bytesRead,0,n);
}
}
catch(Exception* e)
{
throw e;
}
__finally
{
cryptoStream->Close();
outputFileStream->Close();
inputFileStream->Close();
}
#pragma pop_macro("new")
}
void duicheng::OnBnClickedDecrypt()
{
#pragma push_macro("new")
#undef new
CWaitCursor wc;
UpdateData();
if (0 < m_strFileName.GetLength())
{
StreamReader* pSR;
try
{
CString strStatus;
m_strFileContents = _T("");
CString strOutputFile = m_strFileName;
if (0 == strOutputFile.Right(4).CompareNoCase(_T(".enc")))
{
strOutputFile =
strOutputFile.Left(strOutputFile.GetLength() - 4)
+ _T(".dec");
}
else
{
strOutputFile += _T(".dec");
}
strStatus.Format(_T("Decrypting file %s"), m_strFileName);
DecryptFile(m_strFileName, strOutputFile);
strStatus.Format(_T("%s successfully decrypted to file %s"),
m_strFileName,
strOutputFile);
m_lbxStatus.InsertString(0, strStatus);
m_lbxStatus.InsertString(0, _T("Attempting to read "
"newly decrypted file"));
pSR = new StreamReader(strOutputFile);
CString strCurrLine;
while (0 < pSR->Peek())
{
strCurrLine = pSR->ReadLine();
m_strFileContents += strCurrLine;
m_strFileContents += _T("\r\n");
}
m_lbxStatus.InsertString(0, _T("Data read back and displayed"));
m_strFileName = strOutputFile;
}
catch(Exception* e)
{
m_lbxStatus.InsertString(0, (CString)e->Message);
若图片无法显示请联系QQ752018766,密码体制MFC 第3页系统免费,转发请注明源于www.751com.cn
}
file name.");
}
#pragma pop_macro("new")
}
// HashCode.cpp : 实现文件
//
#include "stdafx.h"
#include "0408008404.h"
#include "HashCode.h"
#include ".\hashcode.h"
// CHashCode 对话框
IMPLEMENT_DYNAMIC(CHashCode, CDialog)
CHashCode::CHashCode(CWnd* pParent /*=NULL*/)
: CDialog(CHashCode::IDD, pParent)
, m_strInputData(_T(""))
, m_iStart(0)
, m_iLength(0)
, m_strSHA1Hash(_T(""))
, m_strMD5Hash(_T(""))
, m_strMD5HashSubstring(_T(""))
{
}
CHashCode::~CHashCode()
{
}
void CHashCode::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strInputData);
DDX_Text(pDX, IDC_EDIT2, m_iStart);
DDX_Text(pDX, IDC_EDIT3, m_iLength);
DDX_Text(pDX, IDC_EDIT4, m_strSHA1Hash);
DDX_Text(pDX, IDC_EDIT5, m_strMD5Hash);
DDX_Text(pDX, IDC_EDIT6, m_strMD5HashSubstring);
}
BEGIN_MESSAGE_MAP(CHashCode, CDialog)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// CHashCode 消息处理程序
void CHashCode::OnBnClickedCancel()
{
DestroyWindow();
delete this;
}
void CHashCode::OnBnClickedOk()
{
#pragma push_macro("new")
#undef new
try
{
if(UpdateData())
{
try
{
m_strSHA1Hash=_T("");
m_strMD5Hash=_T("");
m_strMD5HashSubstring=_T("");
UpdateData(FALSE);
Byte barr[]=new Byte[m_strInputData.GetLength()];
for(int i=0; i<m_strInputData.GetLength(); i++)
barr[i]=static_cast<Byte>(m_strInputData[i]);
SHA1CryptoServiceProvider* csp1=new SHA1CryptoServiceProvider();
MD5CryptoServiceProvider* csp2=new MD5CryptoServiceProvider();
Byte hash[];
hash=csp1->ComputeHash(barr);
m_strSHA1Hash=(CString)BitConverter::ToString(hash);
hash=csp2->ComputeHash(barr);
m_strMD5Hash=(CString)BitConverter::ToString(hash);
hash=csp2->ComputeHash(barr,m_iStart,m_iLength);
m_strMD5HashSubstring=(CString)BitConverter::ToString(hash);
}
MessageBox::Show(e->Message);
}
#pragma pop_macro("new")
}