Css Exp 01
Css Exp 01
import javax.swing.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//Take in a String throw JOptionpane, convert to Integer and hash it, then run brute force
until a match for the
// int is found
static void compare(){
//Declared as null because assignment bellow happens in a try-catch which could
(theoretically) fail
byte[] userDefined=null;
//Same thing as above goes for the Integer
int inp=0;
try{
//Try to parse the entered String to an Integer and then hash it
inp = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter your
integer!"));
userDefined=hash(inp);
}
//Should the parsing or the hash fail, the function will call itself and start over
catch(Exception e){
compare();
}
//Check if the Integer is smaller than 0, if it is, start over
if(inp<0){
try {
//JOptionPane can throw a NullPointerException when clicking on window x, so
we use a try-catch statement
//to eliminate a potential software termination
JOptionPane.showMessageDialog(null, "The integer must be bigger than or equal
to 0!");
}
//Should an exception be thrown, the program will terminate. Replace with compare();
if you would like
//it to try again instead of stopping
catch(NullPointerException e){
System.exit(-1);
}
//if the input is smaller than 0, start over
compare();
}
//Brute force starts here. Start at int 0, and goes till the integer size limit is reached,
calculating the hash
//of each integer and comparing it to the input's hash. If a match is found, it prints out
the input in plain text.
for(int i=0;i<2147483647;i++){
//Print out the current integer, it's good to have an overview if you enter a higher
valued integer that
// takes longer
System.out.println(i);
//Check if the current integer's hash matches the input's hash
if(MessageDigest.isEqual(hash(i),userDefined)){
//if a match is found, print the integer in plain text. Again, try catch is for the
JOptionPane
try {
JOptionPane.showMessageDialog(null, "Found a match for " + i);
}
catch(Exception e){
System.out.println("Found a match for " + i);
}
//and terminate the program
System.exit(0);
break;
}
}
//This statement is only reached if no match is found
System.out.println("No match found!");
}
//Hashing function. Takes an Integer input and returns the hash as a byte array
static byte[] hash(int input){
//Because assignment is in a try-catch statement, we set the start value to null
byte[] output=null;
//Convert the integer input to a String, because the hash function uses a String
String inp=Integer.toString(input);
//try-catch because misspelling the Algorithm type will return a
NoSuchAlgorithmException
try{
//Set hash algorithm type
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Calculate the inputs hash as a byte array, and assign it to the variable output
output = digest.digest(inp.getBytes(StandardCharsets.UTF_8));
}
//Catch NoSuchAlgorithmException
catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
//return the byte array
return output;
}
Output:-
Code: -
import java.awt.Point;
import java.util.Scanner;
public class PlayfairCipher
{
//length of digraph array
private int length = 0;
//creates a matrix for Playfair cipher
private String [][] table;
//main() method to test Playfair method
public static void main(String args[])
{
PlayfairCipher pf = new PlayfairCipher();
}
//main run of the program, Playfair method
//constructor of the class
private PlayfairCipher()
{
//prompts the user for the keyword to use for encoding & creates tables
System.out.print("Enter the key for Playfair cipher: ");
Scanner sc = new Scanner(System.in);
String key = parseString(sc);
while(key.equals(""))
key = parseString(sc);
table = this.cipherTable(key);
//prompts the user for the message to be encoded
System.out.print("Enter the plaintext to be enciphered: ");
//System.out.println("using the previously given keyword");
String input = parseString(sc);
while(input.equals(""))
input = parseString(sc);
//encodes and then decodes the encoded message
String output = cipher(input);
String decodedOutput = decode(output);
//output the results to a user
this.keyTable(table);
this.printResults(output,decodedOutput);
}
//parses an input string to remove numbers, punctuation,
//replaces any J's with I's and makes string all caps
private String parseString(Scanner sc)
{
String parse = sc.nextLine();
//converts all the letters in upper case
parse = parse.toUpperCase();
//the string to be substituted by space for each match (A to Z)
parse = parse.replaceAll("[^A-Z]", "");
//replace the letter J by I
parse = parse.replace("J", "I");
return parse;
}
//creates the cipher table based on some input string (already parsed)
private String[][] cipherTable(String key)
{
//creates a matrix of 5*5
String[][] playfairTable = new String[5][5];
String keyString = key + "ABCDEFGHIKLMNOPQRSTUVWXYZ";
//fill string array with the empty string
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
playfairTable[i][j] = "";
for(int k = 0; k < keyString.length(); k++)
{
boolean repeat = false;
boolean used = false;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(playfairTable[i][j].equals("" + keyString.charAt(k)))
{
repeat = true;
}
else if(playfairTable[i][j].equals("") && !repeat && !used)
{
playfairTable[i][j] = "" + keyString.charAt(k);
used = true;
}
}
}
}
return playfairTable;
}
//cipher: takes input (all upper-case), encodes it, and returns the output
private String cipher(String in)
{
length = (int) in.length() / 2 + in.length() % 2;
//insert x between double-letter digraphs & redefines "length"
Output: -
Code:
// This program calculates the Key for two persons
// using the Diffie-Hellman Key exchange algorithm
class Main{
// Power function to return value of a ^ b mod P
private static long power(long a, long b, long p)
{
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}
// Driver code
public static void main(String[] args)
{
long P, G, x, a, y, b, ka, kb;
// Both the persons will be agreed upon the
// public keys G and P
// A prime number P is taken
P = 23;
System.out.println("The value of P:" + P);
// A primitive root for P, G is taken
G = 9;
System.out.println("The value of G:" + G);
// Alice will choose the private key a
// a is the chosen private key
a = 4;
System.out.println("The private key a for Alice:" + a);
// Gets the generated key
x = power(G, a, P);
// Bob will choose the private key b
// b is the chosen private key
b = 3;
System.out.println("The private key b for Bob:" + b);
// Gets the generated key
y = power(G, b, P);
// Generating the secret key after the exchange
// of keys
ka = power(y, a, P); // Secret key for Alice
kb = power(x, b, P); // Secret key for Bob
System.out.println("Secret key for the Alice is:" + ka);
System.out.println("Secret key for the Bob is:" + kb);
}
}
Output: