(k-Rail Fence Cipher): The rail fence cipher is a transposition cipher, where a
ID: 3750919 • Letter: #
Question
(k-Rail Fence Cipher): The rail fence cipher is a transposition cipher, where a plaintext message is enciphered into a ciphertext message by performing some sort of permutation on the plaintext letters. Using this encryption technique, a plaintext is written down as a sequence of diagonals and then reads off as a sequence of rows. For example, to encipher the message “Hello CISC 3580 Students!” with a rail fence of depth 2, we write the following: H l o I C 5 0 t d n s e l C S 3 8 S u e t
The resulting ciphertext is: HloIC50tdnselCS38Suet
The decryption function is performed as follows: - Compute the length of ciphertext. Let m be this length. - Let k be the number of rails (i.e., key). Reserve m placeholders on the k-rail fence in the same way a plaintext was placed on a k-rail fence. - Assign the ciphertext letters to those placeholders’ row by row, starting from the top one. - Read off the resulting plaintext as a sequence of diagonals. 1. Write an algorithm for a k-rail fence cipher, where k 2. 2. Show that your proposed algorithm is correct using an informal proof (i.e., discussion). 3. Give a program corresponding to your proposed algorithm, using your favorite programming language (preferably C++). Your program should simulate this cipher and display the ciphertext along with the corresponding plaintext and key. The implementation of this k-rail fence cipher software should be as structured as possible.
Explanation / Answer
Algorithm for K- rail fence cipher
Example : consider a plain text "I AM A STUDENT"
Arrange it as: I M S U E T
A A T D N
Cipher text is:IMSUETAATDN
Program for rail fence cipher
#include<stdio.h>
#include<string.h>
Void enmsg(char msg[],int key)
{
int mlen = strlen(msg),i,j,k=-1,row=0,col=0;
Char Matrix[key][mlen];
for(i=o;i<key;++i)
for(j=0;j<mlen;j++)
railMatrix[i][j]=' ';
for(i=0;i<mlen;i++)
{
railMatrix[row][col++]=msg[i];
If(row==0||rowkey-1)
Key=key*(-1);
row =row+k;
}
Printf(" encrypted cipher text :");
for(i=0;i<key;++i)
for(j=0;j<mlen;++j)
If(railMatrix[i][j]!=' ')
Printf("%c",railMatrix[i][j]);
}
void decmsg(char enmsg, int key)
{
int mlen=strlen(enmsg),I, j, k=-1,row=0,col=0,m=0;
char railMatrix[key][mlen];
for(i=0;i<key;++i)
for(j=0;j<mlen;++j)
railMatrix[i][j]=' ';
for(i=0;i<mlen;i++)
{
railMatrix[row][col++]='*';
if(row==0||row=key-1)
k=k*(-1);
row=row+k;
}
for(i=0;i<key;++i)
for(j=0:j<mlen;++j)
If(railMatrix[i][j]=enmsg[m++];
row=col=0;
k=-1;
Printf("decrypted message: ");
for(i=0;i<mlen;++I)
{
Printf("%c",railMatrix[row][col++]);
if(row==0||row=key-1)
k=k*(-1);
row=row+k;
}
int main(){
char msg[]="IM A STUDENT":
char enmsg[]="IMSUETAATDN";
interesting key =3;
printf("original msg :%",msg);
enmsg(msg, key) ;
decmsg(enmsg,key);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.