Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have done this with Java i want to decode the html pages using javascript a sample will be good
Java
package encrypt;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author GAYAN
 */
public class Encrypt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        File n = new File("C:\\Users\\Shashika\\Desktop\\enc");
        File[] listFiles = n.listFiles();
        for (File file : listFiles) {
            InputStream is = null;
            OutputStream os = null;
            try {
                BASE64Encoder bs = new BASE64Encoder();
                System.out.println(file.getName());
                is = new FileInputStream(file);
//                os=new FileOutputStream(file);
                byte[] bs1 = null;
                try {
                    bs1 = new byte[is.available()];
                    System.out.println("read="+is.available());
                    is.read(bs1);
                    String encode = bs.encode(bs1);
                    System.out.println(encode);
                    
                    is.close();
                    os=new FileOutputStream(file);
                    
                    os.write(encode.getBytes());
                    os.close();
                    
                    
                    System.out.println("=====================decoding========================");
                    
                    BASE64Decoder decoder=new BASE64Decoder();
                    byte[] decodeBuffer = decoder.decodeBuffer(new FileInputStream(file));
                    
                    String s=new String(decodeBuffer);
                    System.out.println(s);
                    
     File f=new File("C:\\Users\\Shashika\\Desktop\\enc\\enc0000.html");
                    
                    OutputStream os1=new FileOutputStream(f);
                    
                    os1.write(decodeBuffer);
                    os1.flush();
                    os1.close();
                    //                    for (byte b : bs1) {
                    //                        System.out.println("byte="+b);
                    //                    }
                } catch (Exception ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }

                break;
            } //        bs.encode();
            catch (FileNotFoundException ex) {
                Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    is.close();
                } catch (IOException ex) {
                    Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }


//        bs.encode();

    }
}
Posted

 
Share this answer
 
Here is a Base64 Encoder and Decoder with JavaScript[^]


You can read files in JS [^]and perform actions using the read data, but you cannot write to the local disk using JS (with a few exceptions like Google Gears)


Best,
H
 
Share this answer
 
v3
Comments
gayan_priyankara 11-Sep-13 2:31am    
i have try that but how can i give the path to ma local folder??
Herbisaurus 11-Sep-13 2:34am    
If you are talking about JS that runs in a Client's browser, I seriously doubt if it can be done, since access to perform actions on local files is denied (for obvious reasons...)

Try to explain more - What's your use case?
gayan_priyankara 11-Sep-13 2:42am    
i need to write two js
1. encrypt the html and xhtml in ma server file wich am uploading
2. decrypt that in ma application
Herbisaurus 11-Sep-13 2:46am    
And you application is a web application?
gayan_priyankara 11-Sep-13 2:48am    
nope it is 4n application am doing it using phone-gap and html5 css3 and Js
the app is connected with ma server

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900