Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

decoding encrypted message 1

Status
Not open for further replies.

joebeach

Programmer
Sep 12, 1999
13
US
can someone tell me how to decode an encrypted message by using exclusive OR and using the orginal encryption key?
 
Kim,<br>
just to decode a message using XOR. I don't mind to learn how to crack a message though.<br>
<br>
Joe
 
Below is some <b>very</b> simple code that shows how to do this. It would be much more secure if more than one byte was used for the &quot;key&quot;, but I'm keeping it simple. It also has a minor bug. Can you spot it? ;)<br>
<br>
<br>
#include &lt;stdio.h&gt;<br>
#include &lt;string.h&gt;<br>
<br>
int main()<br>
{<br>
<br>
int length, x;<br>
char OrgString[] = &quot;Hello there!&quot;;<br>
char EncryptedStr[20];<br>
char DecryptedStr[20];<br>
char key = 0x55; /* The encryption/decryption key */<br>
<br>
printf( &quot;Original string ---&gt;%s\n&quot;, OrgString );<br>
<br>
length = strlen( OrgString );<br>
/* In the loop below we encrypt the original string with the &quot;key&quot;*/<br>
for ( x=0; x&lt;length; x++)<br>
&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; EncryptedStr[x] = OrgString[x] ^ key;<br>
&nbsp;&nbsp;&nbsp;&nbsp; }<br>
EncryptedStr[x] = 0;<br>
printf( &quot;Encrypted string --&gt;%s\n&quot;, EncryptedStr );<br>
<br>
<br>
length = strlen( EncryptedStr );<br>
/* In the loop below we decrypt the encrypted string with the &quot;key&quot;*/<br>
for ( x=0; x&lt;length; x++)<br>
&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp; DecryptedStr[x] = EncryptedStr[x] ^ key;<br>
&nbsp;&nbsp;&nbsp;&nbsp; }<br>
DecryptedStr[x] = 0;<br>
printf( &quot;Decrypted string --&gt;%s\n&quot;, DecryptedStr );<br>
<br>
return 0;<br>
}<br>
<p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
Joebeach,<br>
I got your e-mail. No problem. The bug in the above code is that the message would be trunciated if any character in the &quot;OrgString&quot; matched the &quot;key&quot;. If you put an uppercase U in the OrgString, you'll see what happens. I've had some fun with this (Added way more security), so check out the code below:<br>
<br>
#include &lt;stdio.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;stdlib.h&gt;<br>
<br>
int main()<br>
{<br>
<br>
int length, x;<br>
char OrgString[] = &quot;Hello there, and how are you today?&quot;;<br>
char EncryptedStr[80];<br>
char DecryptedStr[80];<br>
char key = 0x55; /* The encryption/decryption key */<br>
<br>
printf( &quot;Original string ---&gt;%s\n&quot;, OrgString );<br>
<br>
length = strlen( OrgString );<br>
/* In the loop below we encrypt the original string with the &quot;key&quot; seed*/<br>
srand(key);<br>
for ( x=0; x&lt;length; x++)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;EncryptedStr[x] = OrgString[x] ^ rand();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
EncryptedStr[x] = 0;<br>
printf( &quot;Encrypted string --&gt;%s\n&quot;, EncryptedStr );<br>
<br>
/* In the loop below we decrypt the encrypted string with the &quot;key&quot; seed*/<br>
srand(key);<br>
for ( x=0; x&lt;length; x++)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;DecryptedStr[x] = EncryptedStr[x] ^ rand();<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
DecryptedStr[x] = 0;<br>
printf( &quot;Decrypted string --&gt;%s\n&quot;, DecryptedStr );<br>
<br>
return 0;<br>
}<br>
<p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top