Click here to Skip to main content
15,921,028 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: function pointers Pin
CPallini15-May-07 1:37
mveCPallini15-May-07 1:37 
QuestionException code: c0000090 (FLT_INVALID_OPERATION) - I am in despair! Pin
Best Kiluyar15-May-07 0:25
Best Kiluyar15-May-07 0:25 
AnswerRe: Exception code: c0000090 (FLT_INVALID_OPERATION) - I am in despair! Pin
Arman S.15-May-07 2:09
Arman S.15-May-07 2:09 
QuestionAny File Encryption Pin
Genius Am Not15-May-07 0:21
Genius Am Not15-May-07 0:21 
AnswerRe: Any File Encryption Pin
Arman S.15-May-07 0:25
Arman S.15-May-07 0:25 
GeneralRe: Any File Encryption Pin
Genius Am Not15-May-07 0:35
Genius Am Not15-May-07 0:35 
GeneralRe: Any File Encryption Pin
Arman S.15-May-07 1:09
Arman S.15-May-07 1:09 
GeneralRe: Any File Encryption Pin
Genius Am Not15-May-07 1:17
Genius Am Not15-May-07 1:17 
This is the code snippet for the original binary file encryption program.
I just need to convert this so that it can accept any file. Im using the RIjnael class for the encryption and decryption.
The purpose is to encrypt and decrypt any file using the same key.

<br />
void CFileEnDecryptionDlg::Char2Hex(const unsigned char ch, char *szHex)<br />
{<br />
	unsigned char byte[2];<br />
	byte[0] = ch/16;<br />
	byte[1] = ch%16;<br />
	for(int i=0; i<2; i++)<br />
	{<br />
		if(byte[i] >= 0 && byte[i] <= 9)<br />
			szHex[i] = '0' + byte[i];<br />
		else<br />
			szHex[i] = 'A' + byte[i] - 10;<br />
	}<br />
	szHex[2] = 0;<br />
}<br />
<br />
void CFileEnDecryptionDlg::Hex2Char(const char *szHex, unsigned char &rch)<br />
{<br />
	rch = 0;<br />
	for(int i=0; i<2; i++)<br />
	{<br />
		if(*(szHex + i) >='0' && *(szHex + i) <= '9')<br />
			rch = (rch << 4) + (*(szHex + i) - '0');<br />
		else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')<br />
			rch = (rch << 4) + (*(szHex + i) - 'A' + 10);<br />
		else<br />
			break;<br />
	}<br />
}<br />
<br />
<br />
<br />
void CFileEnDecryptionDlg::CharStr2HexStr(const char *pucCharStr, char *pszHexStr, int iSize)<br />
{<br />
	int i;<br />
	char szHex[3];<br />
	pszHexStr[0] = 0;<br />
	for(i=0; i<iSize; i++)<br />
	{<br />
		Char2Hex(pucCharStr[i], szHex);<br />
		strcat(pszHexStr, szHex);<br />
	}<br />
}<br />
<br />
void CFileEnDecryptionDlg::HexStr2CharStr(const char *pszHexStr, char *pucCharStr, int iSize)<br />
{<br />
	int i;<br />
	unsigned char ch;<br />
	for(i=0; i<iSize; i++)<br />
	{<br />
		Hex2Char(pszHexStr+2*i, ch);<br />
		pucCharStr[i] = ch;<br />
	}<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnEncryptBtn() <br />
{<br />
	<br />
	h = 0;<br />
<br />
tag:<br />
<br />
	CWnd *input, *output, *result; <br />
	CWnd *msgbox;<br />
	CString  key,text, message;<br />
	char buf[300],hex[600]; <br />
		<br />
	memset(buf,0,300);<br />
	memset(hex,0,600);<br />
<br />
<br />
	// Get the pointers<br />
	input = GetDlgItem(IDC_SOURCE_FILE_CONTENTS);<br />
	output = GetDlgItem(IDC_KEY);<br />
	msgbox = GetDlgItem(IDC_MSGBOX);<br />
	result = GetDlgItem(IDC_RESULT);<br />
	<br />
	result->SetWindowText("");<br />
	<br />
	// Get the window Text<br />
	output->GetWindowText(key);<br />
	input->GetWindowText(text);<br />
<br />
	int flag = 0;<br />
	if(key.GetLength()==0)<br />
	{<br />
		output->SetWindowText("1234567890123456");<br />
		msgbox->SetWindowText("Default Value of key used");<br />
		<br />
	}<br />
<br />
	if(text.GetLength() == 0){<br />
		input->SetWindowText("Scolis Technologies Pvt. Ltd.");<br />
	<br />
		output->GetWindowText(key);<br />
      	input->GetWindowText(text);<br />
		flag = 1;<br />
	}<br />
<br />
	<br />
<br />
	// Initilalize the object<br />
	rijndael.MakeKey ((LPCTSTR)key);<br />
<br />
	int length = text.GetLength();<br />
<br />
	<br />
	if((text.GetLength() % 16) != 0)<br />
	{<br />
		<br />
		// pad up the text<br />
		while((text.GetLength() % 16) != 0)<br />
		{<br />
			text.Insert(length,'0');<br />
			length++;<br />
		}<br />
		if (flag == 0)<br />
			msgbox->SetWindowText("Zeroes have been padded to make the length of string divisible by 16 ");<br />
		else<br />
			<br />
			msgbox->SetWindowText("I'm taking default values since you provided none. Note: Zeroes have been padded to make the length of string divisible by 16");<br />
	}	<br />
<br />
	// Encrypt the string<br />
	rijndael.Encrypt((LPCTSTR)text, buf, text.GetLength());<br />
<br />
	// convert to hex and display<br />
	CharStr2HexStr(buf, hex, strlen(buf));<br />
	result->SetWindowText(hex);<br />
<br />
	if (h == 0)<br />
	{ <br />
		h=1; <br />
		goto tag;<br />
	}<br />
<br />
	<br />
	CFile OFile;<br />
	OFile.Open(m_DestinationFileName, CFile::modeWrite | CFile :: modeCreate);<br />
<br />
	CArchive ar(&OFile, CArchive::store);<br />
	ar << (CString)hex;<br />
	ar.Close();<br />
<br />
	memset(buf,0,300);<br />
	memset(hex,0,600);<br />
<br />
<br />
	OFile.Close();<br />
	msgbox -> SetWindowText("Encrypted Content has been Written to file!");<br />
	<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnDecryptBtn() <br />
{<br />
	CWnd *key,*cipher,*result, *msgbox;<br />
	CString key_text, cipher_text;<br />
	char buf[300],buf1[300];<br />
	<br />
	key_text = '\0';<br />
	cipher_text = '\0';<br />
	key = NULL;<br />
<br />
	memset(buf,0,300);<br />
	memset(buf1,0,300);<br />
<br />
<br />
<br />
	// Get the pointers<br />
	key = GetDlgItem(IDC_KEY);<br />
	cipher = GetDlgItem(IDC_SOURCE_FILE_CONTENTS);<br />
	result= GetDlgItem(IDC_RESULT);<br />
	msgbox = GetDlgItem(IDC_MSGBOX);<br />
	<br />
	//plain->SetWindowText("");<br />
<br />
	// read the key and cipher text<br />
	key->GetWindowText(key_text);<br />
	cipher->GetWindowText(cipher_text);<br />
	<br />
	if( cipher_text.GetLength()==0){<br />
		cipher->SetWindowText("C9377FD2C4D57AA3BD84B247EF0995264CB4378A4E0EA21DA98B7016F64B121C");<br />
		cipher->GetWindowText(cipher_text);<br />
	}<br />
<br />
<br />
	if(key_text.GetLength() == 0 ){<br />
		msgbox->SetWindowText("Ciphertext or Key, decrypting using default values");<br />
		key->SetWindowText("1234567890123456");<br />
		key->GetWindowText(key_text);<br />
		<br />
	}<br />
<br />
    <br />
	<br />
	rijndael.MakeKey ((LPCTSTR)key_text);<br />
<br />
	// decrypt them<br />
	HexStr2CharStr((LPCTSTR)cipher_text, buf1, cipher_text.GetLength ());<br />
<br />
	rijndael.Decrypt((LPCTSTR)buf1, buf, strlen(buf1));<br />
<br />
	//convert and display<br />
	result->SetWindowText(buf);<br />
		<br />
	CFile OFile;<br />
	OFile.Open(m_DestinationFileName, CFile::modeWrite | CFile :: modeCreate);<br />
<br />
	CArchive ar(&OFile, CArchive::store);<br />
	ar << (CString)buf;<br />
	ar.Close();<br />
<br />
	memset(buf,0,300);<br />
	memset(buf1,0,300);<br />
<br />
	OFile.Close();<br />
	msgbox -> SetWindowText("Decrypted Content has been Written to file!");<br />
<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnDefaultBtn() <br />
{<br />
	m_SourceFileContents="Scolis Technologies Pvt. Ltd.";<br />
<br />
	m_Key= "1234567890123456";<br />
	CWnd *result, *msgbox;<br />
<br />
	result= GetDlgItem(IDC_RESULT);<br />
	msgbox = GetDlgItem(IDC_MSGBOX);<br />
<br />
	result->SetWindowText("");<br />
	<br />
	UpdateData(FALSE);<br />
	msgbox->SetWindowText("Default Contents Entered");<br />
<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnInBrowse() <br />
{<br />
	CFile TextFile;<br />
<br />
	char strFilter[]={ "Binary Files (*.bin)|*.bin||" };<br />
<br />
	CFileDialog FileDlg( TRUE, "*.bin", NULL, 0, strFilter);// Opening browse dialog box<br />
<br />
	if( FileDlg.DoModal() == IDOK)<br />
	{<br />
		if( TextFile.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )<br />
		{<br />
			return;<br />
		}<br />
<br />
		CArchive ar( &TextFile, CArchive::load); // Loads the contents of the file<br />
		ar >> m_SourceFileContents; // Diplays the contents of the file<br />
		ar.Close();<br />
	}<br />
	else<br />
	{<br />
		return;<br />
	}<br />
<br />
	TextFile.Close();<br />
<br />
	this -> UpdateData(FALSE);<br />
	<br />
		<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnOutBrowse() <br />
{<br />
	CWnd *name;<br />
<br />
	name = GetDlgItem(IDC_DESTINATION_FILE_NAME);<br />
<br />
	CFile TextFile;<br />
<br />
	char strFilter[]={ "Binary Files (*.bin)|*.bin||" };<br />
<br />
	CFileDialog FileDlg( TRUE, "*.bin", NULL, 0, strFilter);// Opening browse dialog box<br />
<br />
	if( FileDlg.DoModal() == IDOK)<br />
	{<br />
		if( TextFile.Open(FileDlg.GetFileName(), CFile::modeWrite | CFile::modeCreate) == FALSE )<br />
		{<br />
			return;<br />
		}<br />
		m_DestinationFileName = FileDlg.GetPathName();<br />
				<br />
	}<br />
	else<br />
	{<br />
		return;<br />
	}<br />
<br />
	TextFile.Close();<br />
<br />
	this -> UpdateData(FALSE);<br />
	<br />
}<br />
<br />
void CFileEnDecryptionDlg::OnCancel() <br />
{<br />
	exit(0);<br />
	<br />
	CDialog::OnCancel();<br />
}


Jesus Loves Us Just the way we are! Invite Him into your life today!

GeneralRe: Any File Encryption Pin
Arman S.15-May-07 2:20
Arman S.15-May-07 2:20 
GeneralRe: Any File Encryption Pin
Genius Am Not20-May-07 22:52
Genius Am Not20-May-07 22:52 
QuestionTrack other processes in the computer Pin
kamalesh8214-May-07 23:58
kamalesh8214-May-07 23:58 
AnswerRe: Track other processes in the computer [modified] Pin
Arman S.15-May-07 0:10
Arman S.15-May-07 0:10 
AnswerRe: Track other processes in the computer Pin
AkiraOne15-May-07 0:15
AkiraOne15-May-07 0:15 
AnswerRe: Track other processes in the computer Pin
Hamid_RT15-May-07 0:58
Hamid_RT15-May-07 0:58 
AnswerRe: Track other processes in the computer Pin
kasturi_haribabu15-May-07 1:08
kasturi_haribabu15-May-07 1:08 
GeneralRe: Track other processes in the computer Pin
Hamid_RT15-May-07 1:18
Hamid_RT15-May-07 1:18 
GeneralRe: Track other processes in the computer Pin
kamalesh8215-May-07 1:20
kamalesh8215-May-07 1:20 
Questionchanging row colour Pin
neha.agarwal2714-May-07 23:53
neha.agarwal2714-May-07 23:53 
AnswerRe: changing row colour Pin
Hamid_RT15-May-07 0:45
Hamid_RT15-May-07 0:45 
AnswerRe: changing row colour Pin
Nelek15-May-07 0:48
protectorNelek15-May-07 0:48 
QuestionProblem with constructor overloading... Pin
sandeepkavade14-May-07 23:46
sandeepkavade14-May-07 23:46 
AnswerRe: Problem with constructor overloading... Pin
Cedric Moonen14-May-07 23:54
Cedric Moonen14-May-07 23:54 
GeneralRe: Problem with constructor overloading... Pin
sandeepkavade14-May-07 23:57
sandeepkavade14-May-07 23:57 
AnswerRe: Problem with constructor overloading... Pin
Arman S.15-May-07 0:00
Arman S.15-May-07 0:00 
QuestionRe: Problem with constructor overloading... Pin
David Crow15-May-07 3:25
David Crow15-May-07 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.