Showing posts with label Cryptography. Show all posts
Showing posts with label Cryptography. Show all posts

Sunday, March 16, 2014

Caesar Cipher Encryption Decryption Java Source code

Written by Pranshu Bajpai |  | LinkedIn

Source code for Caesar Cipher Encryption




package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pranshu
 */
public class DICAss12_CaesorCipherEnc {
    public static void main(String[] args)
    {
        String Msg = "IAMUNDERATTACKW";
        int Key = 3;
        
        String CTxt = "";
        
        int temp;
        char c;
        for(int i = 0 ; i < Msg.length() ; i++){
            if (Msg.charAt(i) + Key > 90){
                temp = (Msg.charAt(i) + Key) -26;
            }
            else{
                temp = (Msg.charAt(i) + Key); 
            }
            
            c = (char) temp;
            CTxt = CTxt + c;
        }        
        
        System.out.println(CTxt);
    }
}

Source Code for Caesar Cipher Decryption

package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Pranshu
 */
public class DICAss13_CaesorCipherDec {
    public static void main(String[] args)
    {
        String CTxt = "LDPXQGHUDWWDFNZC";
        int Key = 3;
        
        String Msg = "";
        
        int temp, num;
        char c;
        for(int i = 0 ; i < CTxt.length() ; i++){
            num = ((CTxt.charAt(i) - Key) - 64);
            if ( num <= 0){
                temp = (90 - (num * (-1))%26);
            }
            else {
                temp = (( num % 26) + 64);
            }
            
            c = (char) temp;
            Msg = Msg + c;
        }              
        System.out.println(Msg);
    }
}

Sunday, March 31, 2013

Cryptography with Java : Program Source Codes

5. For, While and Do While Loops in Java :


import java.io.*;
import java.util.*;
/**
 *
 * @author pranshu
 */
public class Loops_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hello, Welcome to Pranshu's loops...");
        System.out.println("\nHow many times do you want to loop??  ");
        Scanner scn = new Scanner(System.in);
        int times = scn.nextInt();
        System.out.println("You Entered " + times);
        for(int i = 1; i <= times ; i++)
            System.out.println("Inside For Loop, The no. is " + i );
        int i = 1;
        do
        {
            System.out.println("Inside Do-While Loop, The no. is " + i );
            i++;
        } while(i <= times);
        
        i = 1;
        while( i <= times)
        {
            System.out.println("Inside While Loop, The no. is " + i );
            i++;
        }
    }
    
    
}


6. Conversion From Decimal to Binary in Java, Source code:



/**
 *
 * @author root
 */
public class conversions_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter a Decimal : ");
       BufferedReader brobj = new BufferedReader (new InputStreamReader(System.in));
        String dec = brobj.readLine();
        int dec2 = Integer.parseInt(dec);
        String bin = Integer.toBinaryString(dec2);
        System.out.println("The Binary is : " + bin);
    }
   
} 
 
 

7. Search for a Word in Text or String, Java Source code :

 
 
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
/**
 *
 * @author pranshu
 */
public class matchtext {
    
   
    
    public static void main(String[] args)
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Text: ");
    try
    {
    String text = br.readLine();
    System.out.println("Enter the word to match : ");
    String word = br.readLine();
    String[] strarr = text.split(" ");
    int flag =0;
    for(String str:strarr)
    {
    if(str.equalsIgnoreCase(word))
    {
        System.out.println(word + " exists in " + text);    
        flag = 1;
    }
    }
    if(flag == 0)
        System.out.println("No match found");
    
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    } 
 
 

8. File Handling Program in Java : Write to a File: 

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
 *
 * @author pranshu
 */
public class filehandle_pranshu {
    
    public static void main(String[] args) throws IOException
    {
          String com = "comment";
          String name = "uname";
 
          String ipadd = "127.0.0.1" ;

          FileWriter fstream = new FileWriter("/root/comments.txt",true);
          BufferedWriter fbw = new BufferedWriter(fstream);
            
            fbw.write(name + "__" + ipadd + ": ");
            fbw.write(com);
            fbw.newLine();
            System.out.println("Done Writing...");
            System.out.println("Closing File now...");
            fbw.close();
        
    }
    
}

Java Basic Programs With Source Code and Comments

Before starting with Cryptography in Java, these are some of the basic programs that I built quickly to brush up. 


1. Hello Java Source Code : 


            public static void main(String[] args) {

                System.out.println("Hello Java ! ~ ! ~");

        }

       

    }


2. Simple Input/Output Program Source Code :


    import java.io.IOException;

    import java.io.InputStreamReader; 

    public class InputOutput_pranshu {

        public static void main(String[] args) throws IOException

        {

            System.out.println("Hello, What's your name??  ");

            BufferedReader BRobj = new BufferedReader(new InputStreamReader(System.in) );

            String name = BRobj.readLine();

            System.out.println("Hello " + name + ", have a nice day");

        }

       

    }



3.  If Else Decision Making Java Source code :


 /**
 *
 * @author pranshu
 */
 public class IfElse_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hello, Please Enter your name : ");
        BufferedReader brobj = new BufferedReader(new InputStreamReader(System.in)); 
        String name = brobj.readLine();
        if("pranshu".equals(name))
        {
         System.out.println("Hello Pranshu, Please Enter your Password : ");
         String pass = brobj.readLine();
            if("java".equals(pass))
                System.out.println("Welcome Pranshu, Access Granted!!");
            else
                System.out.println("Access Denied, Wrong Password");
        }
         else
            System.out.println("Sorry, this User is NOT Allowed Access");
           
    }
            
}

4. Convert A Char to its ASCII value, Java Source Code :


/**
 *
 * @author root
 */
public class Ascii_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter the Char you want to convert to ASCII :");
        BufferedReader BRobj = new BufferedReader(new InputStreamReader(System.in));
        String text = BRobj.readLine();
        int c = text.charAt(0);
        System.out.println("The ASCII Code is " + c);
    }
    }