Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With XOR, decrypting is exactly the same operation as encrypting. Run the encrypted string through the <code>xor_encrypt</code> method again same key) and you have the plain text back.</p> <h1>warning 1: null characters</h1> <p>One thing to watch out for: if the character in the string matches the corresponding character in the key, your result will be <code>'\0'</code>. This will be interpreted by your current code as the "end of string" and would stop the decryption short. To circumvent this, you want to pass the length of the "actual" string as a parameter to your function. </p> <h1>warning 2: short keys</h1> <p>You also want to make sure you don't run past the end of your key - if the plain text is very long you might have to repeat the key. You can do this with the <code>%</code> operator - just recycle the key from the beginning. </p> <p>Here is a complete example that shows these techniques:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; void xor_encrypt(char *key, char *string, int n) { int i; int keyLength = strlen(key); for( i = 0 ; i &lt; n ; i++ ) { string[i]=string[i]^key[i%keyLength]; } } int main(void) { char plain[] = "This is plain text"; char key[] = "Abcdabcdabciabcdabcd"; int n = strlen(plain); // encrypt: xor_encrypt(key, plain, n); printf("encrypted string: \n"); for(int ii = 0; ii &lt; n; ii++) { if(plain[ii] &gt; 0x32 &amp;&amp; plain[ii] &lt; 0x7F ) printf("%c", plain[ii]); else printf(" 0x%02x ", plain[ii]); } printf("\n"); // **** if you include this next line, things go wrong! n = strlen(plain); xor_encrypt(key, plain, n); printf("after round trip, plain string is '%s'\n", plain); } </code></pre> <p>This (not realizing the problem with kay == string) results in truncated decryption (the <code>i</code> in <code>plain</code> matches the same letter in <code>key</code>):</p> <pre><code>encrypted string: 0x15 0x0a 0x0a 0x17 A 0x0b 0x10 D 0x11 0x0e 0x02 0x00 0x0f B 0x17 0x01 0x19 0x16 after round trip, plain string is 'This is pla' </code></pre> <p>Leaving out the line I marked above (i.e., keeping the value of <code>n</code> as the original length of the string), your result is</p> <pre><code>encrypted string: 0x15 0x0a 0x0a 0x17 A 0x0b 0x10 D 0x11 0x0e 0x02 0x00 0x0f B 0x17 0x01 0x19 0x16 after round trip, plain string is 'This is plain text' </code></pre> <p>exactly as you would expect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload