Thursday, December 13, 2012

Validate the Prime Number in java

this is the method to identify the prime numbers

import java.util.*;

public class prime
{
int n;
    public static void main(String args[])
    {
        Scanner m=new Scanner(System.in);
        System.out.println("Enter your number");
        int P=m.nextInt();
        prime input=new prime();
        input.check(P);
    }   
       
    void check(int P)
        {
            for(int i=2;i<P;i++)
            {
                if(P%i!=0)
                    {
                        n=0;
                    }
                else if(P%i==0)
                    {
                        n=1;break;
                    }
            }
                if(n==0)
                {
                    System.out.println("prime");
                }
                else if(n==1)
                {
                    System.out.println("not prime");
                }
        }
}

Friday, December 7, 2012

PHP Connection With MySql DataBase








Let's learn to connect the php with MySQL Database to store and retrieve data 
<?php
$host='localhost';
$user='root';
$pass='';
$database_name='user';
?>

<?php
    require_once 'config/config.php';
    $connect = mysql_connect($host,$user,$pass) or die("Connection to server failed");
    mysql_select_db($database_name,$connect) or die("Connection to database failed");
   
    $name = $_POST['name'];
    $phone_no = $_POST['phone'];
    $email = $_POST['email_addr'];
   
    $result=mysql_query("INSERT INTO userdetails(Name,PhoneNo,Email) VALUES ('$name',$phone_no,'$email')",$connect);
    echo $result;
    if($result){
        echo "Successfully inserted";       

    }
    mysql_close($connect);
?>

#main
{
    background-color:#00FF00;
    width:300;
    height:100;
    position:absolute;           
    margin-left:400;
    margin-top:200;
    padding-left:10;
    padding-top:10;
}
#mainbody
{
    background-color:#00FF00;
    width:350;
    height:250;
    position:absolute;           
    margin-left:300;
    margin-top:200;
    padding-left:10;
    padding-top:10;
}
   


<html>
    <head>
        <title>HTML Form</title>
        <link rel="stylesheet" type="text/css" href="style.css" >
        <script src="validation.js" type="text/javascript" xml:space="preserve"></script>
    </head>
    <body bgcolor="#9999CC">
    <div id="mainbody">
        <form name="details" action="insert.php"  onsubmit="return validateFormOnSubmit(details)" method="POST" >
            <p>
            Enter your name:
            <br />
            <input type="text" size=50 name="name" />
            <p>
            Enter your phone:
            <br />
            <input type="text" size=50 name="phone" />
            <p>
            Enter your email address:
            <br />
            <input type="text" size=50 name="email_addr" />
            <p>
            <input type=submit value="submit" />
            <input type=reset value="clear" />
        </form>
    </div>
    </body>
</html>


      

Encryption and Decryption















import java.util.*; 
//our scanner class we are going to derive from this library
import java.io.*; 
// it's a library it's have some input output functions and methods in this program we  are going to read and write files so we need this library

public class encryption
{
    static String plaintxt,ciphertxt,cryptxt;
    static int key,selct;
    char k;
    char Mono[];
   
//read text from file
    private void readfile(String filename) throws IOException 
//here we put Io Exception because file may not be in your folder so programe may show error or corrupt to prevent this error in Runtime we use exception
        {
            try
            {
//if try method cannot perform it will call catch method
                File rFile=new File(filename);
//An abstract representation of file and directory pathnames so that instance rFile indicate a full path of given file filname
                Scanner sc=new Scanner(rFile);
//Read the file in given path name rFile
                cryptxt="";
//we wanr to assign empty otherwise it will take null value in results
                while(sc.hasNext())
//hasNext( ) is a special  method in scanner class to verify next line have or not
                    {
                        cryptxt+=sc.nextLine()+" \n"; 
// method in scanner class to read lines and return value
                        cryptxt=cryptxt.toLowerCase();
                      }
            }
           
            catch(FileNotFoundException e)
            { 
//if try method can not perform this method will do whatever in this catch method we say
                System.out.println("Error|"+ filename+"not found"); 
//so here if file not in folder then programe will show that particular filr not found
            }
        }
       
    public void writefile(String filename) throws IOException
    //write file in given name
        {
            PrintWriter fout=new PrintWriter(filename); 
// Creates a new PrintWriter, without automatic line flushing, with the specified file name
            fout.append(ciphertxt); 
//Appends the specified character sequence to this writer.
            fout.close();
// Closes the stream and releases any system resources associated with it.
            System.out.println("your New Encrypted file successfully created or Encrypted Text Added");
        }
   
    public static void main(String args[]) throws IOException
    {
        Scanner n=new Scanner(System.in); 
//to take the input from outside
        encryption e1=new encryption();
//creating new instance of our Encryption class
        System.out.println("What type of cipher model you want(press 1 or 2) \n 1. ceaser cipher \n 2.Monoalphabetic");
        selct=n.nextInt(); 
//assign the input integer to selct variable of n that's a instance of encryption class
        if(selct==1)
    {        
 //In this stage we are going to select what type of action we are going to do it's mean encryption or decryption
        System.out.println("Enter Your Method You Want(press 1 or 2) \n 1.Encryption \n 2.Decryption");
        selct=n.nextInt();
       
        if(selct==1)
          {
 //method to Encryption
            System.out.println("Welcome to Encryption Method \n Enter Your Key Value");
            key=n.nextInt();
//assign the value of the key to variable key because of it's global variable this value will take global value to key variable
           
            System.out.println("Please Enter Your File Name That You Want To Encrypt");
            String Fnme=n.next(); 
//get the file name that we wish to encrypt
            e1.readfile(Fnme); 
//sent the name of the file we want to read to readfile method
           
            System.out.println("Your Text Read From Your File \n");
            System.out.println(cryptxt);
// to show text that read from file in console before encrypt
           
            e1.encrypt();
//call the encrypt method to encrypt our text that were read from our file
           
            System.out.println("Please Enter File Name That Want to save new Encrypted file");
            String Nnme=n.next(); 
// get the file name we wish to save our encrypted file
            e1.writefile(Nnme); 
//sent the file name to writefile method
        }
        else if(selct==2) 
//method to Decryption
        {

//Here we have two chances 1st one is we know the key of the encrypted text 2nd one is we don't know the encrypted text  key value
            System.out.println("Do you know the key value?? (press 1 or 2) \n 1. i know the key \n 2. i Don't know the key value" );
            int crypt=n.nextInt();
            if(crypt==1)
 //when key know
            {
                System.out.println("Welcome to Deccryption method \n enter your key value");
                key=n.nextInt();
// get the value of the encrypted text key
               
                System.out.println("\n please enter your file name that you want to Decrypt");
                String Fnme=n.next(); 
//get the file name that we wish to decrypt
               
                e1.readfile(Fnme); 
//sent the name of the file we want to read to readfile method
               
                System.out.println("Your Text Read From Your File \n");
                System.out.println(cryptxt); 
// to show text that read from file in console before decrypt
               
                e1.Decrypt(); 
//call the Decrypt method to Decrypt our text that were read from our file
            }
            else if(crypt==2)
 //when key don't know
            { 
//key value but we don't know however key is going to be within 26 so we can check all numbers which is correct
                System.out.println("\n please enter your file name that you want to Decrypt");
                String Fnme=n.next(); 
//get the file name that we wish to decrypt
               
                System.out.println("Your Text Read From Your File \n");
                e1.readfile(Fnme); 
//sent the name of the file we want to read to readfile method
                System.out.println(cryptxt); 
// to show text that read from file in console before decrypt
               
                e1.cryptAnalys(); 
//call the cryptAnalys method to check the key value and to find original message
            }
            else
            {
                System.out.println("please press 1 or 2");
            }
           
            System.out.println("\n please enter file name that want to save new Decrypted file");
            String Nnme=n.next(); 
// get the file name we wish to save our decrypted file
            e1.writefile(Nnme); 
//sent the file name to writefile method
        }
        else
        {
           
            System.out.println("please press 1 or 2");
        }
    }
 }
   
    public void encrypt()
    {
        ciphertxt=""; 
//if not assign empty it's will print null value in file
       
        int l=cryptxt.length()-1;
        int as;
        String cyp="";
     
        for(int i=0; i<l;i++)
        {
//here we are going to get each and every character in cryptxt then get the ASCII value of that character then if it is a English  character then encrypt using key then joint them then we get encrypted text
            k=cryptxt.charAt(i);
            as=(int)k; 
//get the ASCII value of each and every character in cryptxt and assign it to variable as
            if(as>96&&as<124) 
//checking is it corrector or symbol
            {
 //if it is a English lowercase character then we are going to add key value with the ascii value of the every character but we want to consider result value want to be within the boundary of the English lowercase character ascii values so here we are doing some mechanism to keep it
                as=((as-97+key)%26)+97;
//here first we  we eliminate the low index characters through subtract 97so now a's ASCII value is going to be 0 then we add key value but after that we take mod 26 because some times if char is z then value is going to be 25 then if we add key it's going to out of border so through take mod 26 we bring it within our boundary and it's not going to create any complexity within the characters because if key is 2 then a is going to be 2 and z is going to be 1 so no complexity 
 
                ciphertxt=ciphertxt+(char)as;
//joint every encrypted characters to ciphertxt
            }
            else
            {
// if it is not English character then joint it to ciphertxt without any change
                ciphertxt=ciphertxt+(char)as;
            }
          }
        System.out.println("\n Your Text That Encrypted \n");
        System.out.println(ciphertxt);
//show in encrypted text in console  
//another important think is we already assign ciphertxt as a global variable so if we assign ciphertxt value here it will be take as a global value to ciphertxt  so wherever you call the ciphertxt after this step it will take this ciphertxt value to that one
    }
   
    public void Decrypt()
    {
        ciphertxt="";
  //if not assign empty it's will print null value in file    
        int l=cryptxt.length()-1;
        int as;
        String cyp="";
       
        for(int i=0; i<l;i++)
        {
            k=cryptxt.charAt(i);
            as=(int)k;
            if(as>96&&as<124) 
//checking is it corrector or symbol
            {
                as=(as-97-key);
                if(as<0) as=as+26;
//here we are using different  mechanism when we encrypt we subtract 97 and add key then take mod then add 97 but here just subtract 97 and key then check whether it's less then 0 or not if it is less then 0 add 26 because we know  that char's are take that index because of out of range 26 so here we just add 26 it will take it's original index so then add 97 now we get original ASCII values of characters
                as=as+97;
            
                ciphertxt=ciphertxt+(char)as;
            }
            else
            {
                ciphertxt=ciphertxt+(char)as; 
//add directly because it's nor corrector
            }
          
        }
        System.out.println("\n Your Yext That Decrypted \n");
        System.out.println(ciphertxt);
    }
   
    public void cryptAnalys()
    {
        Scanner m=new Scanner(System.in);
        for(int i=0;i<27;i++)
        {
            key=i;

            Decrypt();//call the decrypt method to one by one key values until result is satisfy
            System.out.println("Are you Satisfiy in this Decrypted Message?? \n 1.yes \n 2.No");
            int agree=m.nextInt();
            if(agree==1) break;//if result is satisfied then stop the loop 
        }
    }