BCS3752 Network Security Lab
BCS3752 Network Security Lab
BCS3752 Network Security Lab
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char text[100], key;
printf("Enter the text: ");
fgets(text, sizeof(text), stdin);
text[strlen(text) - 1] = '\0'; // remove newline character
printf("Enter the key (a single character): ");
scanf("%c", &key);
getchar(); // consume newline character
monoalphabetic_encrypt(text, key);
printf("Encrypted text: %s\n", text);
monoalphabetic_decrypt(text, key);
printf("Decrypted text: %s\n", text);
return 0;
}
Output :
Enter the text: 23
Enter the key (a single character): 2
Encrypted text: 23
Decrypted text: 23
PROGRAM 2
Implementation of Play Fair cipher in Java.
import java.util.Scanner;
generateMatrix(key);
int index = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
matrix[i][j] = uniqueChars.charAt(index++);
}
}
}
Output :-
Enter the key: 23
Enter the plaintext: text
Encrypted Text: UDYS
Decrypted Text: TEXT
PROGRAM 3
Implementation of Vigenere cipher (Polyalphabetic substitution)
in C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
dest = strdup(src);
upper_case(dest);
upper_case(key);
klen = strlen(key);
for (i = 0; i < slen; i++) {
if (!isupper(dest[i]))
continue;
dest[i] = 'A' + (is_encode ? dest[i] - 'A' + key[i % klen] - 'A'
: dest[i] - key[i % klen] + 26) % 26;
}
return dest;
}
int main() {
const char *str = "Beware the Jabberwock, my son! The jaws that bite, "
"the claws that catch!";
const char *cod, *dec;
char key[] = "VIGENERECIPHER";
OUTPUT:-
Text: Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
key: VIGENERECIPHER
Code:
WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEV
KPAGY
Back:
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHAT
CATCH
PROGRAM 4
Implementation of Hill cipher in Java.
class hillCipher {
/* 3x3 key matrix for 3 characters at once */
public static int[][] keymat = new int[][] { { 1, 2, 1 }, { 2, 3, 2 },
{ 2, 2, 1 } }; /* key inverse matrix */
public static int[][] invkeymat = new int[][] { { -1, 0, 1 }, { 2, -1, 0 }, { -2, 2, -1
} };
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String encode(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0];
y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1];
z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2];
a = key.charAt(x % 26);
b = key.charAt(y % 26);
c = key.charAt(z % 26);
ret = "" + a + b + c;
return ret;
}
private static String decode(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * invkeymat[0][0] + posb * invkeymat[1][0] + posc *
invkeymat[2][0];
y = posa * invkeymat[0][1] + posb * invkeymat[1][1] + posc *
invkeymat[2][1];
z = posa * invkeymat[0][2] + posb * invkeymat[1][2] + posc *
invkeymat[2][2];
a = key.charAt((x % 26 < 0) ? (26 + x % 26) : (x % 26));
b = key.charAt((y % 26 < 0) ? (26 + y % 26) : (y % 26));
c = key.charAt((z % 26 < 0) ? (26 + z % 26) : (z % 26));
ret = "" + a + b + c;
return ret;
}
public static void main(String[] args) throws java.lang.Exception {
String msg;
String enc = "";
String dec = "";
int n;
msg = ("SecurityLaboratory");
System.out.println("simulation of Hill Cipher\n-------------------------");
System.out.println("Input message : " + msg);
msg = msg.toUpperCase();
msg = msg.replaceAll("\\s", "");
/* remove spaces */ n = msg.length() % 3;
/* append padding text X */ if (n != 0) {
for (int i = 1; i <= (3 - n); i++) {
msg += 'X';
}
}
System.out.println("padded message : " + msg);
char[] pdchars = msg.toCharArray();
for (int i = 0; i < msg.length(); i += 3) {
enc += encode(pdchars[i], pdchars[i + 1], pdchars[i + 2]);
}
System.out.println("encoded message : " + enc);
char[] dechars = enc.toCharArray();
for (int i = 0; i < enc.length(); i += 3) {
dec += decode(dechars[i], dechars[i + 1], dechars[i + 2]);
}
System.out.println("decoded message : " + dec);
}
}
Output:
simulation of Hill Cipher
-------------------------
Input message : SecurityLaboratory
padded message : SECURITYLABORATORY
encoded message : EACSDKLCAEFQDUKSXU
decoded message : SECURITYLABORATORY
PROGRAM 5
Implementation of Gauss cipher.
OUTPUT :-
Ciphertext: armmv
Decrypted text: hello
PROGRAM 6
Implementation of Rail Fence cipher In C.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i,j,len,rails,count,code[100][1000];
char str[1000];
printf("Enter a Secret Message\n");
gets(str);
len=strlen(str);
printf("Enter number of rails\n");
scanf("%d",&rails);
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
code[i][j]=0;
}
}
count=0;
j=0;
while(j<len)
{
if(count%2==0)
{
for(i=0;i<rails;i++)
{
//strcpy(code[i][j],str[j]);
code[i][j]=(int)str[j];
j++;
}
}
else
{
for(i=rails-2;i>0;i--)
{
code[i][j]=(int)str[j];
j++;
}
}
count++;
}
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
if(code[i][j]!=0)
printf("%c",code[i][j]);
}
}
printf("\n");
}
OUTPUT:
Enter a Secret Message
34
Enter number of rails
25
34
PROGRAM 7
Implementation of S-DES algorithm for data encryption in
JAVA
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
class DES{
public static void main(String[] args) throws IOException, NoSuchAlgorithmException,
InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
//Generating Key
KeyGenerator Mygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = Mygenerator.generateKey();
OUTPUT:-
Message : This is a confidential message.
Encrypted - P????!????P?sWE??'~?
ecrypted Message - This is a confidential message.
PROGRAM 8
Implement RSA asymmetric (public key and private key)-
Encryption. Encryption key (e, n) & (d,n)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
printf("ENTER FIRST PRIME NUMBER: ");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0 || p == 1)
{
printf("WRONG INPUT\n");
exit(1);
}
n = p * q;
t = (p - 1) * (q - 1);
ce();
encrypt();
decrypt();
return 0;
}
void ce()
{
int k;
k = 0;
for (i = 2; i < t; i++)
{
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q)
{
e[k] = i;
flag = cd(e[k]);
if (flag > 0)
{
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while (i < len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for (j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\nTHE ENCRYPTED MESSAGE IS:\n");
for (i = 0; en[i] != -1; i++)
printf("%c", (char)en[i]);
}
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1)
{
ct = temp[i];
k = 1;
for (j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS:\n");
for (i = 0; m[i] != -1; i++)
printf("%c", (char)m[i]);
}
OUTPUT:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isPrime(int n)
{
int i;
for (i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
return -1;
}
/* Encryption
* a procedure that requires the message, the public key, and an integer n w
hich is the sum of p and q. Using the public key, the function encrypts the me
ssage and then returns the result.
*/
/* Decryption
* a procedure that requires the cypher text, the private key, and an integer
n, which is the sum of the values of p and q. The function uses the private ke
y to decode the cypher text and then returns the message that has been dec
rypted.
*/
int main()
{
int p, q, lambda_n;
long n, e, d;
char *message;
char *cipher;
printf("\nEnter the value of p: ");
scanf("%d", &p);
printf("\nEnter the value of q: ");
scanf("%d", &q);
if (isPrime(p) && isPrime(q))
{
n = p * q;
lambda_n = totient(p, q);
e = randome(lambda_n);
d = private_key(e, lambda_n);
printf("\nThe value of n is %ld", n);
printf("\nThe value of lambda_n is %d", lambda_n);
printf("\nThe value of e is %ld", e);
printf("\nThe value of d is %ld", d);
printf("\nEnter the message: ");
message = (char *)malloc(sizeof(char) * 100);
scanf("%s", message);
cipher = encrypt(message, e, n);
puts("\nThe encrypted message is: ");
printf("%s", cipher);
message = decrypt(cipher, d, n);
puts("\nThe original message was: ");
printf("%s", message);
}
else
{
printf("\nThe value of p and q should be prime.");
}
return 0;
}
Output:
, -> ,
, -> ,
->
-> -
& ->&
-> .
The original message was:
, -&.
PROGRAM 9
Generate digital signature using Hash code in C.
/ DSA
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int p,q,g,x,hm,k,y,r,s,s1,w,u1,u2,v,v1,v2,v3;
clrscr();
printf("enter the value of p:");
scanf("%ld",&p);
printf("enter the value of q:");
scanf("%ld",&q);
printf("enter the value of g:");
scanf("%ld",&g);
printf("enter the value of x:");
scanf("%ld",&x);
printf("enter the value of hm:");
scanf("%ld",&hm);
printf("enter the value of k:");
scanf("%ld",&k);
y=power(g,x,p);
printf("\nvalue of y:%ld",y);
r=power(g,k,p);
r=r%q;
printf("\nvalue of r:%ld",r);
s=ext_eucledian(q,k);
s1=(hm+(x*r));
s=(s*s1)%q;
printf("\nvalue of s:%ld",s);
w=ext_eucledian(q,s);
printf("\nsignature (r,s):%ld %ld",r,s);
printf("\nvalue of w:%ld",w);
u1=(hm*w)%q;
printf("\nvalue of u1:%ld",u1);
u2=(r*w)%q;
printf("\nvalue of u2:%ld",u2);
v=power(g,u1,p);
v1=power(y,u2,p);
v2=(v*v1)%p;
v3=v2%q;
printf("\nvalue of v:%ld",v3);
getch();
}
OUTPUT:
Enter the value of p:283
Enter the value of q : 47
Enter the value of g : 60
Enter the value of x : 24
Enter the value of hm : 41
Enter the value of k : 15
Value of y : 158
Value of r :19
Value of s : 30
Signature (r, s): 19 30
Value of w : 11
Value of u1 :28
Value of u2 : 21
Value of v : 19
PROGRAM 10
Generate digital signature using MAC code.
package java_cryptography;
// Imports
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.util.Scanner;
import javax.xml.bind.DatatypeConverter;
// Signing Algorithm
private static final String
SIGNING_ALGORITHM
= "SHA256withRSA";
private static final String RSA = "RSA";
private static Scanner sc;
// Driver Code
public static void main(String args[])
throws Exception
{
String input
= "GEEKSFORGEEKS IS A"
+ " COMPUTER SCIENCE PORTAL";
KeyPair keyPair
= Generate_RSA_KeyPair();
// Function Call
byte[] signature
= Create_Digital_Signature(
input.getBytes(),
keyPair.getPrivate());
System.out.println(
"Signature Value:\n "
+ DatatypeConverter
.printHexBinary(signature));
System.out.println(
"Verification: "
+ Verify_Digital_Signature(
input.getBytes(),
signature, keyPair.getPublic()));
}
}
OUTPUT: