You are on page 1of 38

RSA Algorithm:#include<stdio.h> #include<conio.

h> int phi,M,n,e,d,C,FLAG; int check() { int i; for(i=3;e%i==0&&phi%i==0;i+2) { FLAG = 1; return; } FLAG = 0; }

void encrypt() { int i; C = 1; for(i=0;i< e;i++) C=C*M%n; C = C%n;

printf("\n\tEncrypted keyword : %d",C); }

void decrypt() { int i; M = 1; for(i=0;i< d;i++) M=M*C%n; M = M%n; printf("\n\tDecrypted keyword : %d",M); }

void main() { int p,q,s; clrscr(); printf("Enter Two Relatively Prime Numbers\t: "); scanf("%d%d",&p,&q); n = p*q; phi=(p-1)*(q-1); printf("\n\tF(n)\t= %d",phi);

do { printf("\n\nEnter e\t: "); scanf("%d",&e); check(); }while(FLAG==1); d = 1; do { s = (d*e)%phi; d++; }while(s!=1); d = d-1; printf("\n\tPublic Key\t: {%d,%d}",e,n); printf("\n\tPrivate Key\t: {%d,%d}",d,n); printf("\n\nEnter The Plain Text\t: "); scanf("%d",&M); encrypt(); printf("\n\nEnter the Cipher text\t: "); scanf("%d",&C); decrypt(); getch();

Output:-

Prime and relative prime numbers:#include <stdio.h> int gcd(int i, int x); int main() { int i, x = 20; printf("20 is relatively prime to:\n"); for(i = 1; i < 20; i++){ if( gcd( i, x ) == 1) printf("%d\n", i); } return 0; } int gcd(int i, int x) { if(x % i == 0) return( i ); return(gcd(x, i % x) ); }

Output:-

Play fair Cipher:#include <stdio.h> #define siz 5 void encrypt(int *i, int *j) { (*i)++,(*j)++; if((*i)==siz) *i=0; else if((*j)==siz) *j=0; } void playfair(char ch1,char ch2, char mat[siz][siz]) { int j,m,n,p,q,c,k; for(j=0,c=0;(c<2)||(j<siz);j++) for(k=0;k<siz;k++) if(mat[j][k] == ch1) m=j,n=k,c++; else if(mat[j][k] == ch2) p=j,q=k,c++; if(m==p) encrypt(&n,&q); else if(n==q) encrypt(&m,&p);

else n+=q,q=n-q,n-=q; printf("%c%c",mat[m][n],mat[p][q]); } void main() { char mat[siz][siz],key[10],str[25]={0}; int m,n,i,j; char temp; printf("Enter Key String:"); gets(key); m=n=0; for(i=0;key[i]!='\0';i++) { for(j=0;j<i;j++) if(key[j] == key[i]) break; if(key[i]=='j') key[i]='i'; if(j>=i) { mat[m][n++] = key[i]; if(n==siz) n=0,m++;

} } for(i=97;i<=122;i++) { for(j=0;key[j]!='\0';j++) if(key[j] == i) break; else if(i=='j') break; if(key[j]=='\0') { mat[m][n++] = i; if(n==siz) n=0,m++; } } printf("Enter input String:"); gets(str); printf("\n\nMatrix :\n"); for(i=0;i<siz;i++) { for(j=0;j<siz;j++) printf("%c\t",mat[i][j]);

printf("\n"); } printf("\n\nEntered text :%s\nCipher Text :",str); for(i=0;str[i]!='\0';i++) { temp = str[i++]; if(temp == 'j') temp='i'; if(str[i]=='\0') playfair(temp,'x',mat); else { if(str[i]=='j') str[i]='i'; if(temp == str[i]) { playfair(temp,'x',mat); i--; } else playfair(temp,str[i],mat); } } }

Output:-

Hill cipher:#include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> void main() { char ct[3],pt[3],ch[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',' w','x','y','z'},pt1[3]; int i,j,c[3],m2[3],m1[3][3],k,m0[3][3],c1[3],m3[3]; clrscr(); c[0]=0;c[1]=0;c[2]=0;c1[0]=0;c1[1]=0;c1[2]=0; printf("\nenter any string with length of 3\n"); gets(pt); printf("\nenter 3*3 matrix\n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) scanf("%d",&m1[i][j]); } for(i=0;i<=2;i++)

{ for(j=0;j<=25;j++) { if(pt[i]==ch[j]) m2[i]=j; } } for(i=0;i<=2;i++) { for(j=0;j<=2;j++) c[i]=c[i]+(m1[i][j]*m2[j]); c[i]=c[i]%26; for(k=0;k<=25;k++) { if(c[i]==k) ct[i]=ch[k]; } } ct[i]='\0'; printf("\nthe cipher text is\n"); puts(ct); for(i=0;i<=2;i++)

{ for(j=0;j<=25;j++) { if(ct[i]==ch[j]) m3[i]=j; } } printf("\nenter inverse matrix entry\n"); for(i=0;i<=2;i++) { for(j=0;j<=2;j++) scanf("%d",&m0[i][j]); } for(i=0;i<=2;i++) { for(j=0;j<=2;j++) c1[i]=c1[i]+(m0[i][j]*m3[j]); c1[i]=c1[i]%26; for(k=0;k<=25;k++) { if(c1[i]==k) pt1[i]=ch[k];

} } printf("\nthe plain text is\n"); puts(pt1); getch(); } Output:-

MD5:-

DemoMD5.java import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;

public class DemoMD5 { private static String convertedToHex(byte[] data) { StringBuffer buf = new StringBuffer();

for (int i = 0; i < data.length; i++) { int halfOfByte = (data[i] >>> 4) & 0x0F; int twoHalfBytes = 0;

do { if ((0 <= halfOfByte) && (halfOfByte <= 9)) {

buf.append( (char) ('0' + halfOfByte) ); }

else { buf.append( (char) ('a' + (halfOfByte - 10)) ); }

halfOfByte = data[i] & 0x0F;

} while(twoHalfBytes++ < 1); } return buf.toString(); }

public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md; md = MessageDigest.getInstance("MD5"); byte[] md5 = new byte[64]; md.update(text.getBytes("iso-8859-1"), 0, text.length());

md5 = md.digest(); return convertedToHex(md5); } }

Password.java import java.util.Scanner; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException;

public class Password { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String password = " "; String again = "Y";

while (again.toUpperCase().charAt(0) == 'Y') { System.out.print("Enter string: "); password = scan.nextLine();

try { System.out.println("MD5 string: " + DemoMD5.MD5(password)); }

catch (NoSuchAlgorithmException a) { a.printStackTrace(); }

catch (UnsupportedEncodingException a) { a.printStackTrace(); }

System.out.print("\nRun again?: "); again = scan.nextLine(); } System.exit(0); } }

Secure Hash Algorithm (sha):-

#ifndef _GLOBAL_H_ #define _GLOBAL_H_ 1 typedef unsigned char *POINTER; typedef unsigned long int UINT4; typedef unsigned char BYTE; #ifndef TRUE #define FALSE #define TRUE #endif /* TRUE */ #endif /* end _GLOBAL_H_ */ /* sha.h */ #ifndef _SHA_H_ #define _SHA_H_ 1 typedef struct { UINT4 digest[ 5 ]; /* Message digest */ /* 64-bit bit count */ 0 ( !FALSE )

UINT4 countLo, countHi; UINT4 data[ 16 ]; int Endianness; } SHA_CTX;

/* SHS data buffer */

/* Message digest functions */ void SHAInit(SHA_CTX *); void SHAUpdate(SHA_CTX *, BYTE *buffer, int count); void SHAFinal(BYTE *output, SHA_CTX *); #endif /* end _SHA_H_ */ /* endian.h */ #ifndef _ENDIAN_H_ #define _ENDIAN_H_ 1 void endianTest(int *endianness); #endif /* end _ENDIAN_H_ */ /* sha.c */ #include <stdio.h> #include <string.h> static void SHAtoByte(BYTE *output, UINT4 *input, unsigned int len); #define SHS_DATASIZE 64 #define SHS_DIGESTSIZE 20 /*#define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) #define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) #define f2(x,y,z) ( x ^ y ^ z ) // Rounds 0-19 */ /* Rounds 0-19 */ /* Rounds 20-39 */

/*#define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) // Rounds 40-59 */ #define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) /* Rounds 40-59 */ #define f4(x,y,z) ( x ^ y ^ z ) /* Rounds 60-79 */

#define K1 0x5A827999L #define K2 0x6ED9EBA1L #define K3 0x8F1BBCDCL #define K4 0xCA62C1D6L /* SHS initial values */ #define h0init 0x67452301L #define h1init 0xEFCDAB89L #define h2init 0x98BADCFEL #define h3init 0x10325476L #define h4init 0xC3D2E1F0L

/* Rounds 0-19 */ /* Rounds 20-39 */ /* Rounds 40-59 */ /* Rounds 60-79 */

#define ROTL(n,X) ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) ) #define expand(W,i) ( W[ i & 15 ] = ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \ W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) ) #define subRound(a, b, c, d, e, f, k, data) \ ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) ) void SHAInit(SHA_CTX *shsInfo) { endianTest(&shsInfo->Endianness); /* Set the h-vars to their initial values */ shsInfo->digest[ 0 ] = h0init; shsInfo->digest[ 1 ] = h1init; shsInfo->digest[ 2 ] = h2init;

shsInfo->digest[ 3 ] = h3init; shsInfo->digest[ 4 ] = h4init; /* Initialise bit count */ shsInfo->countLo = shsInfo->countHi = 0; } static void SHSTransform( digest, data ) UINT4 *digest, *data ; { UINT4 A, B, C, D, E; UINT4 eData[ 16 ]; /* Local vars */ /* Expanded data */

/* Set up first buffer and local data buffer */ A = digest[ 0 ]; B = digest[ 1 ]; C = digest[ 2 ]; D = digest[ 3 ]; E = digest[ 4 ]; memcpy( (POINTER)eData, (POINTER)data, SHS_DATASIZE ); subRound( A, B, C, D, E, f1, K1, eData[ 0 ] ); subRound( E, A, B, C, D, f1, K1, eData[ 1 ] ); subRound( D, E, A, B, C, f1, K1, eData[ 2 ] ); subRound( C, D, E, A, B, f1, K1, eData[ 3 ] ); subRound( B, C, D, E, A, f1, K1, eData[ 4 ] );

subRound( A, B, C, D, E, f1, K1, eData[ 5 ] ); subRound( E, A, B, C, D, f1, K1, eData[ 6 ] ); subRound( D, E, A, B, C, f1, K1, eData[ 7 ] ); subRound( C, D, E, A, B, f1, K1, eData[ 8 ] ); subRound( B, C, D, E, A, f1, K1, eData[ 9 ] ); subRound( A, B, C, D, E, f1, K1, eData[ 10 ] ); subRound( E, A, B, C, D, f1, K1, eData[ 11 ] ); subRound( D, E, A, B, C, f1, K1, eData[ 12 ] ); subRound( C, D, E, A, B, f1, K1, eData[ 13 ] ); subRound( B, C, D, E, A, f1, K1, eData[ 14 ] ); subRound( A, B, C, D, E, f1, K1, eData[ 15 ] ); subRound( E, A, B, C, D, f1, K1, expand( eData, 16 ) ); subRound( D, E, A, B, C, f1, K1, expand( eData, 17 ) ); subRound( C, D, E, A, B, f1, K1, expand( eData, 18 ) ); subRound( B, C, D, E, A, f1, K1, expand( eData, 19 ) );

subRound( A, B, C, D, E, f2, K2, expand( eData, 20 ) ); subRound( E, A, B, C, D, f2, K2, expand( eData, 21 ) ); subRound( D, E, A, B, C, f2, K2, expand( eData, 22 ) ); subRound( C, D, E, A, B, f2, K2, expand( eData, 23 ) ); subRound( B, C, D, E, A, f2, K2, expand( eData, 24 ) ); subRound( A, B, C, D, E, f2, K2, expand( eData, 25 ) );

subRound( E, A, B, C, D, f2, K2, expand( eData, 26 ) ); subRound( D, E, A, B, C, f2, K2, expand( eData, 27 ) ); subRound( C, D, E, A, B, f2, K2, expand( eData, 28 ) ); subRound( B, C, D, E, A, f2, K2, expand( eData, 29 ) ); subRound( A, B, C, D, E, f2, K2, expand( eData, 30 ) ); subRound( E, A, B, C, D, f2, K2, expand( eData, 31 ) ); subRound( D, E, A, B, C, f2, K2, expand( eData, 32 ) ); subRound( C, D, E, A, B, f2, K2, expand( eData, 33 ) ); subRound( B, C, D, E, A, f2, K2, expand( eData, 34 ) ); subRound( A, B, C, D, E, f2, K2, expand( eData, 35 ) ); subRound( E, A, B, C, D, f2, K2, expand( eData, 36 ) ); subRound( D, E, A, B, C, f2, K2, expand( eData, 37 ) ); subRound( C, D, E, A, B, f2, K2, expand( eData, 38 ) ); subRound( B, C, D, E, A, f2, K2, expand( eData, 39 ) );

subRound( A, B, C, D, E, f3, K3, expand( eData, 40 ) ); subRound( E, A, B, C, D, f3, K3, expand( eData, 41 ) ); subRound( D, E, A, B, C, f3, K3, expand( eData, 42 ) ); subRound( C, D, E, A, B, f3, K3, expand( eData, 43 ) ); subRound( B, C, D, E, A, f3, K3, expand( eData, 44 ) ); subRound( A, B, C, D, E, f3, K3, expand( eData, 45 ) ); subRound( E, A, B, C, D, f3, K3, expand( eData, 46 ) );

subRound( D, E, A, B, C, f3, K3, expand( eData, 47 ) ); subRound( C, D, E, A, B, f3, K3, expand( eData, 48 ) ); subRound( B, C, D, E, A, f3, K3, expand( eData, 49 ) ); subRound( A, B, C, D, E, f3, K3, expand( eData, 50 ) ); subRound( E, A, B, C, D, f3, K3, expand( eData, 51 ) ); subRound( D, E, A, B, C, f3, K3, expand( eData, 52 ) ); subRound( C, D, E, A, B, f3, K3, expand( eData, 53 ) ); subRound( B, C, D, E, A, f3, K3, expand( eData, 54 ) ); subRound( A, B, C, D, E, f3, K3, expand( eData, 55 ) ); subRound( E, A, B, C, D, f3, K3, expand( eData, 56 ) ); subRound( D, E, A, B, C, f3, K3, expand( eData, 57 ) ); subRound( C, D, E, A, B, f3, K3, expand( eData, 58 ) ); subRound( B, C, D, E, A, f3, K3, expand( eData, 59 ) );

subRound( A, B, C, D, E, f4, K4, expand( eData, 60 ) ); subRound( E, A, B, C, D, f4, K4, expand( eData, 61 ) ); subRound( D, E, A, B, C, f4, K4, expand( eData, 62 ) ); subRound( C, D, E, A, B, f4, K4, expand( eData, 63 ) ); subRound( B, C, D, E, A, f4, K4, expand( eData, 64 ) ); subRound( A, B, C, D, E, f4, K4, expand( eData, 65 ) ); subRound( E, A, B, C, D, f4, K4, expand( eData, 66 ) ); subRound( D, E, A, B, C, f4, K4, expand( eData, 67 ) );

subRound( C, D, E, A, B, f4, K4, expand( eData, 68 ) ); subRound( B, C, D, E, A, f4, K4, expand( eData, 69 ) ); subRound( A, B, C, D, E, f4, K4, expand( eData, 70 ) ); subRound( E, A, B, C, D, f4, K4, expand( eData, 71 ) ); subRound( D, E, A, B, C, f4, K4, expand( eData, 72 ) ); subRound( C, D, E, A, B, f4, K4, expand( eData, 73 ) ); subRound( B, C, D, E, A, f4, K4, expand( eData, 74 ) ); subRound( A, B, C, D, E, f4, K4, expand( eData, 75 ) ); subRound( E, A, B, C, D, f4, K4, expand( eData, 76 ) ); subRound( D, E, A, B, C, f4, K4, expand( eData, 77 ) ); subRound( C, D, E, A, B, f4, K4, expand( eData, 78 ) ); subRound( B, C, D, E, A, f4, K4, expand( eData, 79 ) );

/* Build message digest */ digest[ 0 ] += A; digest[ 1 ] += B; digest[ 2 ] += C; digest[ 3 ] += D; digest[ 4 ] += E; } static void longReverse(UINT4 *buffer, int byteCount, int Endianness ) {

UINT4 value;

if (Endianness==TRUE) return; byteCount /= sizeof( UINT4 ); while( byteCount-- ) { value = *buffer; value = ( ( value & 0xFF00FF00L ) >> 8 ) | \ ( ( value & 0x00FF00FFL ) << 8 ); *buffer++ = ( value << 16 ) | ( value >> 16 ); } } void SHAUpdate(SHA_CTX *shsInfo, BYTE *buffer, int count) { UINT4 tmp; int dataCount; tmp = shsInfo->countLo; if ( ( shsInfo->countLo = tmp + ( ( UINT4 ) count << 3 ) ) < tmp ) shsInfo->countHi++; /* Carry from low to high */

shsInfo->countHi += count >> 29; dataCount = ( int ) ( tmp >> 3 ) & 0x3F; if( dataCount )

{ BYTE *p = ( BYTE * ) shsInfo->data + dataCount; dataCount = SHS_DATASIZE - dataCount; if( count < dataCount ) { memcpy( p, buffer, count ); return; } memcpy( p, buffer, dataCount ); longReverse( shsInfo->data, SHS_DATASIZE, shsInfo->Endianness); SHSTransform( shsInfo->digest, shsInfo->data ); buffer += dataCount; count -= dataCount; } while( count >= SHS_DATASIZE ) { memcpy( (POINTER)shsInfo->data, (POINTER)buffer, SHS_DATASIZE ); longReverse( shsInfo->data, SHS_DATASIZE, shsInfo->Endianness ); SHSTransform( shsInfo->digest, shsInfo->data ); buffer += SHS_DATASIZE; count -= SHS_DATASIZE; }

memcpy( (POINTER)shsInfo->data, (POINTER)buffer, count ); } void SHAFinal(BYTE *output, SHA_CTX *shsInfo) { int count; BYTE *dataPtr; count = ( int ) shsInfo->countLo; count = ( count >> 3 ) & 0x3F; dataPtr = ( BYTE * ) shsInfo->data + count; *dataPtr++ = 0x80; count = SHS_DATASIZE - 1 - count; if( count < 8 ) { memset( dataPtr, 0, count );

longReverse( shsInfo->data, SHS_DATASIZE, shsInfo->Endianness ); SHSTransform( shsInfo->digest, shsInfo->data ); memset( (POINTER)shsInfo->data, 0, SHS_DATASIZE - 8 ); } else memset( dataPtr, 0, count - 8 ); shsInfo->data[ 14 ] = shsInfo->countHi; shsInfo->data[ 15 ] = shsInfo->countLo; longReverse( shsInfo->data, SHS_DATASIZE - 8, shsInfo->Endianness );

SHSTransform( shsInfo->digest, shsInfo->data ); SHAtoByte(output, shsInfo->digest, SHS_DIGESTSIZE); memset((POINTER)shsInfo, 0, sizeof(shsInfo)); }

static void SHAtoByte(BYTE *output, UINT4 *input, unsigned int len) { /* Output SHA digest in byte array */ unsigned int i, j;

for(i = 0, j = 0; j < len; i++, j += 4) { output[j+3] = (BYTE)( input[i] & 0xff);

output[j+2] = (BYTE)((input[i] >> 8 ) & 0xff); output[j+1] = (BYTE)((input[i] >> 16) & 0xff); output[j ] = (BYTE)((input[i] >> 24) & 0xff); } }

unsigned char digest[20]; unsigned char message[3] = {'a', 'b', 'c' }; unsigned char *mess56 =

"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";

/* Correct solutions from FIPS PUB 180-1 */ char *dig1 = "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D"; char *dig2 = "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1"; char *dig3 = "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"; main() { SHA_CTX sha; int i; BYTE big[1000];

SHAInit(&sha); SHAUpdate(&sha, message, 3); SHAFinal(digest, &sha);

for (i = 0; i < 20; i++) { if ((i % 4) == 0) printf(" "); printf("%02x", digest[i]); } printf("\n");

printf(" %s <= correct\n", dig1);

SHAInit(&sha); SHAUpdate(&sha, mess56, 56); SHAFinal(digest, &sha);

for (i = 0; i < 20; i++) { if ((i % 4) == 0) printf(" "); printf("%02x", digest[i]); } printf("\n"); printf(" %s <= correct\n", dig2);

/* Fill up big array */ for (i = 0; i < 1000; i++) big[i] = 'a';

SHAInit(&sha); /* Digest 1 million x 'a' */ for (i = 0; i < 1000; i++) SHAUpdate(&sha, big, 1000);

SHAFinal(digest, &sha);

for (i = 0; i < 20; i++) { if ((i % 4) == 0) printf(" "); printf("%02x", digest[i]); } printf("\n"); printf(" %s <= correct\n", dig3);

return 0; }

/* endian.c */

void endianTest(int *endian_ness) { if((*(unsigned short *) ("#S") >> 8) == '#') { /* printf("Big endian = no change\n"); */ *endian_ness = !(0); }

else { /* printf("Little endian = swap\n"); */ *endian_ness = 0; } }

Output:-

Euler theorem:# include <stdio.h> # include <conio.h> void main() { int c=0; float x,y,xp,h,dy,i,n; float f(float,float); clrscr(); printf("Solution by Euler Method\n"); printf("Enter initial Boundry condition x,y : "); scanf("%f%f",&x,&y); printf("Enter Value of X at which Y is required : "); scanf("%f",&xp); printf("Enter Interval ,h : "); scanf("%f",&h); printf(" No. \t X\t f(x,y) \t Y\n"); printf("----------------------------------------------------------\n"); for(i=x;i<=xp;i=i+h) { c++; n=y+h*f(i,y);

printf("%2d\t %2.3f\t %5.5f\t %5.6f\n",c,i,f(i,y),n); y=n; } printf("----------------------------------------------------------\n"); printf("Value of y @ %f is %f",xp,n); getch(); } float f(float x,float y) { return (y-x)/(y+x); }

Output:-

You might also like