JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
about 1 month ago1 views
👨‍💻Programming

Ceaser Cipher

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int key;
char text[100],cipher[100],decipher[100];

void enc(char text[],int key);
void dec(char text[],int key);

int main(){
	printf("Enter the Message:");
	//scanf("%s",text);
	fgets(text,sizeof(text),stdin);
	
	printf("Enter the Key:");
	scanf("%d",&key);
	
	int ch;
	printf("1.Encryption\n");
	printf("2.Decryption\n");
	printf("3.Exit\n");
	
	
	while(1){
	printf("Choose a operation:");
	scanf("%d",&ch);
		switch(ch){
			case 1: enc(text,key);
			 		break;
			 		
			case 2: dec(cipher,key);
					break;
			case 3: exit(0);
			
		}
	}
	
	return 0;
}

void enc(char text[],int key){
	int n=strlen(text);
	
	for(int i=0;i<n;i++){
		if(text[i]>='A' && text[i]<='Z')
			cipher[i]=(((text[i]-'A'+key)%26)+'A');
		else if(text[i]>='a' && text[i]<='z')
			cipher[i]=(((text[i]-'a'+key)%26)+'a');
		else 
			cipher[i]=text[i];
			
	}
	cipher[n]='\0';
	printf("Cipher Text:%s",cipher);
}


void dec(char text[],int key){
	int n=strlen(text);
	
	for(int i=0;i<n;i++){
		if(text[i]>='A' && text[i]<='Z')
			decipher[i]=(((text[i]-'A'-key+26)%26)+'A');
		else if(text[i]>='a' && text[i]<='z')
			decipher[i]=(((text[i]-'a'-key+26)%26)+'a');
		else 
			decipher[i]=text[i];
			
	}
	decipher[n]='\0';
	printf("Decrypted Text:%s",decipher);
}

Vigenère cipher

#include<stdlib.h>
char message[100];
char ciphertext[100];
void encrypt(char key[],int n)
{
    for(int i = 0; message[i]!='\0';i++)
    ciphertext[i] = ((message[i]-'a')+(key[i%n]-'a'))%26+'a';
    printf("%s  \n",ciphertext);
}
int main()
{
    char key[100];
    int n;
    printf("Enter the size of the key: ");
    scanf("%d",&n);
    getchar();
    printf("Enter the key: ");
    fgets(key, sizeof(key), stdin);
    key[strcspn(key, "\n")] = '\0';
    printf("Enter the plaintext: ");
    fgets(message, sizeof(message), stdin);
    message[strcspn(message, "\n")] = '\0';
    encrypt(key,n);
    return 0;
}

Playfair

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 
int matrix[5][5], row = 0, col = 0;
char cipher[100], text[100], paired[100];
 
void genmat(char key[]) {
    int i, k = 0;
    int used[26] = {0};
 
    for (i = 0; key[i]; i++) {
        key[i] = toupper(key[i]);
        if (key[i] == 'J') key[i] = 'I';
 
        if (!used[key[i] - 'A']) {
            matrix[k / 5][k % 5] = key[i];
            used[key[i] - 'A'] = 1;
            k++;
        }
    }
 
    for (i = 0; i < 26; i++) {
        if (i + 'A' == 'J') continue;
        if (!used[i]) {
            matrix[k / 5][k % 5] = i + 'A';
            k++;
        }
    }
}
 
void pos(char c) {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (matrix[i][j] == c) {
                row = i;
                col = j;
                return;
            }
        }
    }
}
 
void process(char text[]) {
    int i = 0, j = 0;
    char a, b;
 
    while (text[i]) {
        a = toupper(text[i]);
        if (a == 'J') a = 'I';
        b = toupper(text[i + 1]);
        if (b == 'J') b = 'I';
 
        paired[j++] = a;
 
        if (text[i + 1] && a == b) {
            paired[j++] = 'X';
            i++;
        } else if (text[i + 1]) {
            paired[j++] = b;
            i += 2;
        } else {
            i++;
        }
    }
 
    if (j % 2 != 0)
        paired[j++] = 'X';
 
    paired[j] = '\0';
}
 
void encrypt(char paired[]) {
    int r1, c1, r2, c2, i;
 
    for (i = 0; paired[i]; i += 2) {
        pos(paired[i]);
        r1 = row;
        c1 = col;
 
        pos(paired[i + 1]);
        r2 = row;
        c2 = col;
 
        if (r1 == r2) {
            cipher[i]     = matrix[r1][(c1 + 1) % 5];
            cipher[i + 1] = matrix[r2][(c2 + 1) % 5];
        } else if (c1 == c2) {
            cipher[i]     = matrix[(r1 + 1) % 5][c1];
            cipher[i + 1] = matrix[(r2 + 1) % 5][c2];
        } else {
            cipher[i]     = matrix[r1][c2];
            cipher[i + 1] = matrix[r2][c1];
        }
    }
 
    cipher[i] = '\0';
}
 
int main() {
    char key[50];
 
    printf("Enter the Key: ");
    scanf("%s", key);
 
    printf("Enter the PlainText: ");
    scanf("%s", text);
 
    genmat(key);
 
    printf("\nKey Matrix:\n");
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++)
            printf("%c ", matrix[i][j]);
        printf("\n");
    }
 
    process(text);
    encrypt(paired);
 
    printf("\nCipher Text: %s\n", cipher);
 
    return 0;
}

Hill cipher

#include <stdio.h>

#include <string.h>
 

int key[3][3], invKey[3][3];
char plaintext[200], ciphertext[200];
 


int modInverse(int a) {
    a = (a % 26 + 26) % 26;
    for (int i = 1; i < 26; i++)
        if ((a * i) % 26 == 1)
            return i;
    return -1;
}
 


void calculateInverseKey() {
    int det =
        key[0][0]*(key[1][1]*key[2][2] - key[1][2]*key[2][1]) -
        key[0][1]*(key[1][0]*key[2][2] - key[1][2]*key[2][0]) +
        key[0][2]*(key[1][0]*key[2][1] - key[1][1]*key[2][0]);
 

    det = (det % 26 + 26) % 26;
    int invDet = modInverse(det);
 

    if (invDet == -1) {
        printf("Key matrix is NOT invertible!\n");
        return;
    }
 

    invKey[0][0] = (key[1][1]*key[2][2] - key[1][2]*key[2][1]) * invDet % 26;
    invKey[0][1] = -(key[0][1]*key[2][2] - key[0][2]*key[2][1]) * invDet % 26;
    invKey[0][2] = (key[0][1]*key[1][2] - key[0][2]*key[1][1]) * invDet % 26;
 

    invKey[1][0] = -(key[1][0]*key[2][2] - key[1][2]*key[2][0]) * invDet % 26;
    invKey[1][1] = (key[0][0]*key[2][2] - key[0][2]*key[2][0]) * invDet % 26;
    invKey[1][2] = -(key[0][0]*key[1][2] - key[0][2]*key[1][0]) * invDet % 26;
 

    invKey[2][0] = (key[1][0]*key[2][1] - key[1][1]*key[2][0]) * invDet % 26;
    invKey[2][1] = -(key[0][0]*key[2][1] - key[0][1]*key[2][0]) * invDet % 26;
    invKey[2][2] = (key[0][0]*key[1][1] - key[0][1]*key[1][0]) * invDet % 26;
 

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            invKey[i][j] = (invKey[i][j] + 26) % 26;
}
 


void encrypt() {
    int len = strlen(plaintext);
 

    while (len % 3 != 0) {
        plaintext[len++] = 'X';
        plaintext[len] = '\0';
    }
 

    for (int i = 0; i < len; i += 3) {
        for (int j = 0; j < 3; j++) {
            ciphertext[i + j] =
                (key[j][0]*(plaintext[i]-'A') +
                 key[j][1]*(plaintext[i+1]-'A') +
                 key[j][2]*(plaintext[i+2]-'A')) % 26 + 'A';
        }
    }
    ciphertext[len] = '\0';
    printf("Encrypted Message: %s\n", ciphertext);
}
 


void decrypt() {
    int len = strlen(ciphertext);
 

    for (int i = 0; i < len; i += 3) {
        for (int j = 0; j < 3; j++) {
            plaintext[i + j] =
                (invKey[j][0]*(ciphertext[i]-'A') +
                 invKey[j][1]*(ciphertext[i+1]-'A') +
                 invKey[j][2]*(ciphertext[i+2]-'A')) % 26 + 'A';
        }
    }
    plaintext[len] = '\0';
    printf("Decrypted Message: %s\n", plaintext);
}
 

int main() {
    int choice;
 

    printf("Enter 3x3 Key Matrix (invertible mod 26):\n");
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf("%d", &key[i][j]);
    printf("\nEntered matrix is:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%3d ", key[i][j]);
        }
        printf("\n");
    }
    calculateInverseKey();
 

    while (1) {
        printf("\n--- HILL CIPHER MENU ---\n");
        printf("1. Encrypt\n");
        printf("2. Decrypt\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
 

        switch (choice) {
            case 1:
                printf("Enter plaintext (UPPERCASE): ");
                scanf("%s", plaintext);
                encrypt();
                break;
 

            case 2:
                decrypt();
                break;
 

            case 3:
                printf("Exiting...\n");
                return 0;
 

            default:
                printf("Invalid choice!\n");
        }
    }
}

← Back to timeline