3

Possible Duplicate:
encrypt the html files for client side and browse them in a swing application

I am working on a swing application in which in which client has to access the html files locally stored in the machine but i want client should not access the html files directly so want to encrypt the entire folder of html files using java and in Java application i would write the hard code to decrypt the html files from encrypted folder. One more thing the updation should be possible in encrypted folder so that encrypted files may be merged in future on client side.

I have been stuck here and has no clues for my problem , any help for my problem is appreciated.

Community
  • 1
  • 1
adesh
  • 1,157
  • 3
  • 18
  • 28

2 Answers2

2

- Well i would ask you to use Cipher, CipherInputStream and CipherOutputStream for encryption and decryption.

- You can loop through the files in the folder and then encrypt each file, and similarly you can loop through the files in the folder to decrypt it.

See this link :

http://www.flexiprovider.de/examples/ExampleCrypt.html

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
2

Have a read on this link:

  • Using AES with Java Technology (Could not use the link as normally due to the domain being a number): http://192.9.162.55/developer/technicalArticles/Security/AES/AES_v1.html

By default you can use up to AES 128bit.

In order to use 256 bit AES keys, you must download and install "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from here.

Here is a simple example which encrypts an decrypts a String message in java using AES:

 import java.security.*;
   import javax.crypto.*;
   import javax.crypto.spec.*;
   import java.io.*;

   /**
   * This program generates a AES key, retrieves its raw bytes, and
   * then reinstantiates a AES key from the key bytes.
   * The reinstantiated key is used to initialize a AES cipher for
   * encryption and decryption.
   */

   public class AES {

     /**
     * Turns array of bytes into string
     *
     * @param buf   Array of bytes to convert to hex string
     * @return  Generated hex string
     */
     public static String asHex (byte buf[]) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)
        strbuf.append("0");

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
      }

      return strbuf.toString();
     }

     public static void main(String[] args) throws Exception {

       String message="This is just an example";

       // Get the KeyGenerator

       KeyGenerator kgen = KeyGenerator.getInstance("AES");
       kgen.init(128); // 192 and 256 bits may not be available


       // Generate the secret key specs.
       SecretKey skey = kgen.generateKey();
       byte[] raw = skey.getEncoded();

       SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");


       // Instantiate the cipher

       Cipher cipher = Cipher.getInstance("AES");

       cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

       byte[] encrypted =
         cipher.doFinal((args.length == 0 ?
          "This is just an example" : args[0]).getBytes());
       System.out.println("encrypted string: " + asHex(encrypted));

       cipher.init(Cipher.DECRYPT_MODE, skeySpec);
       byte[] original =
         cipher.doFinal(encrypted);
       String originalString = new String(original);
       System.out.println("Original string: " +
         originalString + " " + asHex(original));
     }
   }

The above code will have to be modified in such a way that you read the encrypted files into a StringBuffer/byte array etc, un-encrypt them (in memory only) do the work needed then re-encrypt StringBuffer/data/bytes and write it to file.

Another great Cryptograpic API is:

There are many examples that can be found for Bouncy Castle API too:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138