Copyright © infotec016 . Powered by Blogger.

Thursday, April 20, 2017

Cryptography


Crypto System

Cryptographic system are characterized along 3 independent dimensions


  • The type of operation transforming plain text to cipher text
  • The number of keys used
  • The way in which plain text is processed.
Substitution techniques
  • Caesar Cipher
  • Monoalphabatic Cipher
  • Playfair Cipher
  • Hill Cipher
  • Polyalphabatic Cipher

Transposition techniques
  • Rail fence Cipher
  • Columnar transposition 



Sample java code for crypto System

Secret key: Representing reverse order of alphabet
Encryption

Example: hello(plain text)-----------svool(cipher text)   


import java.util.*;
public class Encrypt{
                public static void main(String args[]){
                                Scanner myScanner=new Scanner(System.in);
                                String alpha="abcdefghijklmnopqrstuvwxyz";
                                String word;
                                System.out.println("Enter the plain text");
                                word=myScanner.nextLine().toLowerCase();
                                System.out.println("Cipher text");
                                for(int i=0;i<word.length();i++){
                                                boolean found=false;
                                                char a=word.charAt(i);
                                                int j=0;
                                                while(j<alpha.length() &&  found==false){
                                                                char b=alpha.charAt(j);
                                                                if(a==' '){
                                                                                System.out.print(" ");
                                                                                found=true;
                                                                }
                                                                else if(a==b ){
                                                                found=true;
                                                                                System.out.print(alpha.substring(alpha.length()-j-1,alpha.length()-j));
                                                                }
                                                                j++;
                                                   }
                                }
                  }
}                                                                                                                                                                    

Decryption

Example: svool(cipher text)-----------hello(plain text)   

import java.util.*;
public class Decrypt{
                public static void main(String args[]){
                                Scanner myScanner=new Scanner(System.in);
                                String alpha=" zyxwvutsrqponmlkjihgfedcba ";
                                String word;
                                System.out.println("Enter the cipher text");
                                word=myScanner.nextLine().toLowerCase();
                                System.out.println("original text");
                                for(int i=0;i<word.length();i++){
                                                boolean found=false;
                                                char a=word.charAt(i);
                                                int j=0;
                                                while(j<alpha.length() &&  found==false){
                                                                char b=alpha.charAt(j);
                                                                if(a==' '){
                                                                                System.out.print(" ");
                                                                                found=true;
                                                                }
                                                                else if(a==b ){
                                                                found=true;
                                                                                System.out.print(alpha.substring(alpha.length()-j-1,alpha.length()-j));
                                                                }
                                                                j++;
                                                }
                                }
                }
}


                                  

0 comments:

Post a Comment