views:

51

answers:

1

( To moderators - This is the 3rd related post of still unsolved problem , now i am posting with all details possible and after doing changes from previous post feedback , though this is a complete post and not dependent on previous 2 posts , if you think this is duplicate please delete the previous posts. thanks )

This is the code of the function

public void decrypt(final InputStream cph_in, final OutputStream out)
 {
  InputStream in;
  try
  {
   // Bytes read from in will be decrypted
   in = new CipherInputStream(cph_in, dcipher);

   // Read in the decrypted bytes and write the cleartext to out
   int numRead = 0;
   //System.out.println(in.read(buf));
   while ((numRead = in.read(buf)) >= 0)
   {
    out.write(buf, 0, numRead);
    System.out.println(numRead);
   }
   //out.close();
  }
  catch (java.io.IOException e)
  {
  }

here is the console output

the Cell Content : ;Xéü¿Uô{¼9¬ðM
3
the Cell Content : ïB
the Cell Content : þ^[ÊN=—î™ì4´•z&
3
the Cell Content : @ûú!Í?+²uŸK^/?¤
3
the Cell Content : ´ƒôœCëîé V­¢%
3
the Cell Content : q^ŽÐâ\Æn2bšcU
3
the Cell Content : ?³j8¥+¤
the Cell Content : R
the Cell Content : 3ex­Ê]ý­v>>|Äð
3
the Cell Content : š¾‚ýËe©%Ä»
the Cell Content : Æ´=OöÀ¶+'¸e£Ñßpö
3
the Cell Content : etO­„ïŸÞñ?Æü é
the Cell Content : çë

when i put outputstream in excel it looks like this (note 124, 129,130 etc. missing )

*** Here lies the problem .. why are some numbers missing.

123

125
126
127
128


131

133

135

137
138
139
140
141

143
144

here is the call to the function

 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);

and i have a feeling that even the rest of the code will be required so i am uploading the source code at http://www.filesavr.com/aesencryption link text ( jar but not executable. to be extracted ) This is how the program works

After importing into Eclipse and giving the desired Apace POI libraries . you need to put some data in first collumn of excel file called c:\MyExcel.xls for our e.g 123 to 144 you need to Run the DoEncryption.java this will convert all data from MyExcel.xls into 128bit key AES encrypted form in c:\workbook.xls and also create c:\key.txt When workbook.xls and key.txt exist in c directory and you run DoDecryption.java it will create c:\Decrypted.xls containing all data decrypted to get original same as MyExcel.xls

Some of the part of the code is not even used so to solve the problem please follow this sequence only

Guys Please help me out. m counting on you.

+1  A: 

You can't reliably store ciphertext (which is binary) into a cell. I suspect the encoding messed some of the cells up. Try to base64 or hex encode the ciphertext, then store it in the cell.

If you have to use raw binary, make sure your encoding is Latin-1 everywhere. Latin-1 preserves the binary sequence.

ZZ Coder
can you tell me how to implement either of that
rover12