NSM Lab Final
NSM Lab Final
Page
S.No. DATE Name of the Experiment Signature
No.
AIM:
To implement a program for encrypting a plain text and decrypting a cipher text using
substitution cipher technique.
ALGORITHM:
Step 1: Start the Process
Step 2: Declare the header files and variables
Step 3: Read the input plain text to be encrypted and also the Caeser cipher key an integer
between 0 and 25.
Step 4: Encrypt the plain text using the Caeser cipher key and the ALPHABET string.
Step 5: For every character in the plain text
Step 6: Search the ALPHABET string for the character and assign the numeric representation
of the character (plain numeric) as the index position of the character in the ALPHABET
string.
Step 7: Perform encryption using cipher numeric = (plainnumeric + Caeser cipher key) mod
26.
Step 8: Use cipher numeric as the index position and get the corresponding character from the
ALPHABET string as the equivalent cipher text character for the plain text character.
Step 9: Print the equivalent cipher text
Step 10: Decrypt the cipher text using the Caeser cipher key and the ALPHABET string.
Step 11: For every character in the cipher text
Step 12: Search the ALPHABET string for the character and assign the numeric
representation of the character (ciphernumeric) as the index position of the character in the
ALPHABET string.
PROGRAM:
#include<stdio.h>
int main()
{
char *message,*emessage,*dmessage;
int i,j=0,k,key,temp;
clrscr();
printf("\nEnter the key\n");
scanf("%d",&key);
key=key%26;
printf("\nEnter message\n");
fflush(stdin);
gets(message);
for (i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);
for (i=0;message[i]!=NULL;i++) {
if(message[i]==' ')
emessage[j++]=message[i]; else {
if(message[i]>=48 && message[i]<=57) {
temp=message[i]+key;
if(temp>57)
emessage[j++]=48+(temp-58); else
emessage[j++]=temp;
} else {
if(message[i]>=97 && message[i]<=123) {
temp=message[i]+key;
if(temp>122)
emessage[j++]=97+(temp-123); else
emessage[j++]=temp;
} else
emessage[j++]=message[i];
}
// printf("%c ",emessage[j]);
}
}
emessage[j]='\0';
printf("\n\n\nEncrypted message is\n\n");
for (i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
// printf("\n end");
for (i=0,j=0;emessage[i]!=NULL;i++) {
if(emessage[i]==' ')
dmessage[j++]=emessage[i]; else {
if(emessage[i]>=48 && emessage[i]<=57) {
temp=emessage[i]-key;
if(temp<48)
dmessage[j++]=58-(48-temp); else
dmessage[j++]=temp;
} else {
if(emessage[i]>=97 && emessage[i]<=123) {
temp=emessage[i]-key;
if(temp<97)
dmessage[j++]=123-(97-temp); else
dmessage[j++]=temp;
} else
dmessage[j++]=emessage[i];
}
}
}
dmessage[j]='\0';
printf("\n\n\nRetrieved message is\n\n");
for (i=0;dmessage[i]!=NULL;i++)
printf("%c",dmessage[i]);
getch();
return(0);
}
OUTPUT:
RESULT:
Thus the program to implement substitution encryption technique was developed and
executed.
Ex.No.
TRANSPOSITION CIPHER
:1(a)
AIM:
To implement a program for encrypting a plain text and decrypting a cipher text using
substitution cipher technique.
ALGORITHM:
Step 6: Fill the spaces in the table with the letters of the key without any duplication of
letters.
Step 8: Remove any punctuation or characters from the plain text that are not present in the
key square.
Step 9: Identify any double letters in the plaintext and insert ‘X’ between the two
occurrences.
Step 10: Split the plain text into groups of 3 letters
Step 11: Encryption: is a ‘simple columnar transposition’ cipher where the plaintext is
written horizontally with a certain alphabet width.
Step 12: The cipher text is obtained by reading column vertically downward from first to last
column.
Step 13: Decryption: the receiver prepares similar table.
Step 15: The number of rows is obtained by dividing number of total cipher text alphabets by
key value and rounding of the quotient to next integer value.
Step 16: The receiver then writes the received cipher text vertically down and from left to
right column.
Step 17: To obtain the text, he reads horizontally left to right and from top to bottom row.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char*message,*dmessage;
int i,j,klen,emlen,flag=0;
int r,c,index,min,rows;
clrscr();
printf("\t\t\t TRANSPOSITION CIPHER \n");
printf("Enter the key\n");
fflush(stdin);
gets(key);
printf("\nEnter message to be ciphered\n");
fflush(stdin);
gets(message);
strcpy(temp,key);
klen=strlen(key);
k=0;
for(i=0;;i++)
{
if(flag==1)
break;
for(j=0;key[j]!=NULL;j++)
{
if(message[k]==NULL)
{
flag=1;
arr[i][j]='-';
}
else
{
arr[i][j]=message[k++];
}
}
}
r=i;
c=j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%c",arr[i][j]);
}
printf("\n");
}
k=0;
for(i=0;i<klen;i++)
{
index=findMin();
cipher(index,r);
}
emessage[k]='\0';
printf("\nEncrypted message is\n");
for(i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
printf("\n\n");
emlen=strlen(emessage);
strcpy(temp,key);
rows=emlen/klen;
rows;
j=0;
for(i=0,k=1;emessage[i]!=NULL;i++,k++)
{
temp2[j++]=emessage[i];
if((k%rows)==0)
{
temp2[j]='\0';
index=findMin();
makeArray(index,rows);
j=0;
}
}
printf("\nArray Retrieved is\n");
k=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%c",darr[i][j]);
retmessage[k++]=darr[i][j];
}
printf("\n");
}
retmessage[k]='\0';
printf("\nMessage retrived is\n");
for(i=0;retmessage[i]!=NULL;i++)
printf("%c",retmessage[i]);
getch();
return(0);
}
int findMin()
{
int i,j,min,index;
min=temp[0];
index=0;
for(j=0;temp[j]!=NULL;j++)
{
if(temp[j]<min)
{
min=temp[j];
index=j;
}
}
temp[index]=123;
return(index);
}
OUTPUT:
RESULT:
Thus the program to implement transposition encryption technique was developed and
executed.
Ex.No. : 2 DATA ENCRYPTION STANDARD (DES)
AIM:
To develop a program to implement Data Encryption Standard for encryption and
decryption.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i,ch,lp;
char cipher[50],plain[50];
char key[50];
clrscr();
while(1)
{
printf(" \n-----MENU ---- ");
printf("\n1:Data Encryption\t2:DataDecryption\t3:Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nData Encryption");
printf("\nEnter the plain text:");
fflush(stdin);
gets(plain);
printf("\nEnter the encryption key:");
gets(key);
lp=strlen(key);
for(i=0;plain[i]!='\0';i++)
cipher[i]=plain[i]^lp;
cipher[i]='\0';
printf("\nThe encrypted textis:");
puts(cipher);
break;
case 2:
printf("\nData decryption");
for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp;
printf("\nDecrypted text is:");
puts(plain);
break;
case 3:
exit(0);
}
getch();
}
}
OUTPUT:
RESULT:
Thus the program to implement transposition encryption technique was developed and
executed.
Ex.No. : 3 Public Key Cryptography using Diffie –Hellman Algorithm
AIM:
To Develop a program to implement Diffie Hellman Key Exchange Algorithm for
encryption and Decryption.
ALGORITHM:
import java.util.*;
import java.math.BigInteger;
OUTPUT:
RESULT:
Thus the program to implement Diffie-Hellman Key Exchange algorithm was
developed and executed successfully
AIM:
To develop a program to implement RSA algorithm for encryption and decryption.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
void main()
{
int a,b,i,t,j,x,n,k=0,flag=0,prime[100];
char m[20],pp[20];
float p[20],c[20];
double e,d;
clrscr();
for(i=0;i<50;i++)
{
flag=0;
for(j=2;j<i/2;j++)
if(i%j==0);
{
flag=0;
break;
}
if(flag==0)
prime[k++]=i;
}
a=prime[k-1];
b=prime[k-2];
n=a*b;
t=(a-1)*(b-1);
e=(double)prime[2];
d=1/(float)e;
printf("\nKey of encryption is%if",d);
printf("\nEnter the text:");
scanf("%s",&m);
printf("\n\tSource\t\t\t\tDestination");
printf("\nChar Numeric\tCipher\tNumeric\tChar\n");
for(i=0;i<x;i++)
{
printf("%c",m[i]);
printf("\t%d",m[i]-97);
c[i]=pow(m[i]-97,e);
c[i]=fmod(c[i],n);
printf("\t%f",c[i]);
p[i]=pow(c[i],d);
p[i]=fmod(p[i],n);
printf("\t %f",p[i]);
pp[i]=p[i]+97;
printf("\t%c\n",pp[i]);
}
getch();
}
OUTPUT:
RESULT:
Thus the program for implementation of RSA algorithm was executed and verified
successfully.
Ex.No. :5 Secure the Database using User Authentication Security
AIM:
To secure database using User Authentication Security
ALGORITHM:
Step 5: Create a JPanel as panel, JButton as blogin, JTextField as txuser and JPasswordField
as pass.
Step 6: Add the Button, TextField to panel and Panel to the frame using
getContentPane().add(panel).
Step 7: In ActionLogin() method, Get the text from user and check the given text with
username of “text” and password of “12345” using if condition.
Step 8: If the username and password is correct the new frame will be display.
Step 9: Otherwise, the message dialog box will show “Wrong Password / Username”.
PROGRAM:
mport java.awt.event.*;
import javax.swing.*;
public class Log extends JFrame
{
public static void main(String[] args)
{
Log frameTabel = new Log();
}
Log()
{
super("Login Autentification");
setSize(300,200);
setLocation(500,280);
pane.setLayout (null);
txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
blogin.setBounds(110,100,80,20);
pane.add(blogin);
pane.add(txuser);
pane.add(pass);
getContentPane().add(pane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
AIM:
To implement server security program for dynamic Page Generation.
ALGORITHM:
Step 2: Import the swing package javax.swing and AWT package awt.event.*;
Step5: In newfrane() method, set the size of the from and location.
setSize(300,200);
setLocation(500,280);
Step 6: Set the Bounding size of Label welcome.setBounds(70,50,150,60); and add to panel.
Step 7: Add the panel to the frame using getContentPane ().add (panel); and set the deafault
close operation for frame
Step 8: Enable the visibility of frame using setVisible(True).
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
newframe()
{
super("Welcome");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null);
welcome.setBounds(70,50,150,60);
panel.add(welcome);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
OUTPUT:
RESULT:
Thus the program for implementation of Server Security for Dynamic Page Generation was
executed and verified successfully.