Search code examples
javavigenere

Encrypting using Vigenere cipher where Plaintext is read from a file (JAVA)


I am trying a simple Vigenere cipher where plain text is read from a file and encrypted using a key.

Key =ABC Cipher text is obtained by addition between the PT and the key

Plain Text : W E W I L L A T T A C K T O N I G H T

Key : A B C A B C A B C A B C A B C A B C A

Cipher : W F Y I M N A U V A D M T P P I H J T

How can i repeat the key for the length of the plain text and then encrypt it as it is being read.?

The input is read from a file using the following snippet

FileInputStream fileInput = new FileInputStream("/Documents/file1.txt");

    int r;
    int count = 0 ;


    //Read each character from the file one by one till EOF
    while ((r = fileInput.read()) != -1) 
    {
        char c = (char) r; 
      //SOMETHING NEEDS TO BE DONE HERE
    System.out.print(c); 
    count ++; //Keeps count of the number of characters in the plain text.

Solution

  • public static void encrypt (String a)throws IOException 
    {
    
        //Array of the length of the Plain Text is created
        char pt[] = new char[count];
    
        //File is opened again and this time passed into the plain text array 
        FileInputStream f = new FileInputStream("/Users/avtrulzz/Documents/file1.txt") ;
    
        int s ;
    
        int i = 0 ;
    
        while((s = f.read()) != -1)
    
        {
        //Every character read is simultaneously fed into the array
            pt[i] = (char) s ;
                i ++ ;
        }
    
        //Find the length of the key
        int kl = a.length() ;
        //Find how many times the key has to be repeated
        int keyrep = count / kl ;
    
        //Where keyrep is the number of times you want to repeat the string and a is the string  to repeat.
        String repeated = new String(new char[keyrep]).replace("\0", a);
    
        System.out.println("The key repeated till the length of the plain text");
        System.out.println();
        System.out.println(repeated);
    
        //The string "repeated" is made into an array "keyforpt"
        char keyforpt[] = repeated.toCharArray() ;
    
    
        char ct[] = new char[count] ;
    
        for(int ind = 0; ind < pt.length ; ind++)
    
        {
            ct[ind] = toChar((toInt(pt[ind]) + toInt(keyforpt[ind]) + 1) % 26);
        }
    
        System.out.println() ;
        for(int temp = 0;temp < ct.length;temp++)
            {
    
                System.out.print(ct[temp]) ;
            }
        System.out.println() ;
    }
    
    
    private static int toInt(char chr)
    {
        return chr - 'a';
    }
    
    private static char toChar(int value) 
    {
        return (char)(value + 'a');
    }