📄 filecryptdlg.cpp
字号:
BlowFishParams(oStrKeyData, bHex, iMode, iPadding);
if(NULL == m_apBlowFish.get())
//Create First Time
m_apBlowFish = auto_ptr<CBlowFish>(new CBlowFish());
char acKey[56];
iLength = oStrKeyData.GetLength();
if(TRUE == bHex)
{
iLength /= 2;
if(iLength > 56)
iLength = 56;
Hex2Binary(LPCTSTR(oStrKeyData), reinterpret_cast<unsigned char*>(acKey), iLength);
}
else
strncpy(acKey, LPCTSTR(oStrKeyData), 56);
m_apBlowFish->Initialize(acKey, 56, SBlock(0UL,0UL), iMode, iPadding);
poMethod = m_apBlowFish.get();
break;
}
case TEA:
{
int iMode, iPadding;
TEAParams(oStrKeyData, bHex, iMode, iPadding);
if(NULL == m_apTEA.get())
//Create First Time
m_apTEA = auto_ptr<CTEA>(new CTEA());
char acKey[16];
iLength = oStrKeyData.GetLength();
if(TRUE == bHex)
{
iLength /= 2;
if(iLength > 16)
iLength = 16;
Hex2Binary(LPCTSTR(oStrKeyData), reinterpret_cast<unsigned char*>(acKey), iLength);
}
else
strncpy(acKey, LPCTSTR(oStrKeyData), 16);
m_apTEA->Initialize(acKey, 16, CTEA::sm_chain0, iMode, iPadding);
poMethod = m_apTEA.get();
break;
}
case XOR256_BLOCK:
{
int iMode, iPadding;
int iBlockSize, iIRounds, iORounds;
XOR256BlockParams(oStrKeyData, bHex, iBlockSize, iORounds, iIRounds, iMode, iPadding);
if(NULL == m_apXOR256Block.get())
//Create First Time
m_apXOR256Block = auto_ptr<CXOR256Block>(new CXOR256Block());
char acKey[256];
iLength = oStrKeyData.GetLength();
if(TRUE == bHex)
{
iLength /= 2;
if(iLength > 256)
iLength = 256;
Hex2Binary(LPCTSTR(oStrKeyData), reinterpret_cast<unsigned char*>(acKey), iLength);
}
else
strncpy(acKey, LPCTSTR(oStrKeyData), 256);
m_apXOR256Block->Initialize(acKey, 256, CXOR256Block::sm_chain0, iBlockSize, iORounds, iIRounds, iMode, iPadding);
poMethod = m_apXOR256Block.get();
break;
}
case XOR256_STREAM:
{
int iIRounds;
XOR256StreamParams(oStrKeyData, bHex, iIRounds);
if(NULL == m_apXOR256Stream.get())
//Create First Time
m_apXOR256Stream = auto_ptr<CXOR256Stream>(new CXOR256Stream());
char acKey[256];
iLength = oStrKeyData.GetLength();
if(TRUE == bHex)
{
iLength /= 2;
if(iLength > 256)
iLength = 256;
Hex2Binary(LPCTSTR(oStrKeyData), reinterpret_cast<unsigned char*>(acKey), iLength);
}
else
strncpy(acKey, LPCTSTR(oStrKeyData), 256);
m_apXOR256Stream->Initialize(acKey, 256, iIRounds);
poMethod = m_apXOR256Stream.get();
break;
}
default:
ASSERT(FALSE);
}
return poMethod;
}
void CFileCryptDlg::ActionFile()
{
CString oStr;
CEdit* poEdit1;
CEdit* poEdit2;
//Input File
CString oStrInput;
if(ENCRYPT == m_iAction)
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR1));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
}
else //DECRYPT == m_iAction
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR3));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
}
poEdit1->GetWindowText(oStrInput);
poEdit2->GetWindowText(oStr);
oStrInput.TrimLeft();
oStrInput.TrimRight();
oStr.TrimLeft();
oStr.TrimRight();
oStrInput += _T("\\") + oStr;
//Output File
CString oStrOutput;
if(ENCRYPT == m_iAction)
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR3));
}
else //DECRYPT == m_iAction
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR1));
}
poEdit1->GetWindowText(oStr);
poEdit2->GetWindowText(oStrOutput);
oStr.TrimLeft();
oStr.TrimRight();
oStrOutput.TrimLeft();
oStrOutput.TrimRight();
if(CString(_T("")) == oStr)
{
if(ENCRYPT == m_iAction)
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT3));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
}
else
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT1));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
}
//Generate Output from Input and Extension
TCHAR szBuff[MAX_PATH+1];
_tcscpy(szBuff, LPCTSTR(oStrInput));
::PathStripPath(szBuff);
::PathRemoveExtension(szBuff);
CString oStrExt;
poEdit1->GetWindowText(oStrExt);
if(_T("*.") == oStrExt.Left(2))
{
oStrExt = oStrExt.Mid(1);
}
else if(_T('*') == oStrExt.GetAt(0))
{
MessageBox(_T("FileCrypt ERROR: Illegal File Extension ") + oStrExt + _T("!"), g_oStrError, MB_OK|MB_ICONINFORMATION);
ErrorBeep();
return;
}
else if(_T('.') != oStrExt.GetAt(0))
oStrExt = _T(".") + oStrExt;
::PathAddExtension(szBuff, LPCTSTR(oStrExt));
oStrOutput += _T("\\") + CString(szBuff);
_tcscpy(szBuff, LPCTSTR(oStrOutput));
::PathStripPath(szBuff);
poEdit2->SetWindowText(CString(szBuff));
}
else
oStrOutput += _T("\\") + oStr;
try
{
//Start the Hourglass cursor
CWaitCursor oWaitCursor;
//Find out the method
CComboBox* poComboBox;
poComboBox = static_cast<CComboBox*>(GetDlgItem(IDC_COMBOMETHODS));
int iMethod = poComboBox->GetCurSel();
IMethod* poMethod = GetMethod(iMethod);
if(ENCRYPT == m_iAction)
poMethod->EncryptFile(LPCTSTR(oStrInput), LPCTSTR(oStrOutput));
else //DECRYPT == m_iAction
poMethod->DecryptFile(LPCTSTR(oStrInput), LPCTSTR(oStrOutput));
}
catch(exception const& roException)
{
MessageBox(CString(roException.what()), g_oStrError, MB_OK|MB_ICONINFORMATION);
ErrorBeep();
return;
}
if(ENCRYPT == m_iAction)
MessageBox(_T("FileCrypt: File ") + oStrInput + _T(" Successfully Encrypted!"), g_oStrSuccess, MB_OK|MB_ICONINFORMATION);
else //DECRYPT == m_iAction
MessageBox(_T("FileCrypt: File ") + oStrInput + _T(" Successfully Decrypted!"), g_oStrSuccess, MB_OK|MB_ICONINFORMATION);
}
void CFileCryptDlg::ActionString()
{
BOOL bHex = FALSE;
CButton* poButton = static_cast<CButton*>(GetDlgItem(IDC_RADPLAINHEX));
if(1 == poButton->GetCheck())
bHex = TRUE;
CEdit* poEdit1;
CEdit* poEdit2;
//Input String
CString oStrInput;
if(ENCRYPT == m_iAction)
{
if(TRUE == bHex)
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTRHEX));
else
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTR));
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTENCHEX));
}
else //DECRYPT == m_iAction
{
poEdit1 = static_cast<CEdit*>(GetDlgItem(IDC_EDTENCHEX));
if(TRUE == bHex)
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTRHEX));
else
poEdit2 = static_cast<CEdit*>(GetDlgItem(IDC_EDTSTR));
}
poEdit1->GetWindowText(oStrInput);
try
{
//Start the Hourglass cursor
CWaitCursor oWaitCursor;
//Find out the method
CComboBox* poComboBox;
poComboBox = static_cast<CComboBox*>(GetDlgItem(IDC_COMBOMETHODS));
int iMethod = poComboBox->GetCurSel();
IMethod* poMethod = GetMethod(iMethod);
char* pcIn;
char* pcOut;
char* pcHex;
int iLen, iLen1;
if(ENCRYPT == m_iAction)
{
iLen = oStrInput.GetLength();
if(TRUE == bHex)
iLen /= 2;
//Estimate Padding
int iBlockSize = poMethod->GetBlockSize();
if(iBlockSize > -1)
{
if(iLen%iBlockSize != 0)
iLen1 = (iLen/iBlockSize+1)*iBlockSize;
else
iLen1 = iLen;
}
else
iLen1 = iLen;
pcIn = static_cast<char*>(_alloca(iLen1));
pcOut = static_cast<char*>(_alloca(iLen1));
if(TRUE == bHex)
Hex2Binary(LPCTSTR(oStrInput), reinterpret_cast<unsigned char*>(pcIn), iLen);
else
strcpy(pcIn, LPCTSTR(oStrInput));
poMethod->ResetChain();
if(iBlockSize > -1)
poMethod->Pad(pcIn, iLen);
poMethod->Encrypt(pcIn, pcOut, iLen1);
iLen = (iLen1<<1)+1;
pcHex = static_cast<char*>(_alloca(iLen));
Binary2Hex(reinterpret_cast<unsigned char*>(pcOut), iLen1, pcHex);
pcHex[iLen] = 0;
poEdit2->SetWindowText(pcHex);
}
else //DECRYPT == m_iAction
{
int iLen = oStrInput.GetLength()/2;
pcIn = static_cast<char*>(_alloca(iLen));
Hex2Binary(LPCTSTR(oStrInput), reinterpret_cast<unsigned char*>(pcIn), iLen);
pcOut = static_cast<char*>(_alloca(iLen+1));
poMethod->ResetChain();
poMethod->Decrypt(pcIn, pcOut, iLen);
pcOut[iLen] = 0;
if(TRUE == bHex)
iLen1 = (iLen<<1) + 1;
else
iLen1 = iLen + 1;
pcHex = static_cast<char*>(_alloca(iLen1));
if(TRUE == bHex)
{
Binary2Hex(reinterpret_cast<unsigned char*>(pcOut), iLen, pcHex);
pcHex[iLen1] = 0;
}
else
strcpy(pcHex, pcOut);
poEdit2->SetWindowText(CString(pcHex));
}
}
catch(exception const& roException)
{
MessageBox(CString(roException.what()), g_oStrError, MB_OK|MB_ICONINFORMATION);
ErrorBeep();
return;
}
}
void CFileCryptDlg::OnBtndir1()
{
//Get the Directory in the static member
CEdit* poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR1));
poEdit->GetWindowText(sm_oStrDir);
CString oStrPath;
if(TRUE == BrowseDialog(oStrPath))
poEdit->SetWindowText(oStrPath);
}
void CFileCryptDlg::OnBtndir3()
{
//Get the Directory in the static member
CEdit* poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR3));
poEdit->GetWindowText(sm_oStrDir);
CString oStrPath;
if(TRUE == BrowseDialog(oStrPath))
poEdit->SetWindowText(oStrPath);
}
void CFileCryptDlg::OnBtnfile1()
{
//Get the extension
CEdit* poEdit;
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT1));
CString oStrExt;
poEdit->GetWindowText(oStrExt);
GetFirstString(oStrExt);
if(oStrExt.Left(2) != CString(_T("*.")))
{
//Check first character
if(_T('.') == oStrExt.GetAt(0))
oStrExt = _T("*") + oStrExt;
else
oStrExt = _T("*.") + oStrExt;
}
TCHAR szBuff[100];
_stprintf(szBuff, _T("File (%s)_%s__"), LPCTSTR(oStrExt), LPCTSTR(oStrExt));
//Replace 3 times _ with \0
int iCount = 0;
for(int i=0; szBuff[i]!=0; i++)
if(_T('_') == szBuff[i])
szBuff[i] = _T('\0');
//TRUE - File Open
CFileDialog oFileOpen(TRUE);
oFileOpen.m_ofn.lpstrTitle = _T("Open File");
oFileOpen.m_ofn.lpstrFilter = szBuff;
CString oStrDir;
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR1));
poEdit->GetWindowText(oStrDir);
oFileOpen.m_ofn.lpstrInitialDir = LPCTSTR(oStrDir);
if(IDOK == oFileOpen.DoModal())
{
CString oStrFileName = oFileOpen.GetFileName();
CString oStrFilePath = oFileOpen.GetPathName();
TCHAR szDir[MAX_PATH+1];
_tcscpy(szDir, LPCTSTR(oStrFilePath));
::PathRemoveFileSpec(szDir);
oStrFilePath = CString(szDir);
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE1));
poEdit->SetWindowText(oStrFileName);
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR1));
poEdit->SetWindowText(oStrFilePath);
}
}
void CFileCryptDlg::OnBtnfile3()
{
//Get the extension
CEdit* poEdit;
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTEXT3));
CString oStrExt;
poEdit->GetWindowText(oStrExt);
GetFirstString(oStrExt);
if(oStrExt.Left(2) != CString(_T("*.")))
{
//Check first character
if(_T('.') == oStrExt.GetAt(0))
oStrExt = _T("*") + oStrExt;
else
oStrExt = _T("*.") + oStrExt;
}
TCHAR szBuff[100];
_stprintf(szBuff, _T("File (%s)_%s__"), LPCTSTR(oStrExt), LPCTSTR(oStrExt));
//Replace 3 times _ with \0
int iCount = 0;
for(int i=0; szBuff[i]!=0; i++)
if(_T('_') == szBuff[i])
szBuff[i] = _T('\0');
//TRUE - File Open
CFileDialog oFileOpen(TRUE);
oFileOpen.m_ofn.lpstrTitle = _T("Open File");
oFileOpen.m_ofn.lpstrFilter = szBuff;
CString oStrDir;
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR3));
poEdit->GetWindowText(oStrDir);
oFileOpen.m_ofn.lpstrInitialDir = LPCTSTR();
if(IDOK == oFileOpen.DoModal())
{
CString oStrFileName = oFileOpen.GetFileName();
CString oStrFilePath = oFileOpen.GetPathName();
TCHAR szDir[MAX_PATH+1];
_tcscpy(szDir, LPCTSTR(oStrFilePath));
::PathRemoveFileSpec(szDir);
oStrFilePath = CString(szDir);
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTFILE3));
poEdit->SetWindowText(oStrFileName);
poEdit = static_cast<CEdit*>(GetDlgItem(IDC_EDTDIR3));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -