Question

Write a javascript program which implements the following two classical cryptosystem which we covered in class:...

Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as input and outputs a corresponding plaintext string. A main function which will offer users the option to select a cryptosystem, mode (encryption or decryption), and specify plaintext/ciphertext and key inputs using either the command line or a simple GUI. The output of each cryptosystem function should be display to stdout. Note that the format of your key will vary depending on which ciphers you choose to implement. For this problem you should submit your source code and a document containing: Your compilation instructions. Your execution instructions. The output from a sample run in which you use your program to encrypt and decrypt a message using each of your cryptosystems which shows the input, output, and keys used in each.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author miracle
*/

import java.util.*;

public class Cryptograph
{
  
public static String encrypt1(String Msg, int a1, int b1)

{

String CTxt = "";

int a = a1;

int b = b1;

for (int i = 0; i < Msg.length(); i++)

{

CTxt = CTxt + (char) ((((a * Msg.charAt(i)) + b) % 26) + 65);

}

return CTxt;

}

public static String decrypt1(String CTxt, int a2, int b2 )

{

String Msg = "";

int a = a2;

int b = b2;

int a_inv = 0;

int flag = 0;

for (int i = 0; i < 26; i++)

{

flag = (a * i) % 26;

if (flag == 1)

{

a_inv = i;

System.out.println(i);

}

}

for (int i = 0; i < CTxt.length(); i++)

{

Msg = Msg + (char) (((a_inv * ((CTxt.charAt(i) - b)) % 26)) + 65);

}

String d=Msg.toLowerCase();
return d;

}


public static String encrypt2(String text, final String key)

{

String res = "";

text = text.toUpperCase();

for (int i = 0, j = 0; i < text.length(); i++)

{

char c = text.charAt(i);

if (c < 'A' || c > 'Z')

continue;

res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');

j = ++j % key.length();

}

return res;

}

public static String decrypt2(String text, final String key)

{

String res = "";

text = text.toUpperCase();

for (int i = 0, j = 0; i < text.length(); i++)

{

char c = text.charAt(i);

if (c < 'A' || c > 'Z')

continue;

res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');

j = ++j % key.length();

}

return res;

}

public static void main(String[] args)

{

System.out.println("Enter your choice \n");
System.out.println("1.Affine Cipher \n");
System.out.println("2.Vignere Cipher");
Scanner sc = new Scanner(System.in);
int choice= sc.nextInt();
switch(choice){
case 1:
System.out.println("1.Encrypt \n");
System.out.println( "2.Decrypt");
int nc= sc.nextInt();
switch(nc){
case 1:
System.out.println("Enter the key values of alphabets a and b");
int a1=sc.nextInt();
int b1=sc.nextInt();
System.out.println("Enter the String to be encrypted");
String af = sc.next();
  


String af1=af.toUpperCase();

System.out.println("Encrypted Message is : "

+ encrypt1(af1,a1,b1));break;
  
case 2:
System.out.println("enter a the values of a and b");
int a2=sc.nextInt();
int b2=sc.nextInt();
System.out.println("Enter the String to be decrypted");
String df= sc.next();
String df1= df.toUpperCase();
System.out.println("Decrypted Message is: "

+ decrypt1(df1,a2,b2));break;
  
}break;
case 2:
  
System.out.println("1.Encrypt /n 2.Decrypt");
int nc1= sc.nextInt();
switch(nc1){
case 1:
System.out.println("Enter the Key");
String ekey= sc.next();
String key= ekey.toUpperCase();
  
System.out.println("Enter the String to be encrypted in lower case");
String ve = sc.next();
String encryptedMsg = encrypt2(ve, key);
System.out.println("Encrypted message: " + encryptedMsg);
  
case 2:
System.out.println("Enter the Key");

String dkey= sc.next();
String key1= dkey.toUpperCase();
System.out.println("Enter the String to be decrypted in lower case");
String vd = sc.next();
String decryptedMsg = decrypt2(vd, key1);

System.out.println("Decrypted message: " + decryptedMsg);
  
  
  
}
  
  
}

}

Add a comment
Know the answer?
Add Answer to:
Write a javascript program which implements the following two classical cryptosystem which we covered in class:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D....

    Computer Science C++ Help, here's the question that needs to be answered (TASK D): Task D. Decryption Implement two decryption functions corresponding to the above ciphers. When decrypting ciphertext, ensure that the produced decrypted string is equal to the original plaintext: decryptCaesar(ciphertext, rshift) == plaintext decryptVigenere(ciphertext, keyword) == plaintext Write a program decryption.cpp that uses the above functions to demonstrate encryption and decryption for both ciphers. It should first ask the user to input plaintext, then ask for a right...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two...

    Study the VIGENÈRE CIPHER and implemented it with c++ 1.In your program, you should have two functions: encryption and decryption. 2. Use any key you like. (Don't use deceptive in the slides) 3. choose a sentence or a paragraph you like as the plaintext. I have the code I just need the implementation in a different way // C++ code to implement Vigenere Cipher #include<bits/stdc++.h> using namespace std; // This function generates the key in // a cyclic manner until...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT