Search This Blog

Showing posts with label ciphertext. Show all posts
Showing posts with label ciphertext. Show all posts

Tuesday, 2 February 2016

Rail-Fence Cipher Program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=1000;

class Matrix
{
int d,l;
char a[size][size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
l=strlen(x);
int flag=1;
cout << "\t\tEnter depth of the text:";
cin >> d;
int j=0;
for(int i=1;i<=l;i++)
{
int flag2=0;
for(int k=0;k<d;k++)
{
if(j==k && flag2==0)
{
a[j][i-1]=x[i-1];
if(i%d==0)
flag=flag*-1;
j+=flag;
if(j<0)
j=1;
flag2=1;
}
else
a[k][i-1]='$';
}
}
}

void Matrix::display()
{
for(int i=0;i<d;i++)
{
for(int j=0;j<l;j++)
{
if(a[i][j]!='$')
cout << a[i][j];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}

enter string :
abcdef

a        e
  b   d    f
    c
enter depth :
3

aebdfc

Monday, 18 January 2016

Cipher text example with file handling

 Simple Cipher text with file Handling
Input : hello karan gajjar
key : 3
Output : khoor ndudq jdmmdu

As we know encryption and decryption. I have kept input in encode.txt file and aftyer entering key value the decrypted text will be stored in decode.txt file!

CODE:

 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,key,k;
    char str[100],str2[100];
    FILE *fp,*fq;
    fp=fopen("encode.txt","r");
    fgets(str,sizeof(str),fp);
    puts(str);
    fq=fopen("decode.txt","a");
    clrscr();
    printf("Enter Key:");
    scanf("%d",&key);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>=97 && str[i]<120)
        {
            str[i]=str[i]+key;
            str2[i]=str[i];
        }
        else if(str[i]==32)
        {
            str2[i]=str[i];
        }
        else
        {
            str[i]=str[i]-26+key;
            str2[i]=str[i];
        }
    }
    str2[i]='\0';
    puts(str2);
    fprintf(fq,"\n");
    fprintf(fq,str2);
    fclose(fq);
    fclose(fp);
    getch();
}


NOTE: You can add more elseif condition for "," , "!" , "?" this type of ascii value i have added for "space".
              and yes in this example you can't add encoded message with every new line!