You are on page 1of 11

NMS LAB Q.1. Write a Program(WAP) to implement Additive Cipher technique on the string received as input from user.

Q.2. WAP that performs the Substitution Cipher on the string USICT GGSIP UNIVERSITY.

Q3. WAP to implement Autokey Cipher technique on the string NETWORK SECURITY. The Initial key value is to be input by the user.

Q4.WAP to implement Caesar Cipher technique on the string "DELHIINDIA"

Q5) Write a program to implement the DES Algorithm clearly showing the Encryption and Decryption techniques using C/C++/C#/JCA .

Q6) Write a Program to simulate the Asymmetric Cryptography demonstrating the concept of the Public - Private Key sharing concept.

/* Q.1. Write a Program(WAP) to implement Additive Cipher technique on the string received as input from user. */

import java.util.*;

public class Add_Cipher { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter the plain text to be encrypted using Additive cipher technique "); String msg = in.nextLine().toUpperCase(); System.out.println("Enter the key "); int k = Integer.parseInt(in.nextLine()); String cText = "";

for (int i = 0; i < msg.length(); i++) { if (Character.isLetter(msg.charAt(i))) cText+= (char) ('A' + (msg.charAt(i) - 'A' + k) % 26); } System.out.println("The ciphertext is "+cText); System.out.println("Now decrypting the ciphertext "); int dk= 26-k; String dText=""; for (int i = 0; i < cText.length(); i++) { dText+= (char) ('A' + (cText.charAt(i) - 'A' + dk) % 26); } System.out.println("The plaintext retrieved back is- "+dText); } }

/*output C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac Add_Cipher.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java Add_Cipher Enter the plain text to be encrypted using Additive cipher technique WE WILL ATTACK BEFORE THE DAWN Enter the key 9 The ciphertext is FNFRUUJCCJLTKNOXANCQNMJFW Now decrypting the ciphertext The plaintext retrieved back is- WEWILLATTACKBEFORETHEDAWN */

/*Q.2. WAP that performs the Substitution Cipher on the string USICT GGSIP UNIVERSITY.*/ import java.util.*;

public class Sub_Cipher { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("The plain text is USICT GGSIP UNIVERSITY "); String msg= "USICT GGSIP UNIVERSITY"; System.out.println("Enter the key::"); int k = Integer.parseInt(in.nextLine()); String cText = ""; for (int i = 0; i < msg.length(); i++) { if (Character.isLetter(msg.charAt(i))) cText+= (char) ('A' + (msg.charAt(i) - 'A' + k) % 26); } System.out.println("The ciphertext is "+cText); System.out.println("Now decrypting the ciphertext "); int dk= 26-k; String dText=""; for (int i = 0; i < cText.length(); i++) { dText+= (char) ('A' + (cText.charAt(i) - 'A' + dk) % 26);

} System.out.println("The plaintext retrived back is- "+dText); } }

/output C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac Sub_Cipher.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java Sub_Cipher The plain text is USICT GGSIP UNIVERSITY Enter the key::

5 The ciphertext is ZXNHYLLXNUZSNAJWXNYD Now decrypting the ciphertext The plaintext retrived back is- USICTGGSIPUNIVERSITY /

/*Q3. WAP to implement Autokey Cipher technique on the string NETWORK SECURITY. The Initial key value is to be input by the user..*/ import java.util.*;

public class Autokey_Cipher { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("The plain text is NETWORK SECURITY "); StringBuffer msg= new StringBuffer("NETWORK SECURITY"); msg.deleteCharAt(7); System.out.println("Enter the initial key::"); int k = Integer.parseInt(in.nextLine()); String cText = ""; cText += (char) ('A' + ('N' - 'A' + k) % 26); for (int i = 1; i < msg.length(); i++) { if (Character.isLetter(msg.charAt(i))) cText+= (char) ('A' + (msg.charAt(i) - 'A' + msg.charAt(i-1)) % 26); }

System.out.println("The ciphertext is "+cText); System.out.println("Now decrypting the ciphertext "); //int dk= 26-(msg.charAt(i-1)); String dText=""; dText +=(char) ('A' + (cText.charAt(0) - 'A' + 26-k) % 26);

for (int i = 1; i < cText.length(); i++) { dText+= (char) ('A' + (cText.charAt(i) + 'A' -dText.charAt(i-1)) % 26);

} System.out.println("The plaintext retrieved back is- "+dText); } }

output C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac Autokey_Cipher.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java Autokey_Cipher The plain text is NETWORK SECURITY Enter the initial key:: 7 The ciphertext is UEKCXSOPJTJYMOE Now decrypting the ciphertext The plaintext retrieved back is- NETWORKSECURITY C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>

/* Q4.WAP to implement Caesar Cipher technique on the string "DELHIINDIA".*/

public class Caesar_Cipher { public static void main(String[] args) {

System.out.println("The plain text to be encrypted using Caesar cipher tech. is DELHIINDIA "); String msg= "DELHIINDIA"; int k = 3; String cText = ""; for (int i = 0; i < msg.length(); i++) { if (Character.isLetter(msg.charAt(i))) cText+= (char) ('A' + (msg.charAt(i) - 'A' + k) % 26); } System.out.println("The ciphertext is "+cText); System.out.println("Now decrypting the ciphertext "); int dk= 26-k; String dText=""; for (int i = 0; i < cText.length(); i++) { dText+= (char) ('A' + (cText.charAt(i) - 'A' + dk) % 26);

} System.out.println("The plaintext retrieved back is- "+dText); } }

C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac Caesar_Cipher.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java Caesar_Cipher The plain text to be encrypted using Caesar cipher tech. is DELHIINDIA The ciphertext is GHOKLLQGLD Now decrypting the ciphertext The plaintext retrieved back is- DELHIINDIA C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>

/* Q5 Write a program to implement the DES Algorithm clearly showing the Encryption and Decryption techniques using C/C++/C#/JCA . */ import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.security.spec.*; import java.util.*; public class DES { public static void main(String args[]) { File desFile=null; System.out.println("Name of the plaintext file to be encrypted is "+args[0]); try { desFile = new File("out.des"); int i; String map1=args[0]; // System.out.println(map1); int number1 = map1.length();

// Create Key KeyGenerator kg = KeyGenerator.getInstance("DES"); SecretKey secretKey = kg.generateKey(); // Create Cipher Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, secretKey); System.out.println("The cipher is "+desCipher);

// Create stream FileOutputStream fos = new FileOutputStream(desFile); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos,desCipher); ObjectOutputStream oos = new ObjectOutputStream(cos); // Write objects oos.writeUTF(map1); oos.writeInt(number1); oos.flush(); oos.close(); // Change cipher mode desCipher.init(Cipher.DECRYPT_MODE, secretKey); // Create stream FileInputStream fis = new FileInputStream(desFile); BufferedInputStream bis = new BufferedInputStream(fis); CipherInputStream cis = new CipherInputStream(bis,desCipher); ObjectInputStream ois = new ObjectInputStream(cis); // Read objects String map2 = ois.readUTF(); int number2 = ois.readInt(); ois.close(); // Compare original with what was read back if (map1.equals(map2) && (map1.length() == number2)) { System.out.println("The file retrieved back is same as the encrypted one."); } else { System.out.println("Problems during encryption/decryption process."); } } catch (NoSuchPaddingException e) { System.err.println("Padding problem: " + e); } catch (NoSuchAlgorithmException e) { System.err.println("Invalid algorithm: " + e); } catch (InvalidKeyException e) { System.err.println("Invalid key: " + e); } catch (IOException e) { System.err.println("I/O Problem: " + e); } catch (Exception e) { System.err.println("Class loading Problem: " + e); } } } C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac DES.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java DES abc

Name of the plaintext file to be encrypted is abc The cipher is javax.crypto.Cipher@65b4fad5 The file retrieved back is same as the encrypted one. C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>

/* Q6) Write a Program to simulate the Asymmetric Cryptography demonstrating the concept of the Public - Private Key sharing concept .*/ import java.math.BigInteger; import java.security.SecureRandom;

public class RSA { private final static BigInteger one = new BigInteger("1"); private final static SecureRandom random = new SecureRandom(); private BigInteger privateKey; private BigInteger publicKey; private BigInteger modulus; // generate an N-bit (roughly) public and private key void CCA_key(int N) { BigInteger p = BigInteger.probablePrime(N/2, random); BigInteger q = BigInteger.probablePrime(N/2, random); BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1 privateKey = publicKey.modInverse(phi); }

BigInteger src_encrypt(BigInteger message) { return message.modPow(publicKey, modulus); } BigInteger dest_decrypt(BigInteger encrypted) { return encrypted.modPow(privateKey, modulus); } public String toString() { String s = ""; s += "public = " + publicKey + "\n"; s += "private = " + privateKey + "\n"; s += "modulus = " + modulus; return s; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); RSA key = new RSA(); key.CCA_key(N); System.out.println(key); // create random message, encrypt and decrypt BigInteger message = new BigInteger(N-1, random); //// create message by converting string to integer // String s = "test"; // byte[] bytes = s.getBytes(); // BigInteger message = new BigInteger(s); BigInteger src_encrypt = key.src_encrypt(message); BigInteger dest_decrypt = key.dest_decrypt(src_encrypt); System.out.println("message = " + message); System.out.println("encrpyted = " + src_encrypt); System.out.println("decrypted = " + dest_decrypt); } } C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>javac RSA.java C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>java RSA 50 public = 65537 private = 144964959361253 modulus = 821493230061169 message = 487939472386166 encrpyted = 5195649535921 decrypted = 487939472386166 C:\Users\Amresh\Desktop\3rd_cse\nms\nms_lab>

You might also like