Showing posts with label cryptography. Show all posts
Showing posts with label cryptography. Show all posts

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





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



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...