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 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.
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 :)
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.
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Consider the following proposal to prevent DES from exhaustive key search under a known-plaintext attack. The...
Consider the following proposal to prevent DES from exhaustive key search under a known-plaintext attack. The secret key is k=k{1,k2}, where k1 is in (0,1) ^56 and k2 is (0,1) ^64. Let m be in (0,1) ^64 be the plaintext message. Encryption is defined as follows Ek(m)=DESk1(m) XOR k2 (a) Show that this proposal does not increase the work needed to break the encryption scheme E using brute force (exhaustive) search (that is, approximately 2^56 DES operations are still sufficient)....
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT