Note that there are some explanatory texts on larger screens.

plurals
  1. POWhats the wrong with my C encryption program?
    text
    copied!<p>So I'm trying to making a file encrypting program in C (by the way I'm kind of new to C) so I wrote this simple XOR file encryption code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char fileName1[35] = {'\0'}; //Name of original file char fileName2[35] = {'\0'}; //Name of encrypted file char keyString[20] = {'\0'}; //Key determines result encryption FILE* originalFile; //File to be encrypted FILE* cryptedFile; //The encrypted file int c; //byte read from original file printf("Enter file location followed by name of the file you want to encrypt: "); scanf("%s", fileName1); printf("Enter file location followed by name of the encrypted file: "); scanf("%s", fileName2); printf("Enter your key (Encryption changes based on Key): "); scanf("%s", keyString); originalFile = fopen(fileName1, "rb"); //rb to read file bytes cryptedFile = fopen(fileName2, "wb"); //wb to write bytes to file if(originalFile != NULL &amp;&amp; cryptedFile != NULL){ while( (c = getc(originalFile)) != EOF ){ int x; for(x=0; x&lt;strlen(keyString); x++){ c ^= keyString[x]; putc(c, cryptedFile); } } fclose(originalFile); fclose(cryptedFile); } return 0; } </code></pre> <p>So to test this program I created a file called file1.txt and run the encryption program giving it the second file as file2.txt and key as <em>secret</em>. Then I ran the program again but this time on the encrypted file2.txt and created a file3.txt with the same key <em>secret</em>. Since it was the same key the file3.txt should be the same as file1.txt but file3.txt has random contents in it. So what am I doing wrong?</p>
 

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