views:

570

answers:

1

This part of my code was creating xls file successfuly

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

when other part of the code had this statement ( which was before the above code )

in = new ByteArrayInputStream(theCell_00.getBytes(""));

But when I changed it to

in = new ByteArrayInputStream(theCell_00.getBytes("UTF-8"));

this part

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
wb.write(fileOut);
fileOut.close();

is not generating any xls file anymore.

I need to change the encoding to UTF-8 as I have done in ByteArrayInputStream line, so what should I do that the code still generates xls file.

In case you need it, the two parts are taken from this function.

public void getExcel() throws Exception {

    try {
        ByteArrayInputStream in = null;
        FileOutputStream out = null;

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

        /*
         * KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key =
         * kgen.generateKey(); byte[] encoded = key.getEncoded();
         * 
         * IOUtils.write(encoded, new FileOutputStream(new
         * File("C:\\Users\\abc\\Desktop\\key.txt")));
         */

        FileInputStream fin = new FileInputStream("C:\\key.txt");
        DataInputStream din = new DataInputStream(fin);

        byte b[] = new byte[16];

        din.read(b);

        InputStream excelResource = new FileInputStream(path);
        Workbook rwb = Workbook.getWorkbook(excelResource);
        int sheetCount = rwb.getNumberOfSheets();
        Sheet rs = rwb.getSheet(0);
        int rows = rs.getRows();
        int cols = rs.getColumns();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < Col.length; j++) {
                String theCell_00 = rs.getCell(j, i).getContents();
                System.out.println("the Cell Content : " + theCell_00);

                in = new ByteArrayInputStream(theCell_00.getBytes(""));
                out = new FileOutputStream("c:\\Decrypted.txt");

                try {

                    // System.out.println(b);
                    SecretKey key1 = new SecretKeySpec(b, "AES");
                    // Create encrypter/decrypter class
                    AESDecrypter encrypter = new AESDecrypter(key1);

                    encrypter.encrypt(new ByteArrayInputStream(theCell_00.getBytes()),
                        new FileOutputStream("temp.txt"));
                    // Decrypt
                    // encrypter.encrypt(,new FileOutputStream("Encrypted.txt"));

                    encrypter.decrypt(in, out);

                    try {
                        if (out != null)
                            out.close();
                    } finally {
                        if (in != null)
                            in.close();
                    }

                    // encrypter.decrypt(new
                    // ByteArrayInputStream(theCell_00.getBytes(Latin_1)),new
                    // FileOutputStream("c:\\Decrypted.txt"));
                    String filename = "c:\\Decrypted.txt";

                    BufferedReader bufferedReader = null;

                    try {

                        // Construct the BufferedReader object
                        bufferedReader = new BufferedReader(new FileReader(filename));
                        // System.out.println(bufferedReader.readLine());
                        String line = null;

                        while ((line = bufferedReader.readLine()) != null) {
                            // Process the data, here we just print it out

                            /*
                             * HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet =
                             * wb.createSheet("new sheet"); HSSFRow row = sheet.createRow(2);
                             */
                            // System.out.println(i);

                            HSSFRow row = sheet.createRow(i);
                            int s_col = 0;
                            row.createCell(s_col).setCellValue(line);
                            // s_col++;
                            // row.createCell(1).setCellValue(new Date());
                            FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");
                            wb.write(fileOut);
                            fileOut.close();

                            // System.out.println(line);
                        }

                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } finally {
                        // Close the BufferedReader
                        try {
                            if (bufferedReader != null)
                                bufferedReader.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        rwb.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        ex.getMessage();
    }
}
A: 

What data types are expected by the call to AESDecrypter.decrypt? Does it have to take in a FileOutputStream object? Or can you pass in a Writer or other OutputStream?

I normally do something like this to write UTF-8 output:

Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\Decrypted.txt"), "UTF-8"));
Brent Nash