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

8 comments:

  1. i found you croose .. :P
    nice work.. keep it up.

    ReplyDelete
    Replies
    1. File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download Now

      >>>>> Download Full

      File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download LINK

      >>>>> Download Now

      File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download Full

      >>>>> Download LINK Ri

      Delete
  2. This method of encryption is insecure for two reasons:
    1. ECB mode
    2. PKCS5 Padding
    3. It does not use all of the Passwords/Keys entropy.

    Data encrypted using this code is likely to be deciphered quickly without a key. Do not use it for important Data!

    ReplyDelete
  3. how cna i run this? in netbean and visual studio code it just displayed " Usage: java EncryptFile "
    TQ in advance

    ReplyDelete
  4. yes
    find other free file encryption software for any secure file file with,help you now

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download Now

    >>>>> Download Full

    File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download LINK

    >>>>> Download Now

    File Encryption And Decryption Using Aes Symmetric Key Cryptographic Algorithm >>>>> Download Full

    >>>>> Download LINK qi

    ReplyDelete