/* 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 */ import java.io.Console; import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Arrays; //----------------------------------------- // // rm -f zx8_ps_encrypt.class && javac zx8_ps_encrypt.java // // java zx8_ps_encrypt -e Plainfile Cipherfile // // java zx8_ps_encrypt -d Cipherfile Plainfile // public class zx8_ps_encrypt { //----------------------------------------- // // Global Varaiable Definition // private static byte KeyWord[] = new byte[256]; private static int KeyLen = 0; // Secret State Arrays // private static int z[] = new int[256]; private static int x[] = new int[256]; // Global Carry on Array Indices // private static int a = 0; private static int b = 0; //----------------------------------------- // Main // public static void main (String args[]) throws IOException { // Check if a Parameter are passed through String Direction = ""; String InFile = ""; String OutFile = ""; String PrgName = "zx8_ps_encrypt"; try { if (args.length < 3) { ShowUsage(PrgName); System.exit(1); } else { Direction = args[0]; if ( (!Direction.equals("-e")) && (!Direction.equals("-d")) ) { ShowUsage(PrgName); System.exit(1); } // Set InFile and OutFile Name InFile = args[1]; OutFile = args[2]; } //----------------------------------------- // // Read the Encryption Keyword // Console getch = System.console(); char[] KeyRead; if (args.length == 4) { // If passed as Parameter // Convert String to CharArray KeyRead = args[3].toCharArray(); KeyLen = KeyRead.length; } else { KeyRead = getch.readPassword("Enter Keyword: "); if ( Direction.equals("-e")) { char[] KeyComp = getch.readPassword("Enter Keyword again: "); /* To compare two char arrays use, static boolean equals(char array1[], char array2[]) method of Arrays class. It returns true if both arrays are equal. Arrays are considered as equal if they contain same elements in same order. */ if ( !Arrays.equals(KeyRead, KeyComp) ) { System.out.println("KeyRead = " + String.valueOf(KeyRead)); System.out.println("KeyComp = " + String.valueOf(KeyComp)); System.out.printf("Entered Keywords did not match\n\n"); System.exit(1); } } KeyLen = KeyRead.length; if (KeyLen < 8) { System.out.printf("Keyword must be at least 8 unsigned characters long\n\n"); System.exit(1); } } // Convert Char Array into String System.out.println("Keyword: " + String.valueOf(KeyRead)); // Convert Char Array into byte Array //KeyWord = KeyRead.getBytes(); KeyWord = charToBytesArr(KeyRead); long startTime = System.currentTimeMillis(); //----------------------------------------- // Initialisation of zx8 (ps) // // perform the Key Schedule KSA(KeyWord); if (Direction.equals("-e")) { System.out.printf("===================================================\n"); System.out.printf("Encrypting: %s\ninto file : %s\n", InFile, OutFile); System.out.printf("===================================================\n"); } else { System.out.printf("===================================================\n"); System.out.printf("Decrypting: %s\ninto file : %s\n", InFile, OutFile); System.out.printf("===================================================\n"); } //----------------------------------------- // // Encrypt/Decrypt Binary File(buffered Read/Write) // FileInputStream fInStream = new FileInputStream(InFile); FileOutputStream fOutStream = new FileOutputStream(OutFile); System.out.printf("%d byte file size\n", fInStream.available()); int BuffSize = 16384; byte[] inByte = new byte[BuffSize]; byte[] outByte = new byte[BuffSize]; int inB, outB, bufferRead; while ((bufferRead = fInStream.read(inByte, 0, BuffSize)) != -1) { for (int i=0; i= 60-Bit of Entropy. int i, n, j, k, t; // Prefill the Arrays for (i=0; i<256; i++) { z[i] = i; x[i] = i; } a=0; b=0; j=0; k=0; n=0; t=0; for (i=0; i<256; i++) { k = (i % KeyWord.length); for (n=0; n<128; n++) t = PRGA() & 0xff; j = (t + j + z[i] + KeyWord[k]) & 0xff; swap(z, i, j); // z[i] <==> z[j] for (n=0; n<128; n++) t = PRGA() & 0xff; j = (t + j + x[i] + z[x[j]]) & 0xff; swap(x, i, j); // x[i] <==> x[j] } // Reset the Array Indices Start Point a=0; b=0; } //----------------------------------------- // // Pseudo Random Generation Algorithm (ps) // public static int PRGA() { int n1, n2, y, m; // Calculate distant Array Element Indices n1 = (z[a] + x[a]) & 0xff; n2 = (z[b] + x[b]) & 0xff; // First Swap randomly selected Array Element swap(z, a, n1); // z[a] <==> z[n1] swap(x, a, n2); // x[a] <==> x[n2] // Update the global Carry on Array Indices a = (a + b + (n1^n2)) & 0xff; b = (b + 1) & 0xff; // Second Swap sequentially cycle over every Array Element swap(z, b, n1); // z[b] <==> z[n1] swap(x, b, n2); // x[b] <==> x[n2] // Calculate the internal State Selector Value y = (z[n1] ^ x[n2]) & 0xff; // Calculate the internal State Protection Value m = (n1 + n2) & 0xff; // Never reveal internal State Values directly return (z[x[y]] ^ m) & 0xff; } }