Cryptography and Network Security PPTTTTT Lab
Cryptography and Network Security PPTTTTT Lab
Security
• HRITHIK JAISWAL
• 17601A0541
• CSE 3 B
1. XOR a string with a Zero
AIM: Write a C program that contains a string (char pointer) with a value
\ Hello World’ . The program should XOR each character in this string with 0 and display
the result.
PROGRAM:
•
• #include<stdlib.h>
• main()
• {
• char str[]="Hello World";
• char str1[11];
• int i,len;
• len=strlen(str);
• for(i=0;i<len;i++){
• str1[i]=str[i]^0; printf("%c",str1[i]);
• }
• printf(" \ n");
• }
•
•
2. XOR a string with a 127
• AIM: Write a C program that contains a string (char pointer) with a value \ Hello World’.
The program should AND or and XOR each character in this string with 127 and display
the result.
• PROGRAM:
• #include <stdio.h> #include<stdlib.h> void main()
• {
• char str[]="Hello World"; char str1[11];
• char str2[11]=str[]; int i,len;
• len = strlen(str);
• for(i=0;i<len;i++)
• {
• str1[i] = str[i]&127; printf("%c",str1[i]);
• }
• printf(" \ n");
• for(i=0;i<len;i++)
• {
• str3[i] = str2[i]^127; printf("%c",str3[i]);
• }
• 3.Encryption & Decryption using Cipher Algorithms
• AIM: Write a Java program to perform encryption and decryption using the following algorithms:
• a) Ceaser Cipher
• b) Substitution Cipher
• c) Hill Cipher
• a.Ceaser Cipher
import java.io.*;
• import java.util.*;
• public class HillCipher
• {
• static float[][] decrypt=new float[3][3];
• static float[][] a=new float[3][3];
• static float[][] b=new float[3][3];
• static float[][] mes=new float[3][1];
• static float[][] res=new float[3][1];
• static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• static Scanner sc=new Scanner(System.in);
• public static void main(String args[])throws IOException{
• getKeymes();
• for(int i=0;i<3;i
• {
• res[i][j]=res[i][j]+a[i][k]*mes[k][j];
• }
• System.out.print("\nEncrypted string is:");
• for(int i=0;i<3;i++)
• {
• System.out.print((char)(res[i][0]%26+97));
• res[i][0]=res[i][0];
• }
• inverse();
• for(int i=0;i<3;i++)
• for(int j=0;j<1;j++)
• for(int k=0;k<3;k++)
• {
• decrypt[i][j]=decrypt[i][j]+b[i][k]*res[k][j];
• }
• System.out.print("\n Decrypted string is:");
• for(int i=0;i<3;i++){
• System.out.print((char)(decrypt[i][0]%26+97));
• }
• System.out.print("\n");
• public static void getKeymes()throws IOException{
• System.out.println("Enter 3*3 matrix for key(it should be inversible):");
• for(int i=0;i<3;i++)
• for(int j=0;j<3;j++)
• a[i][j]=sc.nextFloat();
• System.out.print("\n Enter a 3 letter string:");
• String msg=br.readLine();
• for(int i=0;i<3;i++)
• mes[i][0]=msg.charAt(i)-97;
• }
• public static void inverse(){
• float p,q;
• float[][] c=a;
• for(int i=0;i<3;i++)
• for(int j=0;j<3;j++)
• if(i==j)
• b[i][j]=1;
• else
• b[i][j]=0;)
• {
• for(int i=0;i<3;i++){
• p=c[i][k];
• q=c[k][k];
• for(int j=0;j<3;j++)
• {
• if(i!=k)
• {
• c[i][j]=c[i][j]*q-p*c[k][j];
• b[i][j]=b[i][j]*q-p*b[k][j];
• }
• }
• }
• }
• for(int i=0;i<3;i++)
• for(int j=0;j<3;j++)
• {
• b[i][j]=b[i][j]/c[i][i];
• }
• System.out.println();
• System.out.println("\n Inverse matrix is:");
• for(int i=0;i<3;i++)
• {
• for(int j=0;j<3;j++)
• System.out.print(b[i][j]+"");
• System.out.print("\n");
• }
• }
• }
• b.Substitution cipher
• import java.io.*;
• import java.util.*;
• public class SubstitutionCipher
• {
• static Scanner sc=new Scanner(System.in);
• static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
• public static void main(String[] args)throws IOException{
• String a="abcdefghijklmnopqrstuvwxyz";
• String b="zyxwvutsrqponmlkjihgfedcba";
• System.out.println("Enter any string:");
• String str=br.readLine();
• String decrypted="";
• char c;
• for(int i=0;i<str.length();i++)
• {
• c=str.charAt(i);
• int j=a.indexOf(c);
• decrypted=decrypted+b.charAt(j);
• }
• System.out.print("Encrypted data is:"+decrypted);
• }
• }
• c.Ceaser cipher
• import java.io.BufferedReader;
• import java.io.*;
• import java.util.Scanner;
• public class ceaser3{
• static Scanner sc =new Scanner(System.in);
• static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
• public static void main(String args[]) throws IOException{
• System.out.println("Enter any String:");
• String str=br.readLine();
• System.out.println("Enter the key:");
• int key =sc.nextInt();
• String encrypted =encrypt(str,key);
• System.out.println("Encrypted String is:"+ encrypted );
• String decrypted =decrypt(encrypted,key);
• System.out.println("Decrypted String is :"+decrypted); }
• public static String encrypt(String str,int key){
• String encrypted ="";
• for(int i=0;i<str.length();i++)
• {
• int c=str.charAt(i);
• if(Character.isUpperCase(c))
• {
• c=c+(key%26);
• if(c>'Z')
• {
• c=c-26;
• }
• else if(Character.isLowerCase(c)){
• c=c+(key%26);
• if(c>'z')
• {
• c=c-26;
• }
• }
• }
• return encrypted;
• }
• import java.util.*;
• import java.io.BufferedReader;
• import java.io.InputStreamReader;
• import java.security.spec.KeySpec;
• import javax.crypto.Cipher;
• import javax.crypto.SecretKey;
• import javax.crypto.SecretKeyFactory;
• import javax.crypto.spec.DESedeKeySpec;
• import sun.misc.BASE64Encoder;
• import sun.misc.BASE64Decoder;
• public class DES{
• private static final String UNICODE_FORMAT = "UTF8";
• public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
• private KeySpec myKeySpec;
• private SecretKeyFactory mySecretKeyFactory;
• private Cipher cipher;
• byte[] keyAsBytes;
• private String myEncryptionKey;
• private String myEncryptionScheme;
• SecretKey key;
• static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
• public DES() throws Exception
• {
• myEncryptionKey= "ThisIsSecretEncryptionKey";
• myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
• keyAsBytes =myEncryptionKey.getBytes(UNICODE_FORMAT);
• myKeySpec= new DESedeKeySpec(keyAsBytes);
• mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
• cipher = Cipher.getInstance(myEncryptionScheme);
• key = mySecretKeyFactory.generateSecret(myKeySpec);
• }
• public String encrypt(String unencryptedString)
• {
• String encryptedString = null;
• try {
• cipher.init(Cipher.ENCRYPT_MODE, key);
• byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
• byte[] encryptedText = cipher.doFinal(plainText);
• BASE64Encoder base64Encoder =new BASE64Encoder();
• encryptedString = base64Encoder.encode(encryptedText);
• }
• catch (Exception e)
• {
• e.printStackTrace();
• }
• return encryptedString;
• }
• public String decrypt(String encryptedString)
• {
• String decryptedText=null;
• try {
• cipher.init(Cipher.DECRYPT_MODE, key);
• BASE64Decoder base64decoder = new BASE64Decoder();
• import java.io.*;
• import java.io.FileInputStream;
• import java.io.FileOutputStream;
• import java.security.Key;
• import javax.crypto.Cipher;
• import javax.crypto.CipherOutputStream;
• import javax.crypto.KeyGenerator;
• import sun.misc.BASE64Encoder;
• public class Blowfish{
• public static void main(String args[])throws Exception{
• KeyGenerator keyGenerator=KeyGenerator.getInstance("Blowfish");
• keyGenerator.init(128);
• Key secretKey=keyGenerator.generateKey();
• Cipher cipherOut=Cipher.getInstance("Blowfish/CFB/Nopadding");
• cipherOut.init(Cipher.ENCRYPT_MODE,secretKey);
• BASE64Encoder encoder=new BASE64Encoder();
• byte iv[]=cipherOut.getIV();
• if(iv!=null){
• System.out.println("initialization vector of the cipher:"+encoder.encode(iv));
• FileInputStream fin=new FileInputStream("inputFile.txt");
• FileOutputStream fout=new FileOutputStream("outputFile.txt");
• CipherOutputStream cout=new CipherOutputStream(fout,cipherOut);
• int input=0;
• while((input=fin.read())!=-1){
• cout.write(input);
• }
• fin.close();
• cout.close();
• }
• }
• 6. Program to implement Rijndael algorithm logic
• AIM: Write a C/ JAVA program to implement the Rijndael algorithm logic.
• PROGRAM:
• import java.security.*;
• import javax.crypto.*;
• import javax.crypto.spec.*;
• import java.io.*;
• public class AES {
• public static String asHex (byte buf[]) { StringBuffer
• strbuf = new StringBuffer(buf.length * 2); int i;
• for (i = 0; i < buf.length; i++) {
• if (((int) buf[i] & 0xff) < 0x10)
• strbuf.append("0");
• strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); }
• return strbuf.toString(); }
• public static void main(String[] args) throws Exception
• { String message="AES still rocks!!";
• / / Get the KeyGenerator
• KeyGenerator kgen = KeyGenerator.getInstance("AES");
• kgen.init(128); / / 192 and 256 bits may not be availab le
• / / Generate the secret key specs. SecretKey
• skey = kgen.generateKey(); byte[] raw =
• skey.getEncoded();
• SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
• / / Instantiate the cipher
• Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE,
• skeySpec);
• byte[] encrypted = cipher.doFinal((args.length == 0 ? message :
AIM: Using Java Cryptography, encrypt the text “Hello world” using BlowFish. Create your own
key using Java keytool.
PROGRAM:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import
javax.swing.JOptionPane; public class BlowFishCipher {
public static void main(String[] args) throws Exception {
/ / create a key generator based upon the Blowfish cipher KeyGeneratorkeygenerator =
KeyGenerator.getInstance("Blowfish");
String inputText = JOptionPane.showInputDialog("Input your message: "); / / encrypt message
byte[] encrypted = cipher.doFinal(inputText.getBytes());
/ / re-initialise the cipher to be in decrypt mode cipher.init(Cipher.DECRYPT_MODE, secretkey);
/ / decrypt message
byte[] decrypted = cipher.doFinal(encrypted);
/ / and display the results
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
"\ nEncrypted text: " + new String(encrypted) + " \ n" + "\ nDecrypted text: " + new
String(decrypted));
System.exit(0);
}}