0% found this document useful (0 votes)
23 views1 page

Document

The document contains code snippets for algorithms and programs related to IP address classification, password strength checking, Caesar cipher encryption/decryption, Diffie-Hellman key exchange, and generating pseudo-random numbers in a range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Document

The document contains code snippets for algorithms and programs related to IP address classification, password strength checking, Caesar cipher encryption/decryption, Diffie-Hellman key exchange, and generating pseudo-random numbers in a range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

12.

Write a Programto identify the category of IP address for a given IP address }


#include <stdio.h> if(count>=8&&count<=25)
#include <string.h> leng++;
void extractIpAddress(unsigned char *sourceString,short *ipAddress) if(symbol>=1&&dig>=2&&upper>=1&&lower>=1&&leng>0)
{ printf(“VALID―);
unsigned short len=0; else
unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5]; printf(“INVALID―);
len=strlen(sourceString); 16.Write a Program to search the given pattern using optimized algorithm
for(i=0;i<len;i++) #include <stdio.h>
{ #include <string.h>
if(sourceString[i]!='.'){ int main (){
buf[cnt++] =sourceString[i]; char txt[] = "tutorialsPointisthebestplatformforprogrammers";
} char pat[] = "a";
if(sourceString[i]=='.' || i==len-1){ int M = strlen (pat);
buf[cnt]='\0'; int N = strlen (txt);
cnt=0; for (int i = 0; i <= N - M; i++){
oct[cnt1++]=atoi(buf); int j;
} for (j = 0; j < M; j++)
} if (txt[i + j] != pat[j])
ipAddress[0]=oct[0]; break;
ipAddress[1]=oct[1]; if (j == M)
ipAddress[2]=oct[2]; printf ("Pattern matches at index %d \n", i);
ipAddress[3]=oct[3]; }
} return 0;
int main() }
{ 33.Write an algorithm and Program for encrypting a plain text and decrypting a cipher text using
unsigned char ip[20]={0}; Caesar Cipher.
short ipAddress[4]; #include<stdio.h>
printf("Enter IP Address (xxx.xxx.xxx.xxx format): "); int main()
scanf("%s",ip); {
char message[100], ch;
extractIpAddress(ip,&ipAddress[0]); int i, key;
printf("Enter a message to encrypt: ");
printf("\nIp Address: %03d. %03d. %03d. gets(message);
%03d\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]); printf("Enter key: ");
scanf("%d", &key);
if(ipAddress[0]>=0 && ipAddress[0]<=127) for(i = 0; message[i] != '\0'; ++i){
printf("Class A Ip Address.\n"); ch = message[i];
if(ipAddress[0]>127 && ipAddress[0]<191) if(ch >= 'a' && ch <= 'z'){
printf("Class B Ip Address.\n"); ch = ch + key;
if(ipAddress[0]>191 && ipAddress[0]<224) if(ch > 'z'){
printf("Class C Ip Address.\n"); ch = ch - 'z' + 'a' - 1;
if(ipAddress[0]>224 && ipAddress[0]<=239) }
printf("Class D Ip Address.\n"); message[i] = ch;
if(ipAddress[0]>239) }
printf("Class E Ip Address.\n"); else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
return 0; if(ch > 'Z'){
} ch = ch - 'Z' + 'A' - 1;
Output }
Enter IP Address (xxx.xxx.xxx.xxx format): 145.160.017.001 message[i] = ch;
Ip Address: 145. 160. 017. 001 }
Class B Ip Address. }
13.Write a Program to check the strength of the password. printf("Encrypted message: %s", message);
#include<stdio.h> return 0;
#include <stdlib.h> }
int main() 38.Write an algorithm and a Program to implement Diffie Hellman Key
{ #include <stdio.h>
char c; // Function to compute `a^m mod n`
int symbol=0,dig=0,upper=0,lower=0,leng=0,count=0; int compute(int a, int m, int n)
while(scanf(“%c―,&c)>0) {
{ int r;
if(c==’#’||c==’!’||c==’$’||c==’@’||c==’_’) int y = 1;
symbol++; while (m > 0)
else if(isdigit(c)) {
dig++; r = m % 2;
else if(c>=65&&c<=90) // fast exponention
upper++; if (r == 1) {
else if(c>=97&&c<=122) y = (y*a) % n;
lower++; }
count++; a = a*a % n;

m = m / 2; printf("Message data = %lf", msg);


} // Encryption c = (msg ^ e) % n
return y; double c = pow(msg, e);
} c = fmod(c, n);
// C program to demonstrate the Diffie-Hellman algorithm printf("\nEncrypted data = %lf", c);
int main() // Decryption m = (c ^ d) % n
{ double m = pow(c, d);
int p = 23; // modulus m = fmod(m, n);
int g = 5; // base printf("\nOriginal Message Sent = %lf", m);
int a, b; // `a` – Alice's secret key, `b` – Bob's secret key. return 0;
int A, B; // `A` – Alice's public key, `B` – Bob's public key }
// choose a secret integer for Alice's private key (only known to Alice) 40. Write an algorithm and Program to generate Pseudo Random numbers in a range
a = 6; // or, use `rand()` #include <stdio.h>
// Calculate Alice's public key (Alice will send `A` to Bob) #include <conio.h>
A = compute(g, a, p); #include <stdlib.h>
// choose a secret integer for Bob's private key (only known to Bob) int main()
b = 15; // or, use `rand()` {
// Calculate Bob's public key (Bob will send `B` to Alice) int n, max, num, c;
B = compute(g, b, p); printf("Enter the number of random numbers you want\n");
// Alice and Bob Exchange their public key `A` and `B` with each other scanf("%d", &n);
// Find secret key printf("Enter the maximum value of random number\n");
int keyA = compute(B, a, p); scanf("%d", &max);
int keyB = compute(A, b, p); printf("%d random numbers from 0 to %d are:\n", n, max);
printf("Alice's secret key is %d\nBob's secret key is %d", keyA, keyB); randomize();
return 0; for (c = 1; c <= n; c++)
} {
39.Write an RSA algorithm and Program to implement digital Signature Scheme num = random(max);
// C program for RSA asymmetric cryptographic printf("%d\n",num);
// algorithm. For demonstration values are }
// relatively small compared to practical getch();
// application return 0;
#include<stdio.h> }
#include<math.h>
// Returns gcd of a and b
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}
// Code to demonstrate RSA algorithm
int main()
{
// Two random prime numbers
double p = 3;
double q = 7;
// First part of public key:
double n = p*q;
// Finding other part of public key.
// e stands for encrypt
double e = 2;
double phi = (p-1)*(q-1);
while (e < phi)
{
// e must be co-prime to phi and
// smaller than phi.
if (gcd(e, phi)==1)
break;
else
e++;
}
int k = 2; // A constant value
double d = (1 + (k*phi))/e;
// Message to be encrypted
double msg = 20;

You might also like