/* zx8 (ps) - Stream Cipher Algorithm/Source Code */ /* Copyright (c) 2012, Karl-Uwe Frank All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #** zx8 (ps) algorithm developed by Karl-Uwe Frank */ #include #include #include #include #include #include //----------------------------------------- // // rm -f zx8_ps && gcc zx8_ps.c -o zx8_ps // // ./zx8_ps -e Plainfile Cipherfile // // ./zx8_ps -d Cipherfile Plainfile // #include "zx8_ps_algorithm.c"; //----------------------------------------- // void ShowUsage(char* ThisName) { fprintf(stderr, "\nUsage: %s -e InFile OutFile for encryption\n", ThisName); fprintf(stderr, "Usage: %s -d InFile OutFile for decryption\n\n", ThisName); } //----------------------------------------- // Invisible Keyboard Input // // http://www.undertec.de/blog/2009/05/ // int getch() { static int ch = -1, fd = 0; struct termios neu, alt; fd = fileno(stdin); tcgetattr(fd, &alt); neu = alt; neu.c_lflag &= ~(ICANON|ECHO); tcsetattr(fd, TCSANOW, &neu); ch = getchar(); tcsetattr(fd, TCSANOW, &alt); return ch; } //----------------------------------------- // Main // int main (int argc, char *argv[]) { // Check if a Parameter are passed through char* PrgName; char* InFile; char* OutFile; char KeyComp[256]; char* Direction; PrgName = argv[0]; if (argc < 4) { ShowUsage(PrgName); return 1; } else { Direction = argv[1]; if ( (strcmp(Direction, "-e") != 0) && (strcmp(Direction, "-d") != 0) ) { ShowUsage(PrgName); return 1; } // Set InFile and OutFile Name InFile = argv[2]; OutFile = argv[3]; } //----------------------------------------- // // Read the Encryption Keyword // int n; char c; if (argc == 5) { // If passed as Parameter sprintf (KeyWord, "%s", argv[4]); KeyLen = strlen(KeyWord); } else { printf ("Enter Keyword: "); n=0; c=0; while(c != '\n'){c=getch();KeyWord[n++]=c;}; KeyWord[n-1]='\0'; printf ("\n"); if ( strcmp(Direction, "-e") == 0) { printf ("Enter Keyword again: "); n=0; c=0; while(c != '\n'){c=getch();KeyComp[n++]=c;}; KeyComp[n-1]='\0'; printf ("\n"); if (strcmp(KeyWord, KeyComp) != 0) { printf ("Entered Keywords did not match\n\n"); return 1; } } KeyLen = n-1; if ((KeyLen) < 8 ) { printf ("Keyword must be at least 8 unsigned characters long\n\n"); return 1; } } printf ("Keyword is: %s\n", KeyWord); //----------------------------------------- // Initialisation of zx8 (ps) // // perform the Key Schedule KSA(); unsigned int FileLen; unsigned char inB, outB; if (strcmp(Direction, "-e") == 0) { printf("===================================================\n"); printf("Encrypting: %s\ninto file : %s\n", InFile, OutFile); printf("===================================================\n"); } else { printf("===================================================\n"); printf("Decrypting: %s\ninto file : %s\n", InFile, OutFile); printf("===================================================\n"); } FILE* fIn = fopen(InFile, "rb"); if (fIn == NULL) { printf("error opening file: %s\n", InFile); return -1; } fseek(fIn , 0 , SEEK_END); FileLen = ftell(fIn); rewind(fIn); FILE* fOut = fopen(OutFile, "wb"); if (fOut == NULL) { printf("error creating file: %s\n", OutFile); return -1; } printf ("%10u byte file size\n", FileLen); // Encrypt/Decrypt Binary File (unbuffered Read/Write) while (fread(&inB, sizeof(char), 1, fIn)) { outB = inB ^ PRGA(); fwrite(&outB, 1, 1, fOut); } return 0; }