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>
#include<string.h>
#include<stdio.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”);
}
OUTPUT:
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<stdlib.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="Hello World";
int i,len;
len=strlen(str);
printf("AND:");
for(i=0;i<len;i++)
{
printf("%c",str[i]&127);
}
printf(“\n”);
printf("XOR:");
for(i=0;i<len;i++)
{
printf("%c",str[i]^127);
}
printf(“\nnOR”);
for(i=0;i<len;i++)
{
printf("%c",str[i]|127);
}
printf(“\n”);
}
OUTPUT:
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
PROGRAM:
a) Ceaser Cipher
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.lang.*;
public class CeaserCipher
{
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.print("Enter any String: "); String str = br.readLine();
System.out.print("\nEnter the Key: ");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
System.out.println("\nEncrypted String is: " +encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("\nDecrypted String is: " +decrypted);
System.out.println("\n");
}
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;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{
String decrypted = "";
for(int i = 0; i < str.length(); i++)
{
int c = str.charAt(i);
if (Character.isUpperCase(c))
{
c = c - (key % 26);
if (c < 'A')
c = c + 26;
}
else if (Character.isLowerCase(c))
{
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
OUTPUT:
b) Substitution Cipher
PROGRAM:
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.print("Enter any string: ");
String str = br.readLine();
String decrypt = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encrypted data is: " +decrypt);
}
}
OUTPUT:
c)Hill Cipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*;
public class HillCipher
{
static float[][] decrypt = new float[3][1];
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++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++)
{
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("\nDecrypted 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 3x3 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("\nEnter 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++)
{
//a[i][j]=sc.nextFloat();
if(i==j)
b[i][j]=1;
else b[i][j]=0;
}
for(int k=0;k<3;k++)
{
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("\nInverse 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");
}
}
}
OUTPUT:
4.Java program for DES Algorithm logic
AIM: Write a Java program to implement the DES algorithm logic.
PROGRAM:
import javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random;
class DES
{
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public DES()
{
try
{
generateSymmetricKey();
inputMessage=JOptionPane.showInputDialog(null,"Enter message to
encrypt");
byte[] ibyte = inputMessage.getBytes();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
System.out.println("Encrypted message "+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data
"+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
System.out.println("Decrypted message "+decryptedMessage);
JOptionPane.showMessageDialog(null,"Decrypted Data
"+"\n"+decryptedMessage);
}
catch(Exception e)
{
System.out.println(e);
}
}
void generateSymmetricKey()
{
try
{
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
}
catch(Exception e)
{
System.out.println(e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(56, sr);
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static void main(String args[])
{
DES des = new DES();
}
}
OUTPUT:
5. Program to implement BlowFish algorithm logic
AIM: Write a C/JAVA program to implement the BlowFish algorithm logic.
PROGRAM:
import javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;
class Blowfish
{
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public Blowfish()
{
try
{
generateSymmetricKey();
inputMessage=JOptionPane.showInputDialog(null,"Enter message to
encrypt");
byte[] ibyte = inputMessage.getBytes();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
System.out.println("Encrypted message "+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data
"+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
System.out.println("Decrypted message "+decryptedMessage);
JOptionPane.showMessageDialog(null,"Decrypted Data
"+"\n"+decryptedMessage);
}
catch(Exception e)
{
System.out.println(e);
}
}
void generateSymmetricKey()
{
try
{
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("Blowfish Symmetric key = "+skeyString);
}
catch(Exception e)
{
System.out.println(e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static void main(String args[])
{
Blowfish bf = new Blowfish();
}
}
OUTPUT:
6. Program to implement Rijndael algorithm logic
AIM: Write a C/JAVA program to implement the Rijndael algorithm logic.
PROGRAM:
import javax.crypto.*;
import javax.crypto.spec.*;
class AES
{
private 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!";
KeyGenerator kgen= KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec= new SecretKeySpec(raw,"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte[] encrypted= cipher.doFinal((args.length==0? message : args[0]).getBytes());
System.out.println("Encrypted String: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[]original=cipher.doFinal(encrypted);
String originalString=new String(original);
System.out.println("Original String in Hexadecimal: " + asHex(original));
System.out.println("Original String: " + originalString);
}
}
OUTPUT:
7. Encrypt a string using BlowFish algorithm
AIM: Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello world” using
Blowfish. Create your own key using Java key tool
PROGRAM:
import javax.crypto.*;
public class RC4
{
public static void main(String[] args) throws Exception
{
KeyGenerator kg=KeyGenerator.getInstance("RC4");
SecretKey skey=kg.generateKey();
Cipher c=Cipher.getInstance("RC4");
c.init(Cipher.ENCRYPT_MODE,skey);
String inputText="Hello World";
byte[] encrypted=c.doFinal(inputText.getBytes());
c.init(Cipher.DECRYPT_MODE,skey);
byte[] decrypted=c.doFinal(encrypted);
System.out.println("Original String: "+inputText);
System.out.println("Encrypted: "+new String(encrypted));
System.out.println("Decrypted: "+new String(decrypted));
}
}
OUTPUT:
8. RSA Algorithm
AIM: Write a Java program to implement RSA Algorithm.
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
public class RSA
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("Enter a Prime number: ");
BigInteger p = sc.nextBigInteger();
System.out.print("Enter another prime number: ");
BigInteger q = sc.nextBigInteger();
BigInteger n = p.multiply(q);
BigInteger n2 = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e = generateE(n2);
BigInteger d = e.modInverse(n2);
System.out.println("Encryption keys are: " + e + ", " + n);
System.out.println("Decryption keys are: " + d + ", " + n);
}
public static BigInteger generateE(BigInteger fiofn)
{
int y, intGCD;
BigInteger e;
BigInteger gcd;
Random x = new Random();
do
{
y = x.nextInt(fiofn.intValue()-1);
String z = Integer.toString(y);
e = new BigInteger(z);
gcd = fiofn.gcd(e);
intGCD = gcd.intValue();
}
while(y <= 2 || intGCD != 1);
return e;
}
}
OUTPUT:
9. Diffie-Hellman
AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript.
PROGRAM:
<script>
function power(a,b,p)
{
if (b == 1)
return a;
else
return((Math.pow(a, b)) % p);
}
var P=21, G=9, x, a=4, y, b=3, ka, kb;
document.write("The value of P:" + P + "<br>");
document.write("The value of G:" + G + "<br>");
document.write("The private key a for Alice:" +a + "<br>");
document.write("The private key b for Bob:" + b + "<br>");
x = power(G, a, P);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
document.write("Secret key for the Alice is:" +ka + "<br>");
document.write("Secret key for the Bob is:" +kb + "<br>");
</script>
OUTPUT:
10. SHA-1
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import java.math.BigInteger;
import java.security.MessageDigest;
public class SHA1
{
public static void main(String args[]) throws Exception
{
String input = "Hello World";
try
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
System.out.println("MessageDigest Object Info");
System.out.println("Message: "+md.getAlgorithm());
System.out.println("Provider: "+md.getProvider());
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32)
{
hashtext = "0" + hashtext;
}
System.out.println("HashCode Generated");
System.out.println(input+" : "+hashtext);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
OUTPUT:
11. Message Digest Algorithm5 (MD5)
AIM: Calculate the message digest of a text using the MD-5 algorithm in JAVA.
PROGRAM:
import java.math.BigInteger;
import java.security.MessageDigest;
public class MD5
{
public static void main(String args[]) throws Exception
{
String input = "Hello World";
try
{
MessageDigest md = MessageDigest.getInstance("MD5");
System.out.println("MessageDigest Object Info");
System.out.println("Message: "+md.getAlgorithm());
System.out.println("Provider: "+md.getProvider());
byte[] messageDigest = md.digest(input.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32)
{
hashtext = "0" + hashtext;
}
System.out.println("HashCode Generated");
System.out.println(input+" : "+hashtext);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
OUTPUT: