sc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "./crypt.h"
  4. void die() {
  5. printf("\
  6. usage: sc (-e/-d) -k <key> -m (ascii | a | letter | l)\n\
  7. - '-e' to encrypt\n\
  8. - '-d' to decrypt\n\
  9. - if '-e' or '-d' are omitted mode defaults to '-e'\n\
  10. - '-k' to specify key\n\
  11. - default key is 3.1415 (or 'letsalllovelain' for letter mode)\n\
  12. - '-m' to specify method\n\
  13. - option 'ascii' (shorthand 'a') will perform full ascii shift\n\
  14. (\\x20 <SPACE> up to \\x7f '~')\n\
  15. - option 'letter' will perform letter shift\n\
  16. (\\x41 'A' to \\x5a 'Z' and \\x61 'a' to \\x7a 'z')\n\
  17. sc stands for shiftcipher\n");
  18. exit(1);
  19. }
  20. #define NONE 0
  21. #define KEY 1
  22. #define METHOD 2
  23. #define ASCII 1
  24. #define LETTER -1
  25. int isarg(char *arg, char *flag)
  26. {
  27. if (strcmp(arg, flag)) return 0;
  28. return 1;
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. if (argc < 2) die();
  33. /* scrapped, not portable */
  34. /* strcmp returns 0 on string equality, so in case of a correct flag */
  35. /* code evaluates to (argc >= 2 && !(!0 || !1)) */
  36. //if (argc >= 2 && !(!strcmp("-e", argv[1]) || !strcmp("-d", argv[1]))) die();
  37. //if (argc >= 3 && !(!!strcmp("-k", argv[2])) || !strcmp("-m", argv[2])) die();
  38. /* index, character */
  39. int i;
  40. char c;
  41. /* encrypt or decrypt */
  42. int e = 1;
  43. /* key */
  44. char *k = NULL;
  45. /* method */
  46. int m = ASCII;
  47. /* parse args (arg parsing is cancer) */
  48. int flag = NONE;
  49. for (int i = 1; i < argc; i++) {
  50. /* parse flags */
  51. if (isarg(argv[i], "-d")) { e = 0; continue; };
  52. if (isarg(argv[i], "-k")) { flag = KEY; continue; };
  53. if (isarg(argv[i], "-m")) { flag = METHOD; continue; };
  54. /* parse flag */
  55. if (flag == KEY || flag == METHOD) {
  56. if (flag == KEY) {
  57. k = argv[i];
  58. flag = NONE;
  59. printf("using custom key '%s'\n", k);
  60. }
  61. if (flag == METHOD) {
  62. if (isarg(argv[i], "letter") || isarg(argv[i], "l")) {
  63. m = LETTER;
  64. }
  65. flag = NONE;
  66. }
  67. }
  68. }
  69. /* key (SUPER SECURE default xd) */
  70. if (k == NULL && m == LETTER) k = "letsalllovelain";
  71. if (k == NULL) k = "3.1415";
  72. //printf("e %i k %s m %i\n", e, k, m);
  73. /* set correct cryptmethod */
  74. char (*cryptmethod)(char,char,int);
  75. cryptmethod = &cryptchar;
  76. if (m == LETTER) {
  77. /* check if key contains only ASCII letters */
  78. char *kp = k;
  79. while (*kp != '\0') {
  80. if (!isasciiletter(*kp)) {
  81. fprintf(stderr, "ERR: when using LETTER mode key must only contain ascii letters\n");
  82. return 1;
  83. }
  84. kp++;
  85. }
  86. cryptmethod = &cryptletter;
  87. }
  88. i = 0;
  89. /* iterate stdin */
  90. while ((c = getchar()) != -1) { //&& !feof(stdin)) {
  91. /* crypt */
  92. c = cryptmethod(c, k[i], e);
  93. printf("%c", c);
  94. /* reset i if cipher was fully iterated */
  95. if (k[++i] == 0) i = 0;
  96. }
  97. return 0;
  98. }