Category : C Source Code
Archive   : SUPER_C.ZIP
Filename : ENCRPT.C

 
Output of file : ENCRPT.C contained in archive : SUPER_C.ZIP
/* Encrypt

This program encrypts the standard input and writes it to
the standard output. It requires one parameter: the key for
the encryption, which can be up to eight characters long.
Decryption is done by simply running the program again on the
encrypted file with the same key -- i.e., encryption and
decryption are the same operation.
*/

#include
#include
#include

#define BUFSIZE 20480 /* I/O buffer size. */
#define KEYSIZE 9 /* Maximum size of key (plus one for null). */

#define CALIBRATE /* Define for start/end prompts. */

char buffer[BUFSIZE]; /* The I/O buffer. */

#define STDINFILE 0 /* Standard input non-stream file number. */
#define STDOUTFILE 1 /* Standard output file number. */

#define TRUE 1
#define FALSE 0

/* main(argc,argv)

Function: Encrypt the standard input and write it to the
standard output, using the single command line parameter
as the key.

Algorithm: Get the key and rearrange it to improve security,
then encrypt using exclusive-or with the key.
*/

main(argc,argv)

int argc;
char *argv[];

{
char key[KEYSIZE]; /* The key. */
char *cp, *cp2;
char c;

/* Check if we've got the right number of command line
parameters. */
if (argc != 2) {
/* If not, remind him how to use this program. */
fputs("Usage: encrpt key cipherText",stderr);<br /> exit(1);<br /> };<br /> <br /> /* Check if we've got a decent sized key. */<br /> if ((argv[1][0] == 0) || (argv[1][1] == 0)) {<br /> /* If not, tell him he needs a bigger key. */<br /> fputs("Key must be at least 2 characters in size.",stderr);<br /> exit(1);<br /> };<br /> <br /> /* Get the key. */<br /> for (cp = key, cp2 = argv[1];<br /> (cp < &key[KEYSIZE-1]) && (*cp2 != 0); *cp++ = *cp2++);<br /> /* Distribute the last byte amoung the top bits of the earlier<br /> ones, and toggle some of the bits in each key byte. All of<br /> this helps ensure a more secure key. */<br /> for (c = *(--cp), *cp = 0; cp >= key; cp--) {<br /> *cp |= (c & 1) << 7;<br /> *cp ^= 0x55;<br /> c >>= 1;<br /> };<br /> <br /> /* Use binary I/O mode. For two reasons: (1) We want to be<br /> able to encrypt binary files; (2) characters may be<br /> turned into LFs in the encryption process, which would<br /> erroneously be translated into CRLF. */<br /> setmode(STDINFILE,O_BINARY);<br /> setmode(STDOUTFILE,O_BINARY);<br /> <br /> /* Encrypt it. */<br /> #ifdef CALIBRATE<br /> fputs("Starting...",stderr);<br /> #endif CALIBRATE<br /> encrypt(STDINFILE,STDOUTFILE,key);<br /> #ifdef CALIBRATE<br /> fputs("done.\n",stderr);<br /> #endif CALIBRATE<br /> }<br /> <br /> /* encrypt(inFile,outFile,theKey)<br /> <br /> Function: Encrypt the file specified by inFile, using<br /> theKey, and write it to outFile.<br /> <br /> Algorithm: Exclusive-or the bytes with theKey, recycling<br /> theKey as needed.<br /> */<br /> <br /> encrypt(inFile,outFile,theKey)<br /> <br /> int inFile;<br /> int outFile;<br /> char *theKey;<br /> <br /> {<br /> register char *keyPtr;<br /> register char *bufPtr;<br /> int bufCnt;<br /> int sizeRead;<br /> <br /> keyPtr = theKey;<br /> /* Cycle until nothing left to read. */<br /> while (TRUE) {<br /> /* Read in a bufferfull. */<br /> sizeRead = bufCnt = read(inFile,buffer,BUFSIZE);<br /> if (sizeRead <= 0) break;<br /> /* Encrypt it. */<br /> for (bufPtr = buffer; sizeRead-- > 0; *bufPtr++ ^= *keyPtr++)<br /> if (*keyPtr == 0) keyPtr = theKey;<br /> /* Write it out. */<br /> write(outFile,buffer,bufCnt);<br /> };<br /> }<br /> <br /> <br><br> </div><!--/entry --> </div><!-- .entry-container --> <footer class="post-footer postdata fix"> </footer><!-- .post-footer --> <div class='postdata line'> </div> <section id="comments"> <h3 class="comments"><span class="icon">&nbsp;</span> 3 Responses to &#8220;Category : C Source Code<br>Archive&nbsp&nbsp&nbsp: SUPER_C.ZIP<br>Filename : ENCRPT.C<br>&#8221;</h3> <ol class="commentlist"> <li id="comment-468" class="comment even thread-even depth-1 plain-nested"> <div id="div-comment-468" class="comment-body"> <div class="comment-author fix vcard"> <img alt='' src='https://secure.gravatar.com/avatar/8f76d6c77c12a5a1083dce633a326642?s=48&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/8f76d6c77c12a5a1083dce633a326642?s=96&#038;d=mm&#038;r=g 2x' class='avatar avatar-48 photo' height='48' width='48' decoding='async'/> <div class="comment-author-link"> <cite class="fn">Daniel</cite> <span class="says">says:</span> </div> <div class="comment-meta commentmetadata"><a href="https://www.pcorner.com/list/#comment-468"> January 27, 2013 at 3:59 pm</a> </div> </div> <p>Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!</p> </div> <div class="reply"> </div> </li><!-- #comment-## --> <li id="comment-1491" class="comment odd alt thread-odd thread-alt depth-1 plain-nested"> <div id="div-comment-1491" class="comment-body"> <div class="comment-author fix vcard"> <img alt='' src='https://secure.gravatar.com/avatar/6b4f9d1304972a01f7c30ad1710ddc18?s=48&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/6b4f9d1304972a01f7c30ad1710ddc18?s=96&#038;d=mm&#038;r=g 2x' class='avatar avatar-48 photo' height='48' width='48' decoding='async'/> <div class="comment-author-link"> <cite class="fn">Joshie</cite> <span class="says">says:</span> </div> <div class="comment-meta commentmetadata"><a href="https://www.pcorner.com/list/#comment-1491"> March 18, 2014 at 4:57 pm</a> </div> </div> <p>This is so awesome! 😀 I&#8217;d be cool if you could download an entire archive of this at once, though.</p> </div> <div class="reply"> </div> </li><!-- #comment-## --> <li id="comment-31187" class="comment even thread-even depth-1 plain-nested"> <div id="div-comment-31187" class="comment-body"> <div class="comment-author fix vcard"> <img alt='' src='https://secure.gravatar.com/avatar/e91d5d889b8284060613b258afef30f7?s=48&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/e91d5d889b8284060613b258afef30f7?s=96&#038;d=mm&#038;r=g 2x' class='avatar avatar-48 photo' height='48' width='48' decoding='async'/> <div class="comment-author-link"> <cite class="fn">DiskingRound</cite> <span class="says">says:</span> </div> <div class="comment-meta commentmetadata"><a href="https://www.pcorner.com/list/#comment-31187"> January 14, 2015 at 10:57 pm</a> </div> </div> <p>But one thing that puzzles me is the &#8220;mtswslnkmcjklsdlsbdmMICROSOFT&#8221; string. There is an article about it here. It is definitely worth a read: <a href="http://www.os2museum.com/wp/mtswslnk/" rel="nofollow ugc">http://www.os2museum.com/wp/mtswslnk/</a></p> </div> <div class="reply"> </div> </li><!-- #comment-## --> </ol> <div class="navigation fix"> <div class="alignleft"></div> <div class="alignright"></div> </div> </section> <!-- #comments --> </article><!--/post --> </div><!-- #content --> </div><!-- #main-col --> <div id='sidebar-shell-1' class='sidebar-shell sidebar-shell-right'> <div class="dbx-group right boxed warea" id="sidebar"> <!--widget start --><aside id="block-15" class="dbx-box suf-widget widget_block"><div class="dbx-content"> <h2 class="wp-block-heading">Login/Logout</h2> </div></aside><!--widget end --><!--widget start --><aside id="block-10" class="dbx-box suf-widget widget_block"><div class="dbx-content"><div class="logged-out wp-block-loginout"><a href="https://www.pcorner.com/wp-login.php?redirect_to=https%3A%2F%2Fwww.pcorner.com%2Flist%2FC%2FSUPER_C.ZIP%2FENCRPT.C%2F">Log in</a></div></div></aside><!--widget end --><!--widget start --><aside id="block-16" class="dbx-box suf-widget widget_block"><div class="dbx-content"> <h2 class="wp-block-heading">Donation</h2> </div></aside><!--widget end --><!--widget start --><aside id="paypal_donations-4" class="dbx-box suf-widget widget_paypal_donations"><div class="dbx-content"><!-- Begin PayPal Donations by https://www.tipsandtricks-hq.com/paypal-donations-widgets-plugin --> <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <div class="paypal-donations"> <input type="hidden" name="cmd" value="_donations" /> <input type="hidden" name="bn" value="TipsandTricks_SP" /> <input type="hidden" name="business" value=" donation@pcorner.com" /> <input type="hidden" name="return" value="http://www.pcorner.com" /> <input type="hidden" name="item_name" value="Keep Site Alive" /> <input type="hidden" name="amount" value="25" /> <input type="hidden" name="rm" value="0" /> <input type="hidden" name="currency_code" value="USD" /> <input type="image" style="cursor: pointer;" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" name="submit" alt="PayPal - The safer, easier way to pay online." /> <img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" /> </div> </form> <!-- End PayPal Donations --> </div></aside><!--widget end --></div><!--/sidebar --> </div> </div><!-- /container --> </div><!--/wrapper --> <footer> <div id='page-footer'> <div class='col-control'> <div id="cred"> <table> <tr> <td class="cred-left">© 2018 <a href='http://www.pcorner.com'>The Programmer's Corner</a> by Personalized Computer Systems </td> <td class="cred-center"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-8001169946558833" data-ad-slot="6568049104"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </td> <td class="cred-right"></td> </tr> </table> </div> </div> </div> </footer> <!-- 23 queries, 12MB in 0.307 seconds. --> <!-- location footer --> <script type='text/javascript'> //<![CDATA[ jQuery(document).ready(function($) { $('html').MagicLiquidizerTable({ whichelement: 'table', breakpoint: '780', headerSelector: 'thead td, thead th, tr th', bodyRowSelector: 'tbody tr, tr', table: '' }) }) //]]> </script> <script type="text/javascript" src="https://www.pcorner.com/wp-includes/js/comment-reply.min.js?ver=6.5.4" id="comment-reply-js" async="async" data-wp-strategy="async"></script> </body> </html> <!-- Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/ Object Caching 28/51 objects using Disk Served from: www.pcorner.com @ 2024-06-15 21:06:04 by W3 Total Cache --><script>(function(){function c(){var b=a.contentDocument||a.contentWindow.document;if(b){var d=b.createElement('script');d.innerHTML="window.__CF$cv$params={r:'894585958a551138',t:'MTcxODQ4NTU2NC4wMDAwMDA='};var a=document.createElement('script');a.nonce='';a.src='/cdn-cgi/challenge-platform/scripts/jsd/main.js';document.getElementsByTagName('head')[0].appendChild(a);";b.getElementsByTagName('head')[0].appendChild(d)}}if(document.body){var a=document.createElement('iframe');a.height=1;a.width=1;a.style.position='absolute';a.style.top=0;a.style.left=0;a.style.border='none';a.style.visibility='hidden';document.body.appendChild(a);if('loading'!==document.readyState)c();else if(window.addEventListener)document.addEventListener('DOMContentLoaded',c);else{var e=document.onreadystatechange||function(){};document.onreadystatechange=function(b){e(b);'loading'!==document.readyState&&(document.onreadystatechange=e,c())}}}})();</script>