Cryptography & Network Security Laboratory
Cryptography & Network Security Laboratory
Cryptography & Network Security Laboratory
LABORATORY MANUAL
Regulation : R16
Branch : CSE
Year / Semester : III/ II
Computer Science
&
Engineering
PREPARED BY
Y.KUMAR
Assistant Professor
INDEX
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");
}
Output:
Hello
World
Hello
World
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
]);
}
printf("\n");
}
PROGRAM:
d) Ceaser Cipher
import
java.io.BufferedReader;
import java.io.IOException;
import
java.io.InputStreamReader;
import java.util.Scanner;
public class CeaserCipher {
Output:
Enter any String: Hello
World Enter the Key: 5
Encrypted String is:
MjqqtBtwqi Decrypted String is:
Hello World
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int
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()
{ floatp,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++) {
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:
Enter a 3 letter string:
hai Encrypted string is
:fdx Inverse Matrix is :
0.083333336 0.41666666 -0.33333334
-0.41666666 -0.083333336 0.6666667
0.5833333 -0.083333336 -0.33333334
Decrypted string is: hai
PROGRAM:
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.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public class DES {
private static final String UNICODE_FORMAT = "UTF8";
public static final String DESEDE_ENCRYPTION_SCHEME =
"DESede"; privateKeySpecmyKeySpec;
privateSecretKeyFactorymySecretKeyFactory;
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 {
// TODO code application logic here myEncryptionKey
}
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(); }
returnencryptedStrin
g; }
public String decrypt(String encryptedString)
{ String decryptedText=null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encryptedText =
base64decoder.decodeBuffer(encryptedString); byte[] plainText =
cipher.doFinal(encryptedText); decryptedText=
bytes2String(plainText); }
catch (Exception e)
{
e.printStackTrace()
;}
returndecryptedTe
OUTPUT:
Input your message: Hello
KGRCET Encrypted text:
3ooo&&(*&*4r4 Decrypted text:
Hello KGRCET
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");
// create a key
// create a cipher based upon Blowfish
Cipher cipher =
Cipher.getInstance("Blowfish");
// initialise cipher to with secret key
cipher.init(Cipher.ENCRYPT_MODE,
secretkey);
// get the text to encrypt
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
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) {
// TODO code application logic
here System.out.print("Enter a Prime
number: ");
BigInteger p = sc.nextBigInteger(); // Here's one prime
number.. System.out.print("Enter another prime number:
"); BigInteger q = sc.nextBigInteger(); // ..and another.
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); // Here's the multiplicative inverse
PROGRAM:
import java.math.BigInteger;
import
java.security.KeyFactory;
import java.security.KeyPair;
import
java.security.KeyPairGenerator;
import
java.security.SecureRandom;
import
javax.crypto.spec.DHParameterSpec;
import
javax.crypto.spec.DHPublicKeySpec;
public class DiffeHellman {
public final static int pValue =
47; public final static int gValue
= 71; public final static int
XaValue = 9; public final static
int XbValue = 14;
public static void main(String[] args) throws Exception
{ // TODO code application logic here
BigInteger p = new
BigInteger(Integer.toString(pValue)); BigInteger g =
new BigInteger(Integer.toString(gValue));
BigIntegerXa = new
BigInteger(Integer.toString(XaValue)); BigIntegerXb
= new BigInteger(Integer.toString(XbValue));
input = "abc";
md.update(input.getBytes(
)); output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = " +bytesToHex(output));
input =
"abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output =
md.digest();
System.out.printl
n();
System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output));
input = "abc";
md.update(input.getBytes(
)); output = md.digest();
System.out.println();
System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));
input =
"abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output =
md.digest();
System.out.printl
n();
System.out.println("MD5(\"" +input+"\") = "
OUTPUT:
Message digest object info:
Algorithm = MD5
Provider = SUN version 1.6
ToString = MD5 Message Digest from SUN, <initialized> MD5("") =
D41D8CD98F00B204E9800998ECF8427E MD5("abc") =
900150983CD24FB0D6963F7D28E17F72 MD5("abcdefghijklmnopqrstuvwxyz")
= C3FCD3D76192E4007DFB496CCA67E13B