0% found this document useful (0 votes)
2 views2 pages

Corrected Code

The document contains a C program that implements an encryption algorithm using both an additive cipher for substitution and a columnar transposition for rearranging characters. The program prompts the user for plaintext, an additive key, and a transposition key, processes the input through the defined functions, and outputs the intermediate and final cipher text. It utilizes a matrix to perform the transposition based on the order of the characters in the key.

Uploaded by

mrinaldalvi.01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Corrected Code

The document contains a C program that implements an encryption algorithm using both an additive cipher for substitution and a columnar transposition for rearranging characters. The program prompts the user for plaintext, an additive key, and a transposition key, processes the input through the defined functions, and outputs the intermediate and final cipher text. It utilizes a matrix to perform the transposition based on the order of the characters in the key.

Uploaded by

mrinaldalvi.01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <string.h>

/* Substitution Function: Additive Cipher */


void substitution(char plain[], char sub[], int key) {
int i;
for (i = 0; plain[i] != '\0'; i++) {
sub[i] = ((plain[i] - 'A' + key) % 26) + 'A';
}
sub[i] = '\0';
}

/* Transposition Function: Columnar Transposition */


void transposition(char text[], char key[], char cipher[]) {
int i, j, k = 0;
int textLen = strlen(text);
int keyLen = strlen(key);
int rows = (textLen + keyLen - 1) / keyLen;

char matrix[rows][keyLen];
int order[keyLen];

/* Find column order */


for (i = 0; i < keyLen; i++) {
order[i] = 0;
for (j = 0; j < keyLen; j++) {
if (key[j] < key[i] || (key[j] == key[i] && j < i))
order[i]++;
}
}

/* Fill matrix row-wise */


for (i = 0; i < rows; i++) {
for (j = 0; j < keyLen; j++) {
if (k < textLen)
matrix[i][j] = text[k++];
else
matrix[i][j] = 'X';
}
}

k = 0;
/* Read columns based on key order */
for (i = 0; i < keyLen; i++) {
for (j = 0; j < keyLen; j++) {
if (order[j] == i) {
int r;
for (r = 0; r < rows; r++)
cipher[k++] = matrix[r][j];
}
}
}
cipher[k] = '\0';
}

/* Main Function */
int main() {
char plaintext[100], subOut[100], finalCipher[100];
char key[20];
int shift;

printf("Enter plaintext (UPPERCASE): ");


scanf("%s", plaintext);

printf("Enter additive key: ");


scanf("%d", &shift);

printf("Enter transposition key: ");


scanf("%s", key);

substitution(plaintext, subOut, shift);


transposition(subOut, key, finalCipher);

printf("\nAfter Substitution: %s", subOut);


printf("\nFinal Cipher Text: %s\n", finalCipher);

return 0;
}

You might also like