crypto file2

of 22

  • date post

    10-Apr-2018
  • Category

    Documents

  • view

    220
  • download

    0

Embed Size (px)

Transcript of crypto file2

  • 8/8/2019 crypto file2

    1/22

    Cryptography and Network SecurityPRACTICAL FILE

    TIT-751

    SUBMITTED TO SUBMITTED BY

    1

  • 8/8/2019 crypto file2

    2/22

    INDEX

    Sl.No

    .Name of Practicals PageNo. Date

    Remarks &

    Signature

    1.Caesar Cipher

    implementation 3-5

    2.Eucledian

    Algorithm 6-7

    3.

    Euclidean

    Algorithm for 10

    numbers stored in

    array

    8-9

    4.Implementation of

    RSA algorithm 10-12

    5.

    Implementation of

    LCG algorithm 13-18

    6.

    Implementation of

    miller rabinalgorithm

    19-22

    7.

    2

  • 8/8/2019 crypto file2

    3/22

    PROGRAM 1

    Caesar Cipher Algorithm:

    Earliest known use of substitution cipher was by Julius Caesar.

    1. assign a numerical equivalent to each letter

    a = 0

    b = 1

    c = 2

    .

    .

    .

    .

    .

    .

    y = 24

    z = 25.

    2. For each plain text letter p, substitute the cipher text letter C

    As

    C = E(3,p) = ( p+3) mod 26.3. general Caesar algorithm is

    C = E(k,p) = (p+k) mod26

    P = D(k,C) = (C- k)mod26

    3

  • 8/8/2019 crypto file2

    4/22

    Implementation of caesar cipher :

    /*ceasar cipher .. */

    #include#include

    void main()

    {

    int a, i, d;

    char arr[5];

    clrscr();

    printf("Enter the character to be encoded\n");

    for(i=0;i

  • 8/8/2019 crypto file2

    5/22

    /*******************OUTPUT******************/

    Enter the character to be encoded

    abcdef

    Enter the value to be added3

    defghi

    5

  • 8/8/2019 crypto file2

    6/22

    PROGRAM #2

    Eucledian algorithm

    EUCLID(a,b)

    1. A a; B b;

    2. if B=0 return A = gcd(a,b)

    3. R = A mod B

    4. A B

    5. B R

    6. goto step 2.

    Implementation of Eucledian algorithm in C

    #include

    #include

    int gcd(int,int);

    void main()

    {

    int x,y,GCD;

    clrscr();

    printf("Enter the number whose GCD is to be calculated\n");

    scanf("%d \n%d", &x,&y);

    if(x==0)GCD = y;

    else if(y==0)

    GCD = x;

    else

    GCD = gcd(x,y);

    6

  • 8/8/2019 crypto file2

    7/22

    printf("The GCD of the given numbers is \n%d", GCD);

    getch();

    }

    int gcd(int a, int b)

    {

    int g, k;

    if(b==0)

    g=a;

    else{

    k = a%b;

    g = gcd(b,k);

    }

    return(g);}

    /********************OUTPUT*******************/

    Enter the number whose GCD is to be calculated

    55

    35

    The GCD of the given numbers is5

    7

  • 8/8/2019 crypto file2

    8/22

    PROGRAM #3

    Finding GCD of 10 numbers stored in array using Eucledian

    algorithm.

    #include

    #include

    int gcd(int x,int y);

    void main()

    {int x,y,i,j,sml;

    int a[10],b[9];

    clrscr();

    printf("enter ten numbers");

    for(i=0;i

  • 8/8/2019 crypto file2

    9/22

    }

    int gcd(int x,int y)

    {

    if(x==0)

    return y;if(y==0)

    return x;

    return(gcd((y%x),x));

    }

    9

  • 8/8/2019 crypto file2

    10/22

    PROGRAM #4

    RSA algorithm.

    1. Select two prime number p & q.2. Calculate n=p*q.

    3. Calculate (n)=(p-1)*(q-1).

    4. Select e such that e relatively prime to (n) and e

  • 8/8/2019 crypto file2

    11/22

    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++;

    11

  • 8/8/2019 crypto file2

    12/22

    }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 *****************/

    Enter Two Relatively Prime Numbers : 7

    17

    F(n) = 96

    Enter e : 5

    Public Key : {5,119}

    Private Key : {77,119}

    Enter The Plain Text : 19

    Encrypted keyword : 66

    Enter the Cipher text : 66

    Decrypted keyword : 19

    12

  • 8/8/2019 crypto file2

    13/22

    PROGRAM #5

    LCG Algorithm:

    A linear congruential generator(LCG) represents one of the oldest and

    best-known pseudorandom number generator algorithms. The theory behind

    them is easy to understand, and they are easily implemented and fast.

    The generator is defined by the recurrence relation:

    where Xn is the sequence of pseudorandom values, and

    the "modulus"

    the "multiplier"

    the "increment" (the special case ofc =

    0 corresponds to ParkMiller RNG)

    the "seed" or "start value"

    are integer constants that specify the generator.

    The period of a general LCG is at most m, and for some

    choices ofa much less than that. The LCG will have a full

    period if and only if:

    1. and are relatively prime,

    2. is divisible by all prime factors of ,

    3. is a multiple of 4 if is a multiple of 4.[2]

    13

  • 8/8/2019 crypto file2

    14/22

    Implementation of Linear congruential generator.

    /**********************************************

    /* A Basic Linear Congruential Generator/* ---------------------------------------

    /**********************************************/

    #include

    #include

    #include

    //This class will give satisfy basic

    // random number considerations for

    // games an general apps.

    class BasicLCG {

    private:

    unsigned long iCurrent;

    public:

    BasicLCG();

    BasicLCG(unsigned long);

    void seed(unsigned long iSeed);unsigned long nextNumber(); //get the next random number

    unsigned short int nextInt();

    unsigned char nextChar();

    int nextBit();

    double nextDouble();

    int inRange(int min, int max);

    };

    //Just a little test code to print some numbersint main()

    {

    BasicLCG rng(time(NULL));

    int i;

    clrscr();

    14

  • 8/8/2019 crypto file2

    15/22

    //Lets see some bits...

    for( i=1; i

  • 8/8/2019 crypto file2

    16/22

    }

    void BasicLCG::seed(unsigned long iSeed)

    {

    iCurrent = iSeed;

    }

    unsigned long BasicLCG::nextNumber()

    {

    unsigned long iOutput;

    unsigned long iTemp;

    int i;

    //take the top two bits//This will shorten our period to (2^32)/16=268,435,456

    //Which seems like plenty

    for(i=0; i> 30;

    iOutput = iOutput

  • 8/8/2019 crypto file2

    17/22

    //Since this is mod 2^32 and our data type is 32 bits long

    // there is no need for the MOD operator.

    iCurrent = (3039177861 * iCurrent + 1);

    iTemp = iCurrent >> 30;

    iOutput = iOutput 30;

    cOutput = cOutput > 31;

    }

    double BasicLCG::nextDouble()

    {

    return (double)nextNumber()/0xFFFFFFFF;

    }

    int BasicLCG::inRange(int iMin, int iMax)

    17

  • 8/8/2019 crypto file2

    18/22

    {

    int Diff;

    //IF the user put them in backwards then swap them

    if (iMax

  • 8/8/2019 crypto file2

    19/22

    PROGRAM #6

    Miller Rabin Algorithm

    TEST(n)1. Find integers k, q, with k>0 , q odd, so that (n-1 = 2kq);

    2. select a random integer a, 1 < a < n-1;

    3. if aq mod n = 1 then return (inconclusive);

    4. for j = 0 to k-1 do

    5. if a2^j q mod n n-1 then return(inconclusive);

    6. return(composite);

    Implementation in c

    #include

    #include

    #include #include

    #include "c:\tc\bin\integer.h"

    #define COMPOSITE 0

    #define PRIME 1

    integer modular_exponent(integer base, integer power, integer modulus) {

    int i, bit;integer result = create_integer(modulus.num_components + 1);

    integer temp = create_integer(modulus.num_components*2 + 1);

    set_zero_integer(result);

    result.c[0] = 1;

    19

  • 8/8/2019 crypto file2

    20/22

    for(i=power.num_components - 1; i>=0; i--) {

    for(bit=COMPONENT_BITS-1; bit>=0; bit--) {

    multiply_integer(result, result, temp);

    mod_integer(temp, modulus, result);

    if ((power.c[i] & (1

  • 8/8/2019 crypto file2

    21/22

    if (compare_integers(a_to_power, one) == 0) { result=PRIME; goto

    exit; }

    for(i=0; i < s-1; i++) {

    if (compare_integers(a_to_power, n_minus_one) == 0)

    { result=PRIME; goto exit; }

    multiply_integer(a_to_power, a_to_power, temp);

    mod_integer(temp, n, a_to_power);

    }

    if (compare_integers(a_to_power, n_minus_one) == 0) { result=PRIME;

    goto exit; }

    result = COMPOSITE;

    exit:

    free_integer(temp);free_integer(a_to_power);

    free_integer(one);

    free_integer(n_minus_one);

    return result;

    }

    void random_integer(integer max, integer result) {

    int i, most_sig = max.num_components - 1;

    while (max.c[most_sig] == 0) most_sig--;