home *** CD-ROM | disk | FTP | other *** search
/ PCMania 10 / Pcmania_Ep2_10_CD-01.iso / ARTICULOS / tecnologia / CAESAR2.ZIP / caesar.c next >
Encoding:
C/C++ Source or Header  |  1997-12-21  |  3.3 KB  |  126 lines

  1. /*
  2.  
  3. CaesarCrypt v2.0
  4. Written By: E-HACK (epicurus@wilter.com)
  5.  
  6. A simple example program to perform the "Caesar Cipher", an old, insecure
  7. encryption scheme. Can both encrypt and decrypt files. Not reccomended for
  8. anything other than novelty and/or study. This encryption algorythm is very
  9. easy to break even without a program to use.
  10.  
  11.  
  12. With the Caesar Cipher, each letter is moved over X spaces, much like the
  13. old "decoder rings" that you used to be able to get by sending in box tops
  14. and crap in the 50's. 
  15.  
  16. I've changed this around quite a bit from the original Caesar Cipher algorithm
  17. in that I've made it more practical for computers by letting it de/encrypt all
  18. 256 ascii charicter values.
  19.  
  20. This program is copylefted, so you can do whatever you want with it, so have
  21. some fun, but please, don't use this for anything important, as it's simply
  22. not secure enough.
  23.  
  24. Disclaimer:
  25. THIS PROGRAM IS DISTRIBUTED "AS IS," WITHOUT ANY IMPLIED OR EXPRESS WARRANTY
  26. AS TO ITS PERFORMANCE OR AS TO THE RESULTS THAT MAY BE OBTAINED BY ITS USE.
  27. E-HACK AND WILTERED FIRE ARE NOT LIABLE FOR DIRECT, INDIRECT, OR INCIDENTAL
  28. DAMAGES FROM ANY DEFECT, MALFUNCTION, INABLILITY OR OMISSION IN THE SOFTWARE
  29. OR MANUAL OR OTHER RELATED ITEMS AND PROCESSES, INCLUDING BUT NOT LIMITED TO
  30. SERVICE, LOSS OF BUSINESS, ANTICIPATED PROFIT, OR OTHER CONSEQUENTIAL DAMAGES.
  31.  
  32. To compile on *NIX:
  33. cc -o caesar caesar.c
  34.  
  35. Successfully tested(compiled and run w/out changes) on: 
  36. BSDi 3.0
  37. MS-DOS 6.2
  38. Windows 95
  39.  
  40. */
  41.  
  42.  
  43.  
  44. #include <stdio.h> /* standard I/O functions */
  45.  
  46.  
  47. int main(void)
  48. {
  49. char infile[256], outfile[256], enc[1];
  50. int c, i, enci, key;
  51. FILE *ffp, *sfp;
  52.  
  53. printf("CaesarCrypt v2.0\nWritten By: E-HACK\n\n");
  54. printf("Input File: ");
  55. scanf("%s", infile); /* gets the input file, it'll be opened as ffp */
  56. printf("Output File: ");
  57. scanf("%s", outfile); /* gets the output file, it'll be opened as sfp */
  58. printf("Key: ");
  59. scanf("%d", &key); /* gets the key to use for encryption/decryption */
  60.  
  61. if(key < 0 || key > 255)
  62. {
  63. printf("Key must be an integer between 0 and 255!\n");
  64. return(0);
  65. }
  66.  
  67. /* ask if they need to decrypt or encrypt */
  68. printf("Encrypt or Decrypt(E/D)? ");
  69. scanf("%s", enc);
  70. enci = 0;
  71. if(tolower(enc[0]) == 'd'){
  72. enci = 1;
  73. }
  74.  
  75. /* open input file */
  76. ffp = fopen(infile, "r");
  77.     if(ffp == NULL) {
  78.         /* oops, can't open that one, it's not there */
  79.     printf("Unable to open %s!\n", infile);
  80.     return(0);
  81.     }
  82. /* open the output file */
  83. sfp = fopen(outfile, "w");
  84. /* flush the contents of the output file */
  85. fflush(sfp);
  86.  
  87. /* do this for every byte in the input file */
  88. while(c != EOF)
  89. {
  90. /* get one byte from the input file */
  91. c = getc(ffp);
  92. i = c;
  93. if(enci == 1)
  94. {
  95. i -= key;
  96. }
  97. if(enci != 1)
  98. {
  99. i += key;
  100. }
  101. /* get i between 0 and 255 */
  102. while(i < 0){ i += 256; }
  103. while(i > 255){ i -= 256; }
  104.  
  105. /* print the encrypted charicter to the output file, unless it's the end of file */
  106. if(c != EOF) fprintf(sfp, "%c", i);
  107.  
  108. }
  109. /* close input and output files */
  110. fclose(ffp);
  111. fclose(sfp);
  112.  
  113. /* delete the input file...I decided to comment this out though
  114. unlink(infile); */
  115.  
  116. /* tell the user that we're done encrypting or decrypting the data */
  117. if(enci == 1){
  118. printf("Decrypted %s to %s\n", infile, outfile);
  119. }
  120. else {
  121. printf("Encrypted %s to %s\n", infile, outfile);
  122. }
  123.  
  124. /* done */
  125. return(0);
  126. }