Question

In: Computer Science

Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be...

Write an Java/C program code to perform known-plaintext attack on Permutation Cipher.
This program can be used to determine the length of the permutation m and the
key permutation.

Solutions

Expert Solution

// Java program for encoding the string

import java.util.Scanner; // Import the Scanner class
public class GFG
{

   // Function generates the encoded text
   static String encoder(char[] key)
   {
       String encoded = "";
      
       // This array represents the
       // 26 letters of alphabets
       boolean[] arr = new boolean[26];

       // This loop inserts the keyword
       // at the start of the encoded string
       for (int i = 0; i < key.length; i++)
       {
           if (key[i] >= 'A' && key[i] <= 'Z')
           {
               // To check whether the character is inserted
               // earlier in the encoded string or not
               if (arr[key[i] - 65] == false)
               {
                   encoded += (char) key[i];
                   arr[key[i] - 65] = true;
               }
           }
           else if (key[i] >= 'a' && key[i] <= 'z')
           {
               if (arr[key[i] - 97] == false)
               {
                   encoded += (char) (key[i] - 32);
                   arr[key[i] - 97] = true;
               }
           }
       }

       // This loop inserts the remaining
       // characters in the encoded string.
       for (int i = 0; i < 26; i++)
       {
           if (arr[i] == false)
           {
               arr[i] = true;
               encoded += (char) (i + 65);
           }
       }
       return encoded;
   }

   // Function that generates encodes(cipher) the message
   static String cipheredIt(String msg, String encoded)
   {
       String cipher = "";

       // This loop ciphered the message.
       // Spaces, special characters and numbers remain same.
       for (int i = 0; i < msg.length(); i++)
       {
           if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')
           {
               int pos = msg.charAt(i) - 97;
               cipher += encoded.charAt(pos);
           }
           else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')
           {
               int pos = msg.charAt(i) - 65;
               cipher += encoded.charAt(pos);
           }
           else
           {
               cipher += msg.charAt(i);
           }
       }
       return cipher;
   }   // Driver code
   public static void main(String[] args)
   {
      
        Scanner myObj = new Scanner(System.in); // Create a Scanner object
      

          System.out.println("Enter length of key : " ); //length of the key

        int len = myObj.nextInt();

       // Hold the Keyword
           System.out.println("enter Keyword : " );
       String key = myObj.nextLine(); // Read user input
  
       System.out.println("Keyword : " + key);

       // Function call to generate encoded text
       String encoded = encoder(key.toCharArray());

       // Message that need to encode
           System.out.println("enter message : " );
       String message = myObj.nextLine(); // Read user input
       System.out.println("Message before Ciphering : " + message);

       // Function call to print ciphered text
       System.out.println("Ciphered Text : " + cipheredIt(message,
               encoded));
   }
}


Related Solutions

Write a C++ program performing the rot13 cipher, The code should perform like this: The user...
Write a C++ program performing the rot13 cipher, The code should perform like this: The user should be able to input any letter or words, or even sentences where once they have inputted the particular word, each letter goes 13 letters ahead, so an 'A' becomes an 'N', a 'C' becomes 'P', and so on. If rot13 cipher is tested a second time, the original plantext should be restored: 'P' becomes 'C', 'N' becomes 'A'. The 13 letters go in...
Write a C++ program performing the rot13 cipher: Basically the code should perform like this: The...
Write a C++ program performing the rot13 cipher: Basically the code should perform like this: The user should be able to input any letter or words, or even sentences where once they have inputted the particular word, each letter goes 13 letters ahead, so an 'A' becomes an 'N', a 'C' becomes 'P', and so on. If the rot13 cipher is applied a second time, the original plantext is restored: 'N' becomes 'A', 'P' becomes 'C'. The 13 letters go...
Write a python program that implements a Brute Force attack on Shift Cipher. In this program...
Write a python program that implements a Brute Force attack on Shift Cipher. In this program there is only one input - ciphertext - is a sequence of UPPER CASE letters. To make it easy, the program will be interactive and will output all possible plaintexts and ask user which plaintext makes sense. As soon as user will decide YES, the program will stop searching and print the desired plaintext and the found SHIFT KEY.
Write a java program which can randomly generate a permutation of the integer {1, 2, 3,...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3, ..., 48,49,50}. Use the most efficient sorting algorithm to sort the list in an acceding order.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or...
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. Given an arbitrary cipher text file, you need to write a C++ program to find out the value of the shift going down the...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT