Saturday, November 30, 2013

How To Run IIS

Today I am going to show how to run IIS. Follow the images one by one....




 

How To Install IIS (Internet Information Server) on Windows

Today I am going to show how to install IIS on your computer.
Go To Control Panel => Programs. In Programs and Features section click 'Turn Windows Features On or Off' . (Or simply type 'appwiz.cpl'  and Run) .Then follow the images....
Then Tick Internet Information Server Section
Then open your browser and type 'localhost' in address bar and enter to check whether IIS is working in your computer

Thursday, February 28, 2013

Find Greatest Common Devicer Using Java

 import java.util.*;  
 public class GCD{  
      public static void main(String args[]){  
           int integer1,integer2,GCD;  
           Scanner scanner = new Scanner(System.in);  
           try{  
                System.out.print("\n\n\nEnter Two Integers(separate by space) : ");  
                integer1 = scanner.nextInt();  
                integer2 = scanner.nextInt();  
                if(integer1==0||integer2==0){  
                     System.out.println("Error:Can not find GCD for 0");  
                     System.exit(-1);  
                }  
                GCD=GCD(integer1,integer2);  
                System.out.println("GCD of "+integer1+" and "+integer2+" is "+GCD);  
           }catch(Exception e){  
                System.out.println("Error:Integer values must be given");  
           }  
      }  
      private static int GCD(int integer1,int integer2){  
           int temp=0;  
           if(integer1>integer2){  
                while(integer1>0){  
                     temp=integer2 % integer1;  
                     integer2=integer1;  
                     integer1=temp;  
                }  
                return integer2;  
           }  
           else if(integer1<integer2){  
                while(integer2>0){  
                     temp=integer1 % integer2;  
                     integer1=integer2;  
                     integer2=temp;  
                }  
                return integer1;  
           }  
           else{  
                return integer1;  
           }  
      }  
 }  

Find Prime Factors Of Given Integer Using Java

 import java.util.*;  
 public class primeFactor{  
      public static void main(String args[]){  
           int integer;  
           Scanner scanner = new Scanner(System.in);  
           List<Integer> factors = new ArrayList<Integer>();  
           try{  
                System.out.print("\n\n\nEnter Integer : ");  
                integer = scanner.nextInt();  
                if(integer==0){  
                     System.out.println("Error:Can not find primes for 0");  
                     System.exit(-1);  
                }  
                System.out.print(integer+" = ");  
                for (Integer integer1 : FindSmallestFactors(integer)) {  
                     System.out.print(integer1+" ");  
                }  
           }catch(Exception e){  
                System.out.println("Error:Integer values must be given");  
           }  
      }  
      private static List<Integer> FindSmallestFactors(int integer){  
           List<Integer> factors = new ArrayList<Integer>();  
           for (int i = 2; i <= integer; i++) {  
                while (integer % i == 0) {  
                     factors.add(i);  
                     integer = integer / i;  
                }  
           }  
           return factors;  
      }  
 }  

Find Prime Numbers Using Java

 import java.util.*;  
 public class Primes{  
      public static void main(String args[]){  
           int integer;  
           Scanner scanner = new Scanner(System.in);  
           List<Integer> factors = new ArrayList<Integer>();  
           try{  
                System.out.print("\n\n\nEnter Integer : ");  
                integer = scanner.nextInt();  
                if(integer==0){  
                     System.out.println("Error:Can not find primes for 0");  
                     System.exit(-1);  
                }  
                 for (Integer integer1 : Primes(integer)) {  
                     System.out.print(integer1+"\t");  
                }  
           }catch(Exception e){  
                System.out.println("Error:Integer values must be given");  
           }  
      }  
      private static List<Integer> Primes(int integer){  
           List<Integer> factors = new ArrayList<Integer>();  
           for (int i = 1; i <= integer; i++) {  
                for(int j=2;j<=i;j++){  
                     if(i==j){  
                          factors.add(i);  
                          break;  
                     }  
                     //value is not prime  
                     if(i %j==0){  
                          break;  
                     }  
                }  
           }  
           return factors;  
      }  
 }  

Using Sieve of Eratosthenes

 About Sieve of Eratosthenes. This method is more faster than above prime number finding method.
 import java.util.*;  
 //create a structure  
 class list{  
      int value;  
      char flag;  
 }  
 public class ex{  
      public static void main(String args[]){  
           int integer;  
           Scanner scanner = new Scanner(System.in);  
           try{  
                System.out.print("\n\n\nEnter Integer : ");  
                //get integer  
                integer = scanner.nextInt();  
                if(integer==0){  
                     System.out.println("Error:Can not find primes for 0");  
                     System.exit(-1);  
                }  
                PrimesSieveOfEratosthenes(integer);  
           }catch(Exception e){  
                System.out.println("Error:Integer values must be given");  
           }  
      }  
      private static void PrimesSieveOfEratosthenes(int integer){  
           list integers[]=new list[integer];  
           //initialize list elements  
           for(int m=0;m<integers.length;m++){  
                integers[m]=new list();  
           }  
           //add values to list elements  
           for(int i=1;i<integer;i++){  
                integers[i].value=i+1;  
                integers[i].flag='0';  
           }  
           //find prime numbers  
           for(int k=2;k<=integer;k++){  
                if(integers[k-1].flag=='0'){  
                     for(int j=2*k-1;j<integer;j=j+k){  
                          integers[j].flag='1';  
                     }  
                }  
           }  
           //print prime numbers  
           for(int l=1;l<integer;l++){  
                if(integers[l].flag=='0'){  
                     System.out.print(integers[l].value+"\t");  
                }  
           }  
      }  
 }  

Wednesday, February 27, 2013

File Encryption and Decryption using AES Symmetric key Cryptographic algorithm

In this work, i encrypt a web page by running EncryptFile.java, then host that encrypted file to the server. Then another client can download that file from the server and decrypt it by running DecryptFile.java if that client has secret key.

EncrypFile.java

 import java.security.*;  
 import java.io.*;  
 import javax.crypto.*;  
 import javax.crypto.spec.*;  
 public class EncryptFile {  
      public static void main(String args[]) {  
           if (args.length < 1) {  
                System.out.println("Usage: java EncryptFile <file name>");  
                System.exit(-1);  
           }  
           try {  
                File aesFile = new File("Encrypted Web Page.html");  
                FileInputStream fis;  
                FileOutputStream fos;  
                CipherInputStream cis;  
                 //Creation of Secret key  
                String key = "MySEcRetKeY";  
                int length=key.length();  
                if(length>16 && length!=16){  
                     key=key.substring(0, 15);  
                }  
                if(length<16 && length!=16){  
                     for(int i=0;i<16-length;i++){  
                          key=key+"0";  
                     }  
                }  
                SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");  
                 //Creation of Cipher objects  
                Cipher encrypt =Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");  
                encrypt.init(Cipher.ENCRYPT_MODE, secretKey);  
                // Open the Plaintext file  
                try {  
                     fis = new FileInputStream(args[0]);  
                     cis = new CipherInputStream(fis,encrypt);  
                     // Write to the Encrypted file  
                     fos = new FileOutputStream(aesFile);  
                     byte[] b = new byte[8];  
                     int i = cis.read(b);  
                     while (i != -1) {  
                          fos.write(b, 0, i);  
                          i = cis.read(b);  
                     }  
                     fos.flush();  
                     fos.close();  
                     cis.close();  
                     fis.close();  
                } catch(IOException err) {  
                     System.out.println("Cannot open file!");  
                     System.exit(-1);  
                }  
           } catch(Exception e){  
                e.printStackTrace();  
           }  
      }  
 }  

DecryptFile.java

 import java.security.*;  
 import java.io.*;  
 import javax.crypto.*;  
 import javax.crypto.spec.*;  
 import java.net.*;  
 public class DecryptFile {  
      public static void main(String args[]) {  
           try {  
                File aesFile = new File("Downloaded Encrypted Web Page.html");  
                // if file doesnt exists, then create it  
                if (!aesFile.exists()) {  
                     aesFile.createNewFile();  
                }  
                aesFile=retrieve(args);  
                File aesFileBis = new File("Decrypted Web Page.html");  
                FileInputStream fis;  
                FileOutputStream fos;  
                CipherInputStream cis;  
                 //Creation of Secret key  
                String key = "MySEcRetKeY";  
                int length=key.length();  
                if(length>16 && length!=16){  
                     key=key.substring(0, 15);  
                }  
                if(length<16 && length!=16){  
                     for(int i=0;i<16-length;i++){  
                          key=key+"0";  
                     }  
                }  
                SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");  
                 //Creation of Cipher objects  
                Cipher decrypt =Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");  
                decrypt.init(Cipher.DECRYPT_MODE, secretKey);  
                // Open the Encrypted file  
                fis = new FileInputStream(aesFile);  
                cis = new CipherInputStream(fis, decrypt);  
                // Write to the Decrypted file  
                fos = new FileOutputStream(aesFileBis);  
                byte[] b = new byte[8];  
                int i = cis.read(b);  
                while (i != -1) {  
                     fos.write(b, 0, i);  
                     i = cis.read(b);  
                }  
                fos.flush();  
                fos.close();  
                cis.close();  
                fis.close();  
           } catch(Exception e){  
                e.printStackTrace();  
           }  
      }  
      public static File retrieve(String args[]){  
           if (args.length!=1) {  
                System.out.println("Usage: UrlRetriever <URL>");  
                System.exit(-1);  
           }  
           File file = new File("Downloaded Encrypted Web Page.html");  
           try {  
                URL url=new URL(args[0]);  
                BufferedInputStream buffer=new  
                BufferedInputStream(url.openStream());  
                DataInputStream in= new DataInputStream(buffer);  
                String line;  
                // if file doesnt exists, then create it  
                if (!file.exists()) {  
                     file.createNewFile();  
                }  
                FileWriter fw = new FileWriter(file);  
                BufferedWriter bw = new BufferedWriter(fw);  
                while ((line=in.readLine())!=null){  
                     bw.write(line);  
                }  
                bw.close();  
                in.close();  
           } catch(MalformedURLException mue) {  
                System.out.println(args[0]+"is an invalid URL:"+mue);  
           }catch(IOException ioe) {  
                     System.out.println("IOException: "+ioe);  
           }  
           return file;  
      }  
 }  

You can download my work out HERE