Cryptography and Network Security Lab
Cryptography and Network Security Lab
: 1a CAESAR CIPHER
Date :
AIM:
ALGORITHM:
3. To encrypt a plaintext letter, the first set of plaintext letters and slides it to LEFT by
4. The plaintext letter is then encrypted to the ciphertext letter on the sliding
ruler Underneath.
5. On receiving the ciphertext, the receiver who also knows the secret shift, positions
his Sliding ruler underneath the ciphertext alphabet and slides it to RIGHT by the agreed shift
6. Then replaces the ciphertext letter by the plaintext letter on the sliding ruler underneath.
PROGRAM:
Import java.util.Scanner;
plainText=plainText.toLowerCase();
String cipherText=””;
For (int i=0; i<plainText.length();i++)
Int charPosition=ALPHABET.indexOf(plainText.charAt(i));
Int keyVal=(shiftKey+charPosition)%26;
Char replaceVal=ALPHABET.charAt(keyVal);
cipherText+=replaceVal;
Return cipherText;
cipherText = cipherText.toLowerCase();
For(int i=0;i<cipherText.length();i++)
Int keyVal=(charPosition-shiftKey)%26;
If (keyVal< 0)
keyVal=ALPHABET.length()+keyVal;
Char replaceVal=ALPHABET.charAt(keyVal);
plainText+=replaceVal;
return plainText;
}
Message=sc.next();
Message,3),3));
Sc.close();
OUTPUT:
F:\bin>javac ceasercipher.java
F:\bin>java ceasercipher
Covid
Encrypted message:Cipher
Text=covid RESULT:
Thus the Caesar cipher substitution technique was implemented and executed successfully
Ex.No. : 2 PLAYFAIR CIPHER
Date :
AIM:
ALGORITHM:
5. Then the plaintext message is split into pairs of two letters (digraphs).
6. Ifboth the letters are in the same column, take the letter below each one.
7. Ifboth letters are in the same row, take the letter to the right of each one.
8. If neither of the preceding two rules are true, form a rectangle with the two letters and
PROGRAM:
Import java.util.Scanner;
System.out.print(“Enter keyword:
pfEncryption.makeArray(key);
msg=pfEncryption.manageMessage(msg);
pfEncryption.doPlayFair(msg, “Encrypt”);
String en=pfEncryption.getEncrypted();
System.out.println(“=============================”);
pfEncryption.doPlayFair(en, “Decrypt”);
pfEncryption.getDecrypted());
Class PFEncryption
char[26];
Keyword=keyword.toUpperCase().replace(“J”,”I”);
Int val=0;
Int uniqueLen;
Present=false;
uniqueLen=0;
if (keyword.charAt(i)!= ‘ ‘)
If (Character.toString(uniqueChar[k])==null)
Break;
uniqueLen++;
If (keyword.charAt(i)==uniqueChar[j])
{
Present=true;
If (!present)
uniqueChar[val]=keyword.charAt(i);
val++;
Ch=ch.replaceAll(Character.toString(keyword.charAt(i)), “”);
Val=0;
uniqueChar[val]=ch.charAt(i);
val++;
Alphabets[i][j]=uniqueChar[val];
Val++;
System.out.print(alphabets[i][j] + “\t”);
System.out.println();
Int val=0;
Int len=msg.length()-2;
String newTxt=””;
String intermediate=””;
While (len>=0)
Intermediate=msg.substring(val, val+2);
If (intermediate.charAt(0)==intermediate.charAt(1))
msg=msg.replaceFirst(intermediate, newTxt);
len++;
Len-=2;
Val+=2;
If (msg.length()%2!=0)
{
Msg=msg+’x’;
Int val=0;
While (val<msg.length())
searchAndEncryptOrDecrypt(msg.substring(val,val+2),tag);
val+=2;
}}
Char ch1=doubblyCh.charAt(0);
Char ch2=doubblyCh.charAt(1);
If (alphabets[i][j]==ch1)
{
Row1=I;
Col1=j;
Else if (alphabets[i][j]==ch2)
{ Row2=
I;
Col2=j;
}}}
If (tag==”Encrypt”)
If (row1==row2)
Col1=col1+1;
Col2=col2+1;
If (col1>4)
Col1=0;
If (col2>4)
Col2=0;
Encrypted+=(Character.toString(alphabets[row1][col1])+
Character.toString(alphabets[row1][col2]));
Else if(col1==col2)
Row1=row1+1;
Row2=row2+1;
If (row1>4)
Row1=0;
If (row2>4)
Row2=0;
Encrypted+=(Character.toString(alphabets[row1][col1])+
Character.toString(alphabets[row2][col1]));
Else
{ encrypted+=(Character.toString(alphabets[row1][col2])+
Character.toString(alphabets[row2][col1]));
}}
If (row1==row2)
Col1=col1-1;
Col2=col2-1;
If (col1<0)
Col1=4;
If (col2<0)
Col2=4;
Decrypted+=(Character.toString(alphabets[row1][col1])+
Character.toString(alphabets[row1][col2]));
Else if(col1==col2)
Row1=row1-1;
Row2=row2-1;
If (row1<0)
Row1=4;
If (row2<0)
Row2=4;
Decrypted+=(Character.toString(alphabets[row1][col1])+
Character.toString(alphabets[row2][col1]));
Else
Decrypted+=(Character.toString(alphabets[row1][col2])+
Character.toString(alphabets[row2][col1]));
}}
String getEncrypted( )
{
Return encrypted;
String getDecrypted( )
Return decrypted;
}}
OUTPUT:
F:\bin>javac Playfair1.java
F:\bin>java Playfair1
cryptography I N F O S
ECABD
GHKLM
PQRTU
VWXYZ
Encrypting….
Decrypting….
CRYPTOGRAPHY RESULT:
Thus the Playfair cipher substitution technique was implemented and executed successfully
Ex.No. : 3 DATA ENCRYPTION STANDARD (DES)
Date :
AIM:
To apply Data Encryption Standard (DES) Algorithm for a practical application like
ALGORITHM:
2. Create a Cipher instance from Cipher class, specify the following information
Algorithm name
Mode (optional)
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
String skeystring;
String inputmessage,encryptedata,decryptedmessage;
Public DES()
Try
Generatesymmetrickey();
Inputmessage=JOptionPane.showInputDialog(null,”Enter message to
Encrypt:”);
System.out.println(“Encrypted message:”+encrypteddata);
JOptionPane.showMessageDialog(null,”Encrypted
Data”+”\n”+encrypteddata);
Byte[] dbyte=decrypt(raw,ebyte);
System.out.println(“Decrypted message:”+decryptedmessage);
OptionPane.showMessageDialog(null,”Decrypted Data
“+”\n”+decryptedmessage);
Catch(Exception e)
System.out.println€;
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
SymmerticKey=”+skeystring);
Catch(Exception e)
System.out.println€;
}
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;
Cipher.init(Cipher.ENCRYPT_MODE,seckey);
Byte[] encrypted=cipher.doFinal(clear);
Return encrypted;
Cipher.init(Cipher.DECRYPT_MODE,seckey);
Byte[] decrypted =
OUTPUT:
RESULT:
Thus the java program for applying Data Encryption Standard (DES) Algorithm for a Practical application
of User Message Encryption is written and executed successfully.
Ex.No. : 4 AES ALGORITHM
Date :
AIM:
ALGORITHM:
2. AES does not use a Feistel network like DES, it uses variant of Rijndael.
3. It has a fixed block size of128 bits, and a key size of 128, 192, or 256 bits.
PROGRAM:
Import java.io.UnsupportedEncodingException;
Import java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Import java.util.Arrays;
Import java.util.Base64;
Import javax.crypto.Cipher;
Import javax.crypto.spec.SecretKeySpec;
Try {
Key = myKey.getBytes(“UTF-8”);
Sha = MessageDigest.getInstance(“SHA-
} catch (NoSuchAlgorithmException e)
{ e.printStackTrace();
} catch (UnsupportedEncodingException e)
{ e.printStackTrace();
{ Try {
setKey(secret);
Cipher.init(Cipher.ENCRYPT_MODE, secretKey);
Return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes
(“UTF-8”)));
} catch (Exception e) {
{ Try {
setKey(secret);
Cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (Exception e) {
Return null;
secretKey);
System.out.println(“Decrypted URL : “ +
decryptedString);
}
}
OUTPUT:
annaUniversity
www.annauniv.edu
RESULT:
Thus the java program for applying Advanced EncryptionStandard (AES) Algorithm
Date :
AIM:
C=t
E mod n
T=c
D mod n
PROGRAM:
Rsa.html
<html>
<head>
<title>RSA Encryption</title>
</head>
<body>
<center>
<h1>RSA Algorithm</h1>
<h2>Implemented Using HTML & Javascript</h2>
<hr>
<table>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td>Public Key:</td>
</tr>
<tr>
<td>Exponent:</td>
<tr>
<td>Private Key:</td>
<td><p id=”privatekey”></p></td>
</tr>
<tr>
<td>Cipher Text:</td>
</tr>
<tr>
</tr>
</table> </center>
</body>
<script type=”text/javascript”>
Function RSA()
P = document.getElementById(‘p’).value;
Q = document.getElementById(‘q’).value;
No = document.getElementById(‘msg’).value;
N = p * q;
T = (p – 1) * (q – 1);
If (gcd(e, t) == 1)
{
Break;
X=1+I*t
If (x % e == 0)
D = x / e;
Break;
Ctt = Math.pow(no,
e).toFixed(0); Ct = ctt % n;
Dtt = Math.pow(ct,
d).toFixed(0); Dt = dtt % n;
Document.getElementById(‘publickey’).innerHTML = n;
Document.getElementById(‘exponent’).innerHTML = e;
Document.getElementById(‘privatekey’).innerHTML = d;
Document.getElementById(‘ciphertext’).innerHTML = ct;
</script>
</html>
OUTPUT:
RESULT:
Thus the RSA algorithm was implemented using HTML and Javascript and executed successfully
Date :
AIM:
algorithm. ALGORITHM:
1. Sender and receiver publicly agree to use a modulus p and base g which is a
g Y mod p
4. Sender computes k1 =
B X mod p
5. Receiver computes k2 =
A Y mod p
PROGRAM:
Import java.io.*;
Import java.math.BigInteger;
Class dh
BigInteger R1=g.modPow(x,p);
System.out.println(“R1=”+R1);
System.out.print(“Enter value for y less than
BigInteger R2=g.modPow(y,p);
System.out.println(“R2=”+R2);
BigInteger k1=R2.modPow(x,p);
OUTPUT
dh
11
11: 3
R1=2
11:6 R2=4
Key calculated at
Receiver’sside:9
Thus the Diffie-Hellman key exchange algorithm was implemented and executed successfully
Date :
AIM:
To calculate the message digest of a text using the SHA-1 algorithm in Java.
ALGORITHM:
5. Initialize Buffers.
PROGRAM:
Import java.util.Scanner;
Import java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
Public class sha1
Message = sc.next();
System.out.println(sha1(message));
MessageDigest mDigest =
mDigest.digest(input.getBytes());
StringBuffer sb = new
StringBuffer(); For(int I =
0;i<result.length;i++)
Return sb.toString();
OUTPUT:
C:\Security Lab New\programs>java
7690b7ccb987f4b3f32d2b9e7e8a69db2d0ded02
RESULT:
Thus the Secure Hash Algorithm (SHA-1) has been implemented and executed successfully
Date :
AIM:
ALGORITHM:
6. The hash code is provided as input to a signature function along with a random
Number K generated for the particular signature.
function. PROGRAM:
Import java.util.*;
Import java.math.BigInteger;
Class dsaAlg {
getNextPrime(String ans)
BigInteger(ans); While (!
test.isProbablePrime(99))
E:
Test = test.add(one);
Return test;
BigInteger(“2”); While (!
n.isProbablePrime(99))
While (!((n.mod(start)).equals(zero)))
Start = start.add(one);
N = n.divide(start);
Return n;
q, Random r)
r); H = h.mod(p);
throws Java.lang.Exception
BigInteger q =
findQ(p.subtract(one)); BigInteger g =
getGen(p,q,randObj);
System.out.println(“\np is: “ +
“ + g);
randObj); X = x.mod(q);
BigInteger y= g.modPow(x,p);
randObj); K = k.mod(q);
BigInteger r = (g.modPow(k,p)).mod(q);
BigInteger(p.bitLength(), randObj);
BigInteger s = kInv.multiply(hashVal.add(x.multiply®));
S = s.mod(q);
+ y);
System.out.println(“h (rndhash) is: “ + hashVal);
System.out.println(“\n generating digital signature:\
System.out.println(“s is : “ + s);
BigInteger w =
s.modInverse(q);
BigInteger u1 =
(hashVal.multiply(w)).mod(q); BigInteger u2
= (r.multiply(w)).mod(q);
BigInteger v= (g.modPow(u1,p)).multiply(y.modPow(u2,p));
V = (v.mod(p)).mod(q);
System.out.println(“w is : “ + w);
System.out.println(“u1 is : “ +
u1); System.out.println(“u2 is : “
+ u2); System.out.println(“v is : “
+ v);
If (v.equals®)
{
Else
}
OUTPUT:
C:\Security Lab New\programs>javac
dsaAlg
are:
P is: 10601
Q is: 53
G is: 6089
X(private) is:6
K (secret) is: 3
Generating digitalsignature:
R is : 2
S is :41
W is : 22
U1 is : 4
U2 is :
44
V is : 2
verified! 2
RESULT:
Thus the Digital Signature Standard Signature Scheme has been implemented and executed successfully