Saturday, September 16, 2017

PlayFair Cipher

PlayFair Cipher Java:

Code:

1-----> PlayFair.java



package com.nearur;

import java.awt.*;

public class PlayFair {
   
   private String key="Monarchy";
   private char[] keyarraay=key.toUpperCase().toCharArray();
   private char[] alpha={'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'};
   char[][] matrix=new char[5][5];
   StringBuffer buffer=new StringBuffer();

   public PlayFair(){
      int j=0,k=0;
      for (int i=0;i<keyarraay.length;i++){
         if(k!=5){
            if(!check(keyarraay[i])){
            matrix[j][k++]=keyarraay[i];
            }
         }else {
            i--;
            k=0;j++;
         }
      }

      for (int i=0;i<alpha.length;i++){
         if(k!=5){
            if(!check(alpha[i])){
               matrix[j][k++]=alpha[i];
            }
         }else {
            i--;
            k=0;j++;
         }
      }

   }

//  function to view matrix   public void show(){
      for (int i=0;i<5;i++) {
         for (int j = 0; j < 5; j++) {
            System.out.print(matrix[i][j]);
         }
         System.out.println();
      }
   }

// function to check for duplicate characters   private boolean check(char a){
      for (int i=0;i<5;i++) {
         for (int j = 0; j < 5; j++) {
            if(matrix[i][j]==a){
               return true;
            }
         }
      }
      return false;
   }


   // function to encrypt   public String encrypt(String msg){

      msg=msg.toUpperCase();
      msg=msg.replaceAll(" ","");
      msg=msg.replaceAll("J","I");
      if(msg.length()%2!=0){
         msg=msg.concat("X");
      }
      char[] msgs=msg.toCharArray();

      for (int i=0;i<msgs.length;i++){
         Point x1=find(msgs[i++]);
         Point x2=find(msgs[i]);

         if(x1.x==x2.x){
//same row            if(x1.y+1==5){
               x1.y=0;
            }
            else {
               x1.y++;
            }
            if(x2.y+1==5){
               x2.y=0;
            }else {
               x2.y++;
            }
            buffer.append(matrix[x1.x][x1.y]);
            buffer.append(matrix[x2.x][x2.y]);
         }else if(x1.y==x2.y){
//same column            if(x1.x+1==5){
               x1.x=0;
            }
            else {
               x1.x++;
            }
            if(x2.x+1==5){
               x2.x=0;
            }else {
               x2.x++;
            }
            buffer.append(matrix[x1.x][x1.y]);
            buffer.append(matrix[x2.x][x2.y]);
         }else{
            //diagonal            if(x2.x>x1.x) {
               int d=x2.x-x1.x;
               x2.x=x2.x-d;
               x1.x=x1.x+d;
            }else{
               int d=x1.x-x2.x;
               x2.x=x2.x+d;
               x1.x=x1.x-d;
            }
            buffer.append(matrix[x2.x][x2.y]);
            buffer.append(matrix[x1.x][x1.y]);

         }

      }

      return buffer.toString();
   }

   // function to decrypt   public String decrypt(String msg){

      buffer.delete(0,buffer.length());
      msg=msg.toUpperCase();

      char[] msgs=msg.toCharArray();

      for (int i=0;i<msgs.length;i++){
         Point x1=find(msgs[i++]);
         Point x2=find(msgs[i]);

         if(x1.x==x2.x){   //same row            if(x1.y-1<0){
               x1.y=4;
            }
            else {
               x1.y--;
            }
            if(x2.y-1<0){
               x2.y=4;
            }else {
               x2.y--;
            }
            buffer.append(matrix[x1.x][x1.y]);
            buffer.append(matrix[x2.x][x2.y]);
         }else if(x1.y==x2.y){   //same column
            if(x1.x-1<0){
               x1.x=4;
            }
            else {
               x1.x--;
            }
            if(x2.x-1<0){
               x2.x=4;
            }else {
               x2.x--;
            }
            buffer.append(matrix[x1.x][x1.y]);
            buffer.append(matrix[x2.x][x2.y]);
         }else{  //diagonal                        if(x2.x>x1.x) {
               int d=x2.x-x1.x;
               x2.x=x2.x-d;
               x1.x=x1.x+d;
            }else{
               int d=x1.x-x2.x;
               x2.x=x2.x+d;
               x1.x=x1.x-d;
            }
            buffer.append(matrix[x2.x][x2.y]);
            buffer.append(matrix[x1.x][x1.y]);

         }

      }

      return buffer.toString();
   }


// function  to return positon of character in matrix   private Point find(char c){
      Point p=null;
      for (int i=0;i<5;i++) {
         for (int j = 0; j < 5; j++) {
            if(matrix[i][j]==c){
               p=new Point(i,j);
               return p;
            }
         }
      }
      return p;
   }

}

2.-------> Encyption.java

package com.nearur;

public class Encryption {

   public static void main(String[] args) {


      PlayFair playFair=new PlayFair();
      String x=playFair.encrypt("key square");
      System.out.println("Cipher : "+x);
      System.out.println("Plain  : "+playFair.decrypt(x));

   }

}

Output:



For Project Click Here  (GitHub Link "https://github.com/mrdishant/Cryptography")

Sunday, September 3, 2017

CG3

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    setcolor(10);
    setbkcolor(15);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
    outtextxy(100,100,"HELLO WORLD!");
    setcolor(20);
    settextstyle(DEFAULT_FONT,HORIZ_DIR,5);
    outtextxy(100,200,"HELLO WORLD!");
    setcolor(25);
    settextstyle(BOLD_FONT,HORIZ_DIR,5);
    outtextxy(100,300,"HELLO WORLD!");
    getch();
}

Output:

CG2

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){
    
    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    line(10,90,300,90);
    setcolor(10);
    rectangle(10,100,300,250);
setcolor(30);
    circle(100,180,50);
setcolor(15);
    line(160,230,200,120);
    line(200,120,240,230);
    line(240,230,160,230);

    getch();
}

Output:

Number To Equivalent InWords String

Code:
package com.nearur;

import java.util.Scanner;

public class English {

String[] ones= {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","ninteen"};
String[] tens= {"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
StringBuffer buffer=new StringBuffer();

String c(int n) {
if(n<0) {
return "Minus "+c(-n);
}else if(n<20) {
return ones[n];
}else if(n<100) {
return tens[n/10]+" "+ones[n%10];
}else if(n<1000) {
return c(n/100)+" hundred "+c(n%100);
}else if(n<100000) {
return c(n/1000)+" thousand "+c(n%1000);
}else if(n<10000000) {
return c(n/100000)+" lakh "+c(n%100000);
}else if(n<1000000000) {
return c(n/10000000)+" crore "+c(n%10000000);
}

return "";
}

public static void main(String[] args) {
English a=new English();
Scanner in =new Scanner(System.in);
System.out.print("Enter Any Number : ");
int n=in.nextInt();
System.out.println(a.c(n).toUpperCase());

}

}

Output:


Friday, September 1, 2017

Name In Binary

Code:

package com.nearur;

import java.util.Scanner;

public class NTB {


static StringBuffer buffer=new StringBuffer();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter Your Name:");
String a=in.nextLine();
String[] name=a.split(" ");

for(String x:name) {
convert(x);
}

System.out.println("Binary Code is : "+buffer.toString());
}

static void convert(String name) {

char[] a=name.toCharArray();
for(char x: a) {
binary((int)x);
buffer.append(" ");
}

}
static void binary(int n) {
while(n>0) {
if(n%2==0) {
buffer.append("0");
}else {
buffer.append("1");
}
n=n/2;
}
}


}


Output:


Wednesday, August 30, 2017

Computer Graphics (PUT PIXEL)

CODE:


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){
int g=DETECT,gmode;
initgraph(&g,&gmode,"C://TURBOC3//BGI");
putpixel(100,100,10);
getch();
}


Output:




Tuesday, August 22, 2017

HangMan

Following Code is Hangman Like game without graphics inspired by Duta http:duta.in:


Code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
static String check="AEIOU";
public static void main(String[] args) {
ArrayList<String> questions=new ArrayList<>();
questions.add("ABRUPTLY");
questions.add("BOOKWORM");
questions.add("GALAXY");
questions.add("PNEUMONIA");
questions.add("STRONGHOLD");
questions.add("WHIZZING");
questions.add("PUZZLING");
questions.add("WRISTWATCH");
questions.add("ZODIAC");
questions.add("THUMBSCREW");
questions.add("YACHTSMAN");
questions.add("MNEMONIC");
questions.add("TRANSPLANT");
questions.add("RHYTHM");
questions.add("MICROWAVE");
Collections.shuffle(questions);
String ques=questions.get(0);
   char[] given=new char[ques.length()];
   int count=0;
   for(int i=0;i<given.length;i++) {
    if(varcheck(ques.charAt(i))) {
    given[i]=ques.charAt(i);
    }else {
    given[i]='_';
    }
   }
   boolean win=false;
   Scanner in=new Scanner(System.in);
   StringBuffer buffer=new StringBuffer();
outer:
   while(count<7 && !win) {
    for(int i=0;i<given.length;i++) {
    System.out.print(given[i]+" ");
    }
    System.out.println();
    System.out.println("Guess a Letter");
    char c=in.nextLine().toUpperCase().charAt(0);
    int i=0;
    while(i<given.length) {
    if(given[i]==c) {
    System.out.println("Already Present in Word!!");
    System.out.println("Guessed Letters : "+buffer);
    System.out.println("Chances left : "+(7-count));
    System.out.println("------------------------------");
    continue outer;
    }i++;
    }
    if(buffer.toString().contains(c+"")) {
    System.out.println("Already Guessesed!!");
    System.out.println("Guessed Letters : "+buffer);
    System.out.println("Chances left : "+(7-count));
    System.out.println("------------------------------");
    continue outer;
    }
    if(ques.contains(c+"")) {
    int idx=ques.indexOf(c);
    while(idx>=0) {
    given[idx]=c;
    idx=ques.indexOf(c,idx+1);
    win=true;
    }
    System.out.println("Good Guess!!");
    }else {
    count++;
    System.out.println("Bad Guess!!");
    }
    buffer.append(c+",");
    System.out.println("Guessed Letters : "+buffer.toString().substring(0,buffer.length()-1));
    System.out.println("Chances left : "+(7-count));
    for(char x: given) {
    if(x=='_') {
    win=false;
    }
    }
    System.out.println("--------------------------------\n");
   }
    System.out.println("Word is : "+ques);
   if(win) {
    System.out.println("Congratulations,You win!!");
   }else {
    System.out.println("You Loose!!,Don't Worry Try Again");
   }
   in.close();
}
static boolean varcheck(char c) {
if(check.contains(c+"")) return true;
return false;
}

}

Output: