Find us on Google+ Kill the code: cipher
Showing posts with label cipher. Show all posts
Showing posts with label cipher. Show all posts

Sunday, 24 February 2013

Rail Fence Cipher in JAVA

In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows.
 
This is program for Rail Fence Cipher JAVA.

public class railfence {
    public static void main(String args[])
    {
        String input = "inputstring";
        String output = "";
        int len = input.length(),flag = 0;

        System.out.println("Input String : " + input);
        for(int i=0;i<len;i+=2) {
           
            output += input.charAt(i);
        }
        for(int i=1;i<len;i+=2) {
           
            output += input.charAt(i);
        }
       
        System.out.println("Ciphered Text : "+output);
    }
}







Saturday, 18 February 2012

Vigener Cipher in C


// WAP to implement Vignere cipher in C.

#include<conio.h>
#include<stdio.h>
void main()
{
  int i,j,b=65,c=65,d=65,t[20];
  char s[20],a[26][26];
  clrscr();
  for(i=0;i<26;i++)
  {
    for(j=0;j<26;j++)
    {
      if(b<91)
      {
        a[i][j]=b;
        b++;
      }
      else
      {
        a[i][j]=d;
        d++;


      }
    }
    c++;
    b=c;
    d=65;
  }
  for(i=0;i<26;i++)
  {
    for(j=0;j<26;j++)
    {
      printf("%c",a[i][j]);
    }
    printf("\n");
  }
  printf("\nEnter the text : ");
  gets(s);
  for(i=0;i<strlen(s);i++)
  {
    t[i]=s[i]-65;
  }
  for(i=0;i<strlen(s);i++)
  {
    s[i]=(a[t[i]][i]);
  }
  for(i=0;i<strlen(s);i++)
  {
    printf("%c",s[i]);
  }
  getch();
}


/*OUTPUT


Enter the text : 


D E A R
 |   |  |  |
 |   |  |  |
 |   |  |  |           // Cipher text is : 
D F C V
 |  |   |  |
 |  |   |  |
 |  |   |  |
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 Z 
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
C D E F G H I J K L M N O P Q R S T U V W X Y Z A B 
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 
E F G H I J K L M N O P Q R S T U V W X Y Z A B C D 
F G H I J K L M N O P Q R S T U V W X Y Z A B C D E 
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F 
H I J K L M N O P Q R S T U V W X Y Z A B C D E F G 
 I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
J K L M N O P Q R S T U V W X Y Z A B C D E F G H I 
K L M N O P Q R S T U V W X Y Z A B C D E F G H I J 
L M N O P Q R S T U V W X Y Z A B C D E F G H I J K 
M N O P Q R S T U V W X Y Z A B C D E F G H I J K L 
N O P Q R S T U V W X Y Z A B C D E F G H I J K L M 
O P Q R S T U V W X Y Z A B C D E F G H I J K L M N 
P Q R S T U V W X Y Z A B C D E F G H I J K L M N O 
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P 
R S T U V W X Y Z A B C D E F G H I J K L M N O P Q 
S T U V W X Y Z A B C D E F G H I J K L M N O P Q R 
T U V W X Y Z A B C D E F G H I J K L M N O P Q R S 
U V W X Y Z A B C D E F G H I J K L M N O P Q R S T 
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U 
W X Y Z A B C D E F G H I J K L M N O P Q R S T U V 
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W 
Y Z A B C D E F G H I J K L M N O P Q R S T U V W X 
Z 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 


 */

Friday, 17 February 2012

Rail Fence Cipher in C



#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k=0,l=0,m=0;
char s[20],a[10],b[10];
clrscr();
printf("enter a string:");
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if(i%2==0)
{
a[k]=s[i];
k++;
}
else
{
b[l]=s[i];
l++;
}
}


for(i=0;i<k;i++)
{
printf("%c ",a[i]);
s[m]=a[i];
m++;
}
printf("\n");
for(i=0;i<l;i++)
{
printf(" %c",b[i]);
s[m]=b[i];
m++;
}
printf("\n\ncipher text is %s",s);
getch();
}

Monday, 6 February 2012

Hill Cipher in C

Just paste this program in your Turbo C/C++ compiler...



#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,ans[25][1],sum=0,mtrx[25][25],end;
char txt[25];
clrscr();
printf("\nEnter the string : ");
scanf("%s",txt);
//clrscr();
for(i=0;i<25;i++)
{
if(txt[i]>=97 && txt[i]<122) {}
else
{
end=i;
break;
}
}
for(i=0;i<end;i++)
{
//printf("initial : %d ",txt[i]);
txt[i]=txt[i]-'a';
//printf("final : %d ",txt[i]);
//printf("\n\n");
}
clrscr();
printf("\nEnter matrix...\n");
for(i=0;i<end;i++)
{
for(j=0;j<end;j++)
{
scanf("%d",&mtrx[i][j]);
}
}
for(i=0;i<end;i++)
{
sum=0;
for(j=0;j<end;j++)
{
sum+=mtrx[i][j]*(int)txt[j];
}
ans[i][0]=sum;
}
for(i=0;i<end;i++)
{
printf(" %c",((ans[i][0])%26)+97);
}
getch();
}