2

I've written a program to encrypt a given message by XOR. It works, but It doesn't end. Here is the code.(I have created 3 files):

encrypt.h :

void encrypt(char *message);

message_hider.c:

#include <stdio.h>
#include "encrypt.h"

int main() {
  char msg[80];

  while (fgets(msg, 80, stdin)){
    encrypt(msg);
    printf("%s", msg);
  }

  return 0;
}

encrypt.c :

#include "encrypt.h"

void encrypt(char *message) {
  while (*message) {
    *message++ ^= 0x1f;
  }
}

As I mentioned above, It works. but I can't stop it. When I pressed Ctrl+D to stop it (in cmd) It encrypts it also.(I need this code stop after it encrypt a message). Please explain me about this case.

0xEDD1E
  • 1,172
  • 1
  • 13
  • 28
  • As you wrote this. Can you explain under what situation you expected this code to stop? – BugFinder Aug 17 '12 at 07:55
  • cmd does not understand Ctrl+D, use end-of-file condition (i.e. write a file and give it to your program on stdin: `prog < file`). And you don't encrypt, you obfuscate (a little). BTW, don't expect printf be able to actually display the obfuscated text. – fork0 Aug 17 '12 at 07:57
  • You use a worse subject, please modify it to meaningful. – Daniel YC Lin Aug 17 '12 at 08:02
  • @fork0 (1) Windows `cmd` recognizes Ctrl+Z as EOF; (2) this is indeed an encryption using a simple substitution cipher, just like how [ROT13 is a substitution cipher](http://en.wikipedia.org/wiki/Rot13). – obataku Aug 17 '12 at 08:05
  • the line edited by Brad is Ok, but it encrypt the return character also. – 0xEDD1E Aug 17 '12 at 08:11
  • please also tag your question with information about the platform that you are using – Jens Gustedt Aug 17 '12 at 08:38
  • @0xEDD1E what line did Brad edit? – obataku Aug 17 '12 at 09:30

5 Answers5

5

When I pressed Ctrl+D to stop it (in cmd)

If that's the cmd from Windows you probably want Ctrl+Z.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
5

Ctrl-D is used for the console EOF on Unix systems.

Ctrl-Z is used for the console EOF on Windows systems.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
2

isprint() can help:

#include <stdio.h>
#include <ctype.h>

void encrypt(char *message)
{
    while (*message) {
        *message = *message ^ 31;
        message++;
    }
}

int main(void)
{
    char msg[80];

    while (fgets(msg, 80, stdin) != NULL) {
        if (!isprint((unsigned char)*msg)) break;
        encrypt(msg);
        printf("%s", msg);
    }
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

Add an exit condition:

if( c < 0x20 ) break;

You may need to add other checks also to support backspace without encoding it...

http://www.asciitable.com/

Nicholas Albion
  • 3,096
  • 7
  • 33
  • 56
0

Just run

  $> kill -l

To see the list of signals in Linux. You will not find SIGKILL (Ctrl + D) signal there :(

Ctrl + D is SIGKILL (0) signal in Linux which is not documented anywhere. Ctrl + Z is for Windows which tell EOF and we need to press "Enter" to close.

Viswesn
  • 4,674
  • 2
  • 28
  • 45
  • This is incorrect. Ctrl-D doesn't cause a signal to be sent on unix. SIGKILL is typically signal 9, not 0. The value 0 doesn't have a standard name assigned to it. – Kenster Aug 25 '15 at 12:52