Showing posts with label Java. Show all posts
Showing posts with label Java. 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);
    }
}

Binary Hex and Octal Conversion in Java | Example | Source code

Written by Pranshu Bajpai |  | LinkedIn

Source Code



package DICAss;

class DICAss7_Conversion
{
	public static void main(String[] args)
	{
		String binary = "111";
		String hex = "A";
		String oct = "772";

		int decimal1 = Integer.parseInt(binary, 2);

		int decimal2 = Integer.parseInt(hex, 16);

		int decimal3 = Integer.parseInt(oct, 8);

		System.out.println(decimal1);
		System.out.println(decimal2);
		System.out.println(decimal3);

	}	
}

Simple Switch Case in Java | Example | Source code

Written by Pranshu Bajpai |  | LinkedIn

Source Code



package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
/**
 *
 * @author Pranshu
 */
public class DICAss6_SwitchCase {
    public static void main(String[] args){
        
        int ch;
        
        BufferedReader BRObj = new BufferedReader
                (new InputStreamReader(System.in));
        
        System.out.print("Give Input : ");
        
        try{
            ch = Integer.parseInt(BRObj.readLine());
            
            switch (ch)
                    {
                        case 1:
                            System.out.println("Jan");
                        break;
                       
                        case 2:
                            System.out.println("Feb");
                        break;
                            
                        case 3:
                            System.out.println("Mar");
                        break;
                            
                        case 4:
                            System.out.println("Apr");
                        break;
                            
                        case 5:
                            System.out.println("May");
                        break;
                        
                        case 6:
                            System.out.println("Jun");
                        break;
                            
                        case 7:
                            System.out.println("July");
                        break;
                        
                        case 8:
                            System.out.println("Aug");
                        break;
                        
                        case 9:
                            System.out.println("Sept");
                        break;
                            
                        case 10:
                            System.out.println("Oct");
                        break;
                            
                        case 11:
                            System.out.println("Nov");
                        break;
                            
                        case 12:
                            System.out.println("Dec");
                        break;
                          
                        default:
                            System.out.println("Wrong Choice!!!");
                        
                    }
        }
        catch(Exception err){
            System.out.println("Error : " + err);
        }
    }
}

Simple Input Output Handling in Java | Example | Source code

Written by Pranshu Bajpai |  | LinkedIn

This code illustrates I/O handling with Java

Source Code



package DICAss;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
/**
 *
 * @author Pranshu
 */
public class DICAss5_IOHandle {
    public static void main(String[] args){
        
        String Str;
        
        BufferedReader BRObj = new BufferedReader
                (new InputStreamReader(System.in));
        
        System.out.print("Give Input : ");
        
        try{
            Str = BRObj.readLine();
            System.out.println("Input was " + Str);
        }
        catch(Exception err){
            System.out.println("Error : " + err);
        }
    }
}

Simple File Handling in Java | Example | Source code

Written by Pranshu Bajpai |  | LinkedIn

Here's a simple code that illustrates File Handling in Java:

Source Code



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DICAss;
import java.io.*;

/**
 *
 * @author Pranshu
 */
public class DICAss0_FileHandling {
    
    
    
    public static void main(String[] a){
        int Flag = 0;
        String FileName;
        String FileBody;
        int opt;
        
                
        BufferedReader ObjTake = new BufferedReader(
                new InputStreamReader(System.in));
        
        System.out.println("Choose an Option: \n 1. File Writing \n 2. File Reading");
        
        try{
            System.out.println("What you want to do?? enter option : ");
            opt = Integer.parseInt(ObjTake.readLine());
            
            switch (opt){
                case 1:
                    System.out.print("Enter Name of the File (with extension): ");
                    FileName = ObjTake.readLine();
                    
                    System.out.print("Enter Body of the File : ");
                    FileBody = ObjTake.readLine();
                    
                    Flag = FileWriteFunc(FileName, FileBody);
                    
                    if(Flag == 0){
                        System.err.println("Unable to write!!!");
                    }
                break;
                    
                case 2:
                    System.out.print("Enter Name of the File (with extension): ");
                    FileName = ObjTake.readLine();
                    
                    String FText;
                     FText = FileReadFunc(FileName);
                     System.out.println("Text in file : " + FText);
                    
                    if(FText == null){
                        System.err.println("Unable to read!!!");
                    }
                break;
                    
                default:
                    System.err.println("Invalid Option!!!");
            }
        }
        catch(Exception err){
            System.err.println("Error : " + err);
        }
    }
    
    public static int FileWriteFunc(String FName, String FBody){
        FileOutputStream ObjOut;
        PrintStream ObjPrint;
        
        try{
            ObjOut = new FileOutputStream(FName);
            
            ObjPrint = new PrintStream(ObjOut);
            
            ObjPrint.print(FBody);
            ObjPrint.close();
            
            //System.out.println("Writing Complete!!!");
            
            return 1;
        }
        catch(Exception err){
            System.err.println("Error: " + err);
            return 0;
        }
    }
    
    public static String FileReadFunc(String FName){          
        try{
            FileInputStream FStream = new FileInputStream(FName);
            
            DataInputStream ObjIn = new DataInputStream(FStream);         
         
            String str = "";
            while (ObjIn.available() != 0){
                //System.out.println(ObjIn.readLine());
                str = str + ObjIn.readLine();
            }
            
            ObjIn.close();
            
            //System.out.println("reading Complete!!!");
            return str;
        }
        
        catch(Exception err){
            System.err.println("Error : " + err);
            return null;
        }        
    }
}

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);
    }
    }