Вы находитесь на странице: 1из 13

Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include<stdio.h>
#define STEP 3
int check(char arr[]){
int i;
for(i=0;i<strlen(arr);i++){
if((arr[i]>='A' && arr[i]<='Z') | (arr[i]>='a' && arr[i]<='z'))
continue;
else
return 0;
}
return 1;
}
void encrypt(char arr[]){
int i,j;
for(i=0;i<strlen(arr);i++){
if(arr[i]==' ')
continue;
for(j=0;j<STEP;j++){
arr[i]++;
if(arr[i]<97 && arr[i] > 90)
arr[i] = 122;
if(arr[i] < 65)
arr[i] = 90;
}
}
}
void decrypt(char arr[]){
int i,j;
for(i=0;i<strlen(arr);i++){
if(arr[i]==' ')
continue;
for(j=0;j<STEP;j++){
arr[i]--;
if(arr[i]>122)
arr[i] = 97;
if(arr[i] > 90 && arr[i]<97)
arr[i] = 65;
}
}
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

int main(){
char msg[100];
printf("Enter your message:\n");
scanf ("%[^\n]s", msg);
if(!check(msg))
printf("Invalid Characters present in the message.\n");
else{
printf("\nEncrypted Version : \n");
encrypt(msg);
printf("%s",msg);
printf("\n Decrypt Version : \n");
decrypt(msg);
printf("%s",msg);
}
return 0;
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include <iostream>

using namespace std;

class mRND
{
public:
void seed(unsigned int s)
{
_seed = s;
}

protected:
mRND() :
_seed(0), _a(0), _c(0), _m(2147483648)
{
}
int rnd()
{
return (_seed = (_a * _seed + _c) % _m);
}

int _a, _c;


unsigned int _m, _seed;
};

class MS_RND: public mRND


{
public:
MS_RND()
{
_a = 214013;
_c = 2531011;
}
int rnd()
{
return mRND::rnd() >> 16;
}
};

class BSD_RND: public mRND


{
public:
BSD_RND()
{
_a = 1103515245;

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

_c = 12345;
}
int rnd()
{
return mRND::rnd();
}
};

int main(int argc, char* argv[])


{
BSD_RND bsd_rnd;
MS_RND ms_rnd;

cout << "MS RAND:" << endl << "========" << endl;
for (int x = 0; x < 10; x++)
cout << ms_rnd.rnd() << endl;

cout << endl << "BSD RAND:" << endl << "=========" << endl;
for (int x = 0; x < 10; x++)
cout << bsd_rnd.rnd() << endl;

return 0;
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long long mulmod(long long a, long long b, long long mod)
{
long long x = 0,y = a % mod;
while (b > 0)
{
if (b % 2 == 1)
{
x = (x + y) % mod;
}
y = (y * 2) % mod;
b /= 2;
}
return x % mod;
}
long long modulo(long long base, long long exponent, long long mod)
{
long long x = 1;
long long y = base;
while (exponent > 0)
{
if (exponent % 2 == 1)
x = (x * y) % mod;
y = (y * y) % mod;
exponent = exponent / 2;
}
return x % mod;
}

int Miller(long long p,int iteration)


{

int i;
long long s;
if (p < 2)
{
return 0;
}
if (p != 2 && p % 2==0)
{
return 0;
}
s = p - 1;
while (s % 2 == 0)

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

{
s /= 2;
}
for (i = 0; i < iteration; i++)
{
long long a = rand() % (p - 1) + 1, temp = s;
long long mod = modulo(a, temp, p);
while (temp != p - 1 && mod != 1 && mod != p - 1)
{
mod = mulmod(mod, mod, p);
temp *= 2;
}
if (mod != p - 1 && temp % 2 == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int iteration = 5;
long long num;
printf("Enter integer to test primality: ");
scanf("%lld", &num);
if ( Miller( num, iteration))
printf("\n%lld is prime\n", num);
else
printf("\n%lld is not prime\n", num);
return 0;
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include <stdio.h>

int gcd_algorithm(int x, int y)


{
if (y == 0) {
return x;
} else if (x >= y && y > 0) {
return gcd_algorithm(y, (x % y));
}
}

int main(void)
{
int num1, num2, gcd;
printf("\nEnter two numbers to find gcd using Euclidean algorithm: ");
scanf("%d%d", &num1, &num2);
gcd = gcd_algorithm(num1, num2);
if (gcd)
printf("\nThe GCD of %d and %d is %d\n", num1, num2, gcd);
else
printf("\nInvalid input!!!\n");
return 0;
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include<stdio.h>
int main()
{
int i, cnt=0, p8[8]={6,7,8,9,1,2,3,4};
int p10[10]={6,7,8,9,10,1,2,3,4,5};

char input[11], k1[10], k2[10], temp[11];


char LS1[5], LS2[5];
//k1, k2 are for storing interim keys
//p8 and p10 are for storing permutation key

//Read 10 bits from user...


printf("Enter 10 bits input:");
scanf("%s",input);
input[10]='\0';

//Applying p10...
for(i=0; i<10; i++)
{
cnt = p10[i];
temp[i] = input[cnt-1];
}
temp[i]='\0';
printf("\nYour p10 key is :");
for(i=0; i<10; i++)
{ printf("%d,",p10[i]); }

printf("\nBits after p10 :");


puts(temp);
//Performing LS-1 on first half of temp
for(i=0; i<5; i++)
{
if(i==4)
temp[i]=temp[0];
else
temp[i]=temp[i+1];
}
//Performing LS-1 on second half of temp
for(i=5; i<10; i++)
{
if(i==9)
temp[i]=temp[5];
else
temp[i]=temp[i+1];
}
printf("Output after LS-1 :");

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

puts(temp);

printf("\nYour p8 key is :");


for(i=0; i<8; i++)
{ printf("%d,",p8[i]); }

//Applying p8...
for(i=0; i<8; i++)
{
cnt = p8[i];
k1[i] = temp[cnt-1];
}
printf("\nYour key k1 is :");
puts(k1);
}

DES

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;


char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0)
{
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q)
{
printf("\nWRONG INPUT\n");
getch();
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for(i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

getch();
}
int prime(long int pr)
{
int i;
j=sqrt(pr);
for(i=2;i<=j;i++)
{
if(pr%i==0)
return 0;
}
return 1;
}
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;
}
}
}
long int cd(long int x)
{
long int k=1;
while(1)
{
k=k+t;
if(k%x==0)
return(k/x);
}
}

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

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",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",m[i]);

ABES Engineering College Sign of Faculty with date


Roll No.: ……………………………… Date: ……..…………….….. Page No.: …..…

Practical Name: ……………………………………………….…..………………………………………………...………. Practical No.: ………………….…

ABES Engineering College Sign of Faculty with date

Вам также может понравиться