Saturday, September 26, 2020

RSA Algorithm Implementation in C

Aim

To write a C program to implement RSA (Rivest–Shamir–Adleman) asymmetric cryptographic algorithm.

Algorithm

  1. Select two large prime numbers p and q.
  2. Compute n = p * q.
  3. Choose system modulus phi(n) = (p - 1) * (q - 1).
  4. Decrypt by computing d = e-1 mod phi(n).
  5. Encryption: c = me mod n.
  6. Decryption: m = cd mod n.

Program Code

#include <stdio.h>

int gcd(int a, int b)
{
    int temp;
    while (b != 0)
    {
        temp = a;
        a = b;
        b = temp % b;
    }
    return a;
}

int power(int a, int n, int m)
{
    int res = 1;
    while (n != 0)
    {
        if (n % 2 == 1)
            res = (res * a) % m;
        a = (a * a) % m;
        n /= 2;
    }
    return res;
}

int main()
{
    int p, q, n, phi, e, d, msg, enc, dec;

    printf("Enter two non-equal prime numbers: ");
    scanf("%d %d", &p, &q);

    n = p * q;
    e = 2;
    phi = (p - 1) * (q - 1);
    while (e < phi)
    {
        if (gcd(e, phi) == 1)
            break;
        else
            e++;
    }
    int k = 2;
    d = (1 + k*phi) / e;

    printf("Enter numeric message data: ");
    scanf("%d", &msg);
    enc = power(msg, e, n);
    dec = power(enc, d, n);

    printf("\np = %d, q = %d", p, q);
    printf("\nn = %d", n);
    printf("\nPhi(n) = %d", phi);
    printf("\nPublic key, PU = {%d, %d}", e, n);
    printf("\nPrivate key, PR = {%d, %d}", d, n);
    printf("\nMessage data = %d", msg);
    printf("\nEncrypted data = %d", enc);
    printf("\nDecrypted data = %d", dec);

    return 0;
}

Output





Wednesday, September 23, 2020

C Program to implement 2D Composite Transformation

Aim

To write a C program to implement 2D composite transformations such as consecutive translation, consecutive rotation, pivot point rotation and consecutive scaling.

Algorithm

Consecutive Translation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the 1st and 2nd translation co-ordinates.
  4. For each translation, add the translation co-ordinates to the object co-ordinates.
  5. Draw the translated object on the screen.


Consecutive Rotation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the 1st and 2nd rotation angles.
  4. For each rotation, calculate r = (Ɵ * 3.14) / 180.
  5. Calculate xn = xi * cos(r) - yi * sin(r).
  6. Calculate yn = yi * cos(r) + xi * sin(r).
  7. Draw the rotated object on the screen.


Pivot Point Rotation

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the pivot point co-ordinates.
  4. Input the rotation angle.
  5. Translate the object to the pivot point co-ordinates.
  6. Rotate the object with the given angle.
  7. Translate the rotated object back to the original co-ordinates.


Consecutive Scaling

  1. Input the co-ordinates of the object.
  2. Draw the object on the screen.
  3. Input the scaling factors.
  4. For each scaling transformation, multiply the object co-ordinates by the scaling factors.
  5. Draw the scaled object on the screen.



Program Code

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void drawTri(int x[], int y[])
{
    int i;
    for (i = 0; i < 3; i++)
    {
        if (i == 2)
            line(x[i], y[i], x[0], y[0]);
        else
            line(x[i], y[i], x[i+1], y[i+1]);
    }
}

void main()
{
    int gd = DETECT, gm;
    int choice, x[3], y[3], i, j, n;
    int tx1, tx2, ty1, ty2, theta1, theta2, px, py, sx[10], sy[10];
    float tx, ty, r;
    clrscr();
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

    do
    {
        printf("1. Consecutive Translation\n2. Consecutive Rotation\n3. Pivot Point Rotation\n4. Consecutive Scaling\n5. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        if (choice != 5)
        {
            printf("\nEnter the co-ordinates of Triangle...\n");
            for (i = 0; i < 3; i++)
            {
                printf("(x%d, y%d): ", i+1, i+1);
                scanf("%d %d", &x[i], &y[i]);
            }
        }

        switch (choice)
        {
        case 1:
            printf("\nEnter the 1st translation (tx1, ty1): ");
            scanf("%d %d", &tx1, &ty1);
            printf("\nEnter the 2nd translation (tx2, ty2): ");
            scanf("%d %d", &tx2, &ty2);
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] += tx1;
                y[i] += ty1;
            }
            setcolor(3);
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] += tx2;
                y[i] += ty2;
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 2:
            printf("\nEnter the 1st rotation angle: ");
            scanf("%d", &theta1);
            printf("Enter the 2nd rotation angle: ");
            scanf("%d", &theta2);
            cleardevice();
            drawTri(x, y);
            r = M_PI * theta1 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(3);
            drawTri(x, y);
            r = M_PI * theta2 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 3:
            printf("\nEnter the pivot point (px, py): ");
            scanf("%d %d", &px, &py);
            printf("Enter the rotation angle: ");
            scanf("%d", &theta1);
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] = x[i] + px;
                y[i] = y[i] + py;
            }
            setcolor(3);
            drawTri(x, y);
            r = M_PI * theta1 / 180;
            for (i = 0; i < 3; i++)
            {
                tx = x[i];
                ty = y[i];
                x[i] = tx * cos(r) - ty * sin(r);
                y[i] = tx * sin(r) + ty * cos(r);
            }
            setcolor(BLUE);
            drawTri(x, y);
            for (i = 0; i < 3; i++)
            {
                x[i] = x[i] - px;
                y[i] = y[i] - py;
            }
            setcolor(GREEN);
            drawTri(x, y);
            getch();
            break;
        case 4:
            printf("\nEnter the number of scaling transformations: ");
            scanf("%d", &n);
            for (i = 0; i < n; i++)
            {
                printf("Enter the scaling unit (sx%d, sy%d): ", i+1, i+1);
                scanf("%d %d", &sx[i], &sy[i]);
            }
            cleardevice();
            drawTri(x, y);
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    x[j] = x[j] * sx[i];
                    y[j] = y[j] * sy[i];
                }
                setcolor(i + 1);
                drawTri(x, y);
            }
            getch();
            break;
        default:
            break;
        }

        clrscr();
        cleardevice();
        setcolor(WHITE);

    } while (choice != 5);
    closegraph();
}

C Program to implement Window to Viewport Transformation

Aim

To write a C graphics program to implement window to viewport transformation.

Algorithm

  1. Initialize graphics mode.
  2. Draw a window.
  3. Get the object.
  4. Translate the object together with its window until the lower left corner of the window is at origin.
  5. Object and window are scaled until the window has the dimensions of the viewport.
  6. Translate the viewport to its correct position on the screen.
  7. Display the contents inside the viewport.

Program Code

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void main()
{
    float sx, sy;
    int w1, w2, w3, w4, x1, x2, x3, x4, y1, y2, y3, y4, v1, v2, v3, v4;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    printf("Enter coordinates for (x1,y1),(x2,y2),(x3,y3)\n");
    scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
    cleardevice();
    w1 = 5;
    w2 = 5;
    w3 = 635;
    w4 = 465;
    rectangle(w1, w2, w3, w4);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    getch();
    v1 = 425;
    v2 = 75;
    v3 = 550;
    v4 = 250;
    sx = (float)(v3 - v1) / (w3 - w1);
    sy = (float)(v4 - v2) / (w4 - w2);
    rectangle(v1, v2, v3, v4);
    x1 = v1 + floor(((float)(x1 - w1) * sx) + 5);
    x2 = v1 + floor(((float)(x2 - w1) * sx) + 5);
    x3 = v1 + floor(((float)(x3 - w1) * sx) + 5);
    y1 = v2 + floor(((float)(y1 - w2) * sy) + 5);
    y2 = v2 + floor(((float)(y2 - w2) * sy) + 5);
    y3 = v2 + floor(((float)(y3 - w2) * sy) + 5);
    line(x1, y1, x2, y2);
    line(x2, y2, x3, y3);
    line(x3, y3, x1, y1);
    getch();
}

Output



Sunday, September 20, 2020

Affine Cipher Implementation in C

Overview

The Affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent (a=0, b=1, c=2, ..., z=25), encrypted using a simple mathematical function, and converted back to a letter.

Choose two integers a and b such that gcd(a, 26) == 1. The affine encryption function is

E(x) = a*x + b (mod 26)

The affine decryption function is

D(x) = a-1 * (x - b) (mod 26)

where a-1 is the modular multiplicative inverse of 'a' modulo 26 such that a * a-1 mod 26 == 1.

For example, let a = 5 and b = 7. Take a plain text letter x = 'H' (value is 7).

It is encrypted as a*x + b => 5*7 + 7 => 42 => 16 which is letter 'Q'.

So, the text "CODE VIBES" is encrypted as "RZWB IVMBT".

More about Affine Cipher

Aim

To write a C program to implement Affine cipher cryptography encryption and decryption algorithm.

Program Code

#include <stdio.h>

void encrypt(char msg[], int a, int b, char encrypted[])
{
    int i;

    for (i = 0; msg[i] != '\0'; i++) {
        if (msg[i] != ' ')
            encrypted[i] = (char)(((a * (msg[i] - 'A') + b) % 26) + 'A');
        else
            encrypted[i] = msg[i];
    }
    encrypted[i] = '\0';
}

void decrypt(char encrypted[], int a, int b, char decrypted[])
{
    int aInv = 0, flag = 0, i;

    for (i = 0; i < 26; i++) {
        flag = (a * i) % 26;
        if (flag == 1)
            aInv = i;
    }

    for (i = 0; encrypted[i] != '\0'; i++) {
        if (encrypted[i] != ' ')
            decrypted[i] = (char)(((aInv * ((encrypted[i] + 'A') - b)) % 26) + 'A');
        else
            decrypted[i] = encrypted[i];
    }
    decrypted[i] = '\0';
}

int main()
{
    char msg[100], encrypted[100], decrypted[100];
    int a = 5, b = 7;

    printf("Enter the message in upper case: ");
    scanf("%[^\n]s", msg);
    getchar();

    encrypt(msg, a, b, encrypted);
    printf("\nEncrypted text: %s\n", encrypted);

    decrypt(encrypted, a, b, decrypted);
    printf("\nDecrypted text: %s\n", decrypted);

    return 0;
}

Output




Saturday, September 19, 2020

Hill Cipher Implementation in C

Overview

Hill cipher is developed by a mathematician named Lester S. Hill in 1929. The encryption algorithm takes m successive plain text letters an substitutes for them, m cipher text letters. The substitution is determined by m linear equations in which each character is assignd a numerical value (a=0, b=1, c=3, ..., z=25). In general terms, the Hill system can be expressed as

C = E(K, P) = P*K mod 26

P = D(K, C) = C*K-1 mod 26 = P*K*K-1 = P

More about Hill Cipher

Aim

To write a C program to implement Hill Cipher cryptography encryption and decryption algorithm.

Program Code

#include <stdio.h>
#include <ctype.h>

void encrypt(int msgVector[][1], int keyMat[][3], int cipherMat[][1])
{
    for (int i = 0; i < 3; i++) {
        cipherMat[i][0] = 0;
        for (int j = 0; j < 3; j++) {
            cipherMat[i][0] = cipherMat[i][0] + keyMat[i][j] * msgVector[j][0];
        }
        cipherMat[i][0] = cipherMat[i][0] % 26;
    }
}

void decrypt(int cipherMat[][1], int keyInv[][3], int decryptMat[][1])
{
    for (int i = 0; i < 3; i++) {
        decryptMat[i][0] = 0;
        for (int j = 0; j < 3; j++) {
            decryptMat[i][0] = decryptMat[i][0] + keyInv[i][j] * cipherMat[j][0];
        }
        decryptMat[i][0] = decryptMat[i][0] % 26;
    }
}

int main()
{
    char msg[4], encrypted[4], decrypted[4];
    int keyMat[3][3], keyInv[3][3], msgVector[3][1], cipherMat[3][1], decryptMat[3][1];

    printf("Enter 3 letter message: ");
    for (int i = 0; i < 3; i++) {
        char c;
        scanf("%c", &c);
        msg[i] = toupper(c);
    }
    msg[3] = '\0';

    for (int i = 0; i < 3; i++) {
        msgVector[i][0] = msg[i] - 'A';
    }

    printf("Enter 3x3 key matrix...\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &keyMat[i][j]);
        }
    }

    encrypt(msgVector, keyMat, cipherMat);
    for (int i = 0; i < 3; i++) {
        encrypted[i] = (char)(cipherMat[i][0] + 'A');
    }
    encrypted[3] = '\0';
    printf("\nEncrypted text: %s\n", encrypted);

    printf("\nEnter 3x3 inverse of key matrix...\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &keyInv[i][j]);
        }
    }

    decrypt(cipherMat, keyInv, decryptMat);
    for (int i = 0; i < 3; i++) {
        decrypted[i] = (char)(decryptMat[i][0] + 'A');
    }
    decrypted[3] = '\0';
    printf("\nDecrypted text: %s", decrypted);

    return 0;
}

Output



Wednesday, September 16, 2020

2D Transformation Graphics Program in C

Aim

To write a C program to implement two dimensional transformations such as translation, rotation, scaling, shearing and reflection on objects.

Algorithm

Translation

  1. Input the line endpoints (x1, y1) and (x2, y2).
  2. Input the translation co-ordinates (tx, ty).
  3. Draw the line with endpoints (x1, y1) and (x2, y2).
  4. Add tx to the co-ordinates x1 and x2.
  5. Add ty to the co-ordinates y1 and y2.
  6. Draw the translated line with endpoints (x1, y1) and (x2, y2).



Rotation

  1. Input the line endpoints (x1, y1) and (x2, y2).
  2. Input the rotation angle Ɵ.
  3. Draw the line with endpoints (x1, y1) and (x2, y2).
  4. Calculate r = (Ɵ * 3.14) / 180.
  5. Calculate xn = x2 * cos(r) - y2 * sin(r).
  6. Calculate yn = y2 * cos(r) + x2 * sin(r).
  7. Draw the rotated line with endpoints (x1, y1) and (xn, yn).



Scaling

  1. Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
  2. Input the scaling factors sx and sy.
  3. Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
  4. Update x1, x2 and x3 by multiplying sx to them.
  5. Update y1, y2 and y3 by multiplying sy to them.
  6. Draw the scaled triangle with points (x1, y1), (x2, y2) and (x3, y3).



Shearing

  1. Input the 4 endpoints of the rectangle (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
  2. Input the value for shearing ‘sh’.
  3. Draw the rectangle with endpoints (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
  4. If shearing along X-axis, update x1, x2, x3 and x4 by adding (sh*y1), (sh*y2), (sh*y3) and (sh*y4) to them respectively.
  5. If shearing along Y-axis, update y1, y2, y3 and y4 by adding (sh*x1), (sh*x2), (sh*x3) and (sh*x4) to them respectively.
  6. Draw the sheared rectangle with endpoints (x1,y1), (x2, y2), (x3, y3), (x4, y4).

X axis shear


Y axis Shear



Reflection

  1. Input the 3 endpoints of the triangle (x1, y1), (x2, y2) and (x3, y3).
  2. Calculate middle value of X axis as ‘midx’ and Y axis as ‘midy’.
  3. Draw the triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).
  4. If reflection about X axis, set y1, y2 and y3 as (midy-y1) + midy, (midy-y2) + midy and (midy-y3) + midy respectively.
  5. If reflection about Y axis, set x1, x2 and x3 as (midx-x1) + midx, (midx-x2) + midx and (midx-x3) + midx respectively.
  6. Draw the reflected triangle with endpoints (x1, y1), (x2, y2) and (x3, y3).

Reflection about X axis


Reflection about Y axis


Program Code

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void main()
{
    int gd = DETECT, gm;
    int x1, y1, x2, y2, x3, y3, x4, y4, xn, yn, choice;
    int tx, ty, sx, sy, theta, axis, maxx, maxy, midx, midy;
    float rad, sh;
    clrscr();
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
    maxx = getmaxx();
    maxy = getmaxy();
    midx = maxx / 2;
    midy = maxy / 2;
    
    do
    {
        printf("1. Translation\n2. Rotation\n3. Scaling\n4. Shearing\n5. Reflection\n6. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        
        switch (choice)
        {
        case 1:
            printf("\nEnter two line endpoints (x1, y1), (x2, y2): ");
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            printf("Enter translation co-ordinates (tx, ty): ");
            scanf("%d %d", &tx, &ty);
            cleardevice();
            line(x1, y1, x2, y2);
            x1 += tx;
            y1 += ty;
            x2 += tx;
            y2 += ty;
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            getch();
            break;
        case 2:
            printf("\nEnter two line endpoints (x1, y1), (x2, y2): ");
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            printf("Enter the angle for rotation: ");
            scanf("%d", &theta);
            cleardevice();
            line(x1, y1, x2, y2);
            rad = (theta * 3.14) / 180;
            xn = x2 * cos(rad) - y2 * sin(rad);
            yn = y2 * cos(rad) + x2 * sin(rad);
            setcolor(GREEN);
            line(x1, y1, xn, yn);
            getch();
            break;
         case 3:
            printf("\nEnter the 1st point for the triangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the triangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the triangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("Enter the scaling factor (sx, sy): ");
            scanf("%d %d", &sx, &sy);
            cleardevice();
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            x1 *= sx;
            y1 *= sy;
            x2 *= sx;
            y2 *= sy;
            x3 *= sx;
            y3 *= sy;
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            getch();
            break;
        case 4:
            printf("\nEnter the 1st point for the rectangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the rectangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the rectangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("Enter the 4th point for the rectangle (x4, y4): ");
            scanf("%d %d", &x4, &y4);
            printf("1. X axis shearing\n2. Y axis shearing\n");
            printf("Enter your choice: ");
            scanf("%d", &axis);
            printf("Enter the value for shearing: ");
            scanf("%f", &sh);
            cleardevice();
            line(midx+x1, midy-y1, midx+x2, midy-y2);
            line(midx+x2, midy-y2, midx+x3, midy-y3);
            line(midx+x3, midy-y3, midx+x4, midy-y4);
            line(midx+x4, midy-y4, midx+x1, midy-y1);
            setcolor(3);
            line(0, midy, maxx, midy);
            line(midx, 0, midx, maxy);
            if (axis == 1) {
            x1 = x1 + (sh * y1);
            x2 = x2 + (sh * y2);
            x3 = x3 + (sh * y3);
            x4 = x4 + (sh * y4);
            }
            else {
            y1 = y1 + (sh * x1);
            y2 = y2 + (sh * x2);
            y3 = y3 + (sh * x3);
            y4 = y4 + (sh * x4);
            }
            setcolor(GREEN);
            line(midx+x1, midy-y1, midx+x2, midy-y2);
            line(midx+x2, midy-y2, midx+x3, midy-y3);
            line(midx+x3, midy-y3, midx+x4, midy-y4);
            line(midx+x4, midy-y4, midx+x1, midy-y1);
            getch();
            break;
        case 5:
            printf("\nEnter the 1st point for the triangle (x1, y1): ");
            scanf("%d %d", &x1, &y1);
            printf("Enter the 2nd point for the triangle (x2, y2): ");
            scanf("%d %d", &x2, &y2);
            printf("Enter the 3rd point for the triangle (x3, y3): ");
            scanf("%d %d", &x3, &y3);
            printf("1. Reflection about X axis\n2. Reflection about Y axis\n");
            printf("Enter your choice: ");
            scanf("%d", &axis);
            cleardevice();
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            setcolor(3);
            line(0, midy, maxx, midy);
            line(midx, 0, midx, maxy);
            if (axis == 1) {
            y1 = (midy - y1) + midy;
            y2 = (midy - y2) + midy;
            y3 = (midy - y3) + midy;
            }
            else {
            x1 = (midx - x1) + midx;
            x2 = (midx - x2) + midx;
            x3 = (midx - x3) + midx;
            }
            setcolor(GREEN);
            line(x1, y1, x2, y2);
            line(x2, y2, x3, y3);
            line(x3, y3, x1, y1);
            getch();
            break;
        }
        
        clrscr();
        cleardevice();
        setcolor(WHITE);
    
    } while (choice != 6);
    closegraph();
}

3D Transformation Graphics Program in C

Aim To write a C program to implement 3D transformations such as translation, rotation and scaling of objects. Algorithm: ·         Tr...