Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I have got this encryption decryption program ,
the following program compiles and works alright .
However I cannot figure out what to write for the decryption section.

Can anybody please tell me the code for writing the corresponding decrypt_chars() routine?

Thank you


---------------------------------------


// The encryption program in C++ and ASM with a very simple encryption method - it simply adds 1 to the character.

#include <conio.h> // for kbhit
#include <iostream> // for cin >> and cout <<
#include <iomanip> // for fancy output
using namespace std;

#define MAXCHARS 6 // feel free to alter this, but 6 is the minimum
#define dollarchar '$' // string terminator

char OChars[MAXCHARS], EChars[MAXCHARS], DChars[MAXCHARS] = "Soon!"; // Global Original, Encrypted, Decrypted character strings

//----------------------------- C++ Functions ----------------------------------------------------------


void get_char(char& a_character)
{
cin >> a_character;
while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
{
cout << "Alphanumeric characters only, please try again > ";
cin >> a_character;
}
}
//-------------------------------------------------------------------------------------------------------------

void get_original_chars(int& length)
{
char next_char;
length = 0;
get_char(next_char);

while ((length < MAXCHARS) && (next_char != dollarchar))
{
OChars[length++] = next_char;
get_char(next_char);
}
}

//---------------------------------------------------------------------------------------------------------------
//----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------

void encrypt_chars(int length, char EKey)
{
char temp_char; // char temporary store

for (int i = 0; i < length; i++) // encrypt characters one at a time
{
temp_char = OChars[i]; //
__asm { //
push eax // save register values on stack to be safe
push ecx //
movzx ecx, temp_char //
lea eax, EKey //
call encrypt // encrypt the character
mov temp_char, al //
pop ecx // restore original register values from stack
pop eax //
}
EChars[i] = temp_char; // Store encrypted char in the encrypted chars array
}
return;

// --- Start of Assembly code
__asm {


// Inputs: register EAX = 32-bit address of Ekey,
// ECX = the character to be encrypted (in the low 8-bit field, CL).
// Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).


encrypt5: push eax
mov al, byte ptr[eax]
push ecx
and eax, 0x7C
ror eax, 1
ror eax, 1
inc eax
mov edx, eax
pop ecx
pop eax
mov byte ptr[eax], dl
xor edx, ecx
mov eax, edx
rol al, 1
ret



encrypt :
mov eax, ecx // get character
inc eax // simply add 1 to character! EKey value not used in this simple version.
ret
}

//--- End of Assembly code
}
// end of encrypt_chars function
//---------------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length, char EKey)
{ /*** to be written ***/


return;

}


// end of decrypt_chars function
//---------------------------------------------------------------------------------------------------------------


int main(void)
{
int char_count; // The number of actual characters entered (upto MAXCHARS limit).
char EKey; // Encryption key.

cout << "\nPlease enter your Encryption Key (EKey) letter: "; get_char(EKey);

cout << "\nNow enter upto " << MAXCHARS << " alphanumeric characters:\n";
get_original_chars(char_count);
cout << "\n\nOriginal source string = " << OChars << "\tHex = ";
for (int i = 0; i<char_count;>
encrypt_chars(char_count, EKey);
cout << "\n\nEncrypted string = " << EChars << "\tHex = ";
for (int i = 0; i<char_count;>
decrypt_chars(char_count, EKey);
cout << "\n\nDecrypted string = " << DChars << "\tHex = ";
for (int i = 0; i<char_count;>
cout << "\n\nPress a key to end...";
while (!_kbhit()); //hold the screen until a key is pressed
return (0);


} // end of whole encryption/decryption program --------------------------------------------------------------------
Posted

1 solution

I answered this in your repost of this question.
 
Share this answer
 
Comments
ekcs 6-Apr-14 7:40am    
I am sorry I cannot find you answer , ( if it is possible can you send me the answer in my email at ssl-27@hotmail.com ) . Thank you .
ekcs 6-Apr-14 8:17am    
But I thought there was a solution for this encryption / decryption program but I just could not figure out , I have to write the decryption routine .
Can you please have another look at it and help me out on this one ?
I would very much appreciate this .

Thank you
Richard MacCutchan 6-Apr-14 8:56am    
You cannot decrypt the data because your encryption algorithm destroys it. The first instruction in the encryptor masks off the bottom two bits, so the characters A, B and C would all end up with the same encrypted value. I would strongly suggest you forget this and use a proper supported encryption routine as provided in the Microsoft libraries.
ekcs 6-Apr-14 9:28am    
Right , thank you for your comment .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900