/* Encryptor for fairly simple text encryption
   
   Copyright (C) 2007 James Stanley
   
   No license - Use as you wish */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char chars[81] = {
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
  'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 
  'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
  'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
  'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', 
  '8', '9', '!', '"', '-', '+', ' ', '^', '&', '\'', 
  '(', ')', ';', ':', ',', '.', '<', '>', '?', '_', '\0' };

int main(int argc, char **argv) {
  char *key;
  char *data = malloc(sizeof(char) * 2048);
  int keylen, datalen;
  int keypos;
  int kch, dch, ech;//Key character, Data character, Encrypted character
  int i;
  int warn;//Whether or not to warn about a bad key
  int warnmsg;//Whether or not to warn about a bad message
  char kp, dp;//Key, Data
  printf("James Stanley's Encryptor\n-------------------------");
  //Handle parameters and get a key
  if(argc == 2) {
    key = argv[1];
  } else if((argc == 1)&&(argv[1]=="--help")) {
    printf("James Stanley's Encryptor\n-------------------------\nPass the key as the first parameter, and the message as the second please\n\nThis encryptor is copyright James Stanley 2007.");
  } else if(argc > 2) {
    key = argv[1];
    printf("key = \"%s\";\nPass the key as one parameter, please (enclosing it in single quotes is the usual way)\n\n", key);
  } else {
    printf("Pass the key as the parameter to avoid typing it now!\nkey: ");
    key = malloc(sizeof(char) * 512);
    fgets(key, 512, stdin);
    if(key[strlen(key) - 1] == '\n') key[strlen(key) - 1] = '\0';
  }
  
  keylen = strlen(key);
  
  //Now loop encrypting stuff
  while(1) {
    fgets(data, 2048, stdin);
    if(data[strlen(data) - 1] == '\n') data[strlen(data) - 1] = '\0';
    
    if(strcmp(data, "exit") == 0) return 0;
    
    datalen = strlen(data);
    warn = 0;
    warnmsg = 0;
    
    for(i = 0; i < datalen; i++) {
      
      keypos = i % keylen;
      kp = key[keypos];
      dp = data[i];
      
      if(strchr(chars, kp) && strchr(chars, dp)) {//If the key and data characters are valid
        kch = strchr(chars, kp) - chars;
        dch = strchr(chars, dp) - chars;
        ech = dch + kch;
        while(ech < 0) ech += 80;
        printf("%c", chars[ech % 80]);
      } else {
        if(!strchr(chars, kp)) warn = kp;
        if(!strchr(chars, dp)) warnmsg = dp;
        printf("%c", data[i]);
      }
    }
    printf("\n");
    
    if(warn) printf("WARNING: Your key contained one or more invalid characters, leaving part of the message unencrypted! The last invalid key character was '%c'.\n", warn);
    
    if(warnmsg) printf("WARNING: Your message contained one or more invalid characters, leaving part of the message unencrypted! The last invalid message character was '%c'.\n", warnmsg);
  }
  
  return 0;
}
