vendredi 19 octobre 2018

One time pad encryptions in java

I've create a one time pad encryption in java but I have two problems which are : 1- In Encryption, how can I can make the size of the key flexible according to the size of the plaintext and generated randomly for example , the size of the plaintext is 4 letters so the size of the array key must be 32-bit, because each letter has 8-bit. 2-In Decryption, how can I read from to files and these two files in binary form and then do XOR between them then print it as ASCLL from.

 
public class onetimepad {

    public static void main(String[] args) throws Exception {
 
        int[] key = generate8BitKey();

        Scanner in = new Scanner(System.in);
        System.out.println(" One Time Pad encryption and decryption ");
        System.out.println(" For encryption Enter 1 ");
        System.out.println(" For decryption Enter 2 ");
        System.out.println(" Exit Enter 3 ");
        int a = in.nextInt();

        switch (a) {
            case 1:

                File input = new File("message.txt");
                Scanner sc = new Scanner(input);
                String msg = sc.nextLine();
                System.out.println("Key:    ");

                //Write the Key in file.
                PrintWriter writer2 = new PrintWriter("Output.txt", "UTF-8");
                writer2.println("------ Key ------- ");
                for (int i : key) {

                    System.out.print(key[i]);
                    writer2.print(key[i]);

                }
                writer2.close();

                System.out.println();
                String ciphertext = encrypt(msg, key);
                System.out.println("Encrypted Message: " + ciphertext);
                 break;
            case 2:
                 File input2 = new File("ciphertext.txt");
                Scanner sc2 = new Scanner(input2);
                String msg2 = sc2.nextLine();
                File input3 = new File("Key.txt");
                Scanner sc3 = new Scanner(input3);
                String msg3 = sc2.nextLine();
                 
               
                 System.out.println("Decrypted Message: " + decrypt(msg3, key));
                break;
            default:
        }

    }// End the main.

    //------------------- Methods.
    public static String encrypt(String msg, int[] key) {
        int[] binmsg = stringToBinary(msg);
        int[] result = xor(binmsg, repeatArray(key, msg.length()));
        String r = "";
        for (int i : result) {
            r += (char) (result[i] + '0');
        }

        return r;
    }

    //---------------------
    public static String decrypt(String ciphertext, int[] key) {
        int[] bin = new int[ciphertext.length()];
        for (int i = 0; i < ciphertext.length(); i++) {
            bin[i] = ciphertext.charAt(i) - '0';
        }
       int[] result = xor(bin, repeatArray(key, bin.length / 8));
        return binaryToString(result);
    }

    //---------------------
    public static int[] stringToBinary(String msg) {
        int[] result = new int[msg.length() * 8];
        for (int i = 0; i < msg.length(); i++) {
            String bin = Integer.toBinaryString((int) msg.charAt(i));
            while (bin.length() < 8) {
                bin = "0" + bin;
            }
            for (int j = 0; j < bin.length(); j++) {
                result[i * 8 + j] = bin.charAt(j) - '0';
            }
        }
        return result;
    }

    //---------------------
    public static String binaryToString(int[] bin) {
        String result = "";
        for (int i = 0; i < bin.length / 8; i++) {
            String c = "";
            for (int j = 0; j < 8; j++) {
                c += (char) (bin[i * 8 + j] + '0');
            }
            result += (char) Integer.parseInt(c, 2);
        }
        return result;
    }

    //---------------------
    public static int[] generate8BitKey() {
        int[] key = new int[8];
        for (int i = 0; i < 8; i++) {
            SecureRandom sr = new SecureRandom();
            key[i] = sr.nextInt(2);
        }
        return key;
    }

    //---------------------
    public static int[] xor(int[] a, int[] b) {
        int[] result = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = a[i] == b[i] ? 0 : 1;
        }
        return result;
    }

    //---------------------
   public static int[] repeatArray(int[] a, int n) {
        int[] result = new int[a.length * n];
        for (int i = 0; i < result.length; i++) {
            result[i] = a[i % a.length];  // mod 
        }
        return result;
    }

}



Aucun commentaire:

Enregistrer un commentaire