Pages

Monday, December 26, 2016

Security - Rail Fence (complex) Decryption using C program.

This C program demonstrate how to decrypt given cipher text using Rail fence (complex) technique.

Assumptions:

1. 5 X 5 matrix is used.
2. Key = 21345

Sample Output expected:

RAIL FENCE DECRYPTION

Cipher text: BGLQVAFKPUCHMRWDINSXEJOTY

Key Used = 2 1 3 4 5

Plain text: ABCDEFGHIJKLMNOPQRSTUVWXY


* * * * *

* * * * *
/*
Security - Decryption using Rail fence (complex) technique.

Assumptions:
1. 5 column matrix is used.
2. Key = 21345
*/
#include<stdio.h>
int main()
{
  char str[5][5], plaintext[26];   
  char ciphertext[26]="BGLQVAFKPUCHMRWDINSXEJOTY";
  int i,j,k,key[5]={2,1,3,4,5}, cnt=0, len;

  len = strlen(ciphertext);
  printf("RAIL FENCE DECRYPTION\n\n");
  printf("Input Data: BGLQVAFKPUCHMRWDINSXEJOTY\n\n");
  printf("Key Used = 2 1 3 4 5 \n\n");

  //Writing Cipher text in str[][] matrix.
  for(i=0; i<len/5; i++)
  {
k=key[i]-1;
for(j=0; j<5; j++)
str[j][k]=ciphertext[cnt++];
  }
  printf("Ciphertext arranged in matrix for key:21345\n\n");
  for(i=0; i<len/5; i++)
  {
for(j=0; j<5; j++)
printf("%c ",str[i][j]);
printf("\n");
  }
  printf("\nPlain text: ");
  for(i=0; i<len/5; i++)
  {
for(j=0; j<5; j++)
printf("%c",str[i][j]);
  }
  return 0;
}

//Data can be taken from cipher.txt data file.
//Plain text can be stored into plain.txt.
//You can make this program more dynamic by using file management.
/*
Output

RAIL FENCE DECRYPTION

Input Data: BGLQVAFKPUCHMRWDINSXEJOTY

Key Used = 2 1 3 4 5

Ciphertext arranged in matrix for key:21345

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y

Plain text: ABCDEFGHIJKLMNOPQRSTUVWXY
*/

No comments:

Post a Comment