AnsweredAssumed Answered

Encrypting data in repository

Question asked by tim91 on Jul 6, 2013
Latest reply on Jul 10, 2013 by tim91
Hi,

I hope that this is right place to wrote this post.

I extended few classes called ContentServiceImpl, FileContentStore, FileContentReader and FileContentWriter.

In my new classes called EnryptedContentReader and EnryptedContentWriter i am grabbing data stream in overrided methods and i encrypt/decrypt it.

If file is only encrypted i can download it without problem and file is encrypted (file encrypts and everything is fine).

Problem is when i am trying download file and decrypt it, in method  getContent(OutputStream os) (method from class AbstractContentReader) that i am overriding, i am doing something like this.


        @Override
   public void getContent(OutputStream os) throws ContentIOException {

      if (!decrypt) {  //i am checking if i must decrypt file
         try
           {
               InputStream is = getContentInputStream();
               os.flush();
               FileCopyUtils.copy(is, os);  // both streams are closed
               // done
           }
           catch (IOException e)
           {
               throw new ContentIOException("Failed to copy content to output stream: \n" +
                       "   accessor: " + this,
                       e);
           }
      } else {
            decryptDuringCopy(os);
      }
   }

   public void decryptDuringCopy(OutputStream os){
      InputStream is = null;
      CipherInputStream cis = null;
       try
       {
          is = getContentInputStream();
           os.flush();
            AESManager am = new AESManager();
            Cipher cipher = null;
         try {
            cipher = am.getCipherDecrypt(key.getBytes("UTF-8"));
         } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
            cis = new CipherInputStream(is, cipher);
            byte[] data = new byte[1024];
            int read = cis.read(data);
            int readed = read;
            while (read != -1) {
                os.write(data, 0, read);
                read = cis.read(data);
                readed += read;
            }
           
       }
       catch (IOException e)
       {
           throw new ContentIOException("Failed to copy content to output stream: \n" +
                   "   accessor: " + this,
                   e);
       } finally {
           try {
              os.flush();
            os.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
           try {
            is.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
           try {
            cis.close();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
       }
   }


Problem is strange because file is downloading but when all bytes was copied into OutputStream, in browser i have 99% of download and i must wait about 10sec to process ending. After download, file is correctly decrypted. It look like in alfresco is turning sleep method after copying all bytes. I am wondering if maybe browser is checking checksum ( file that is downloaded is decrypted and is different to file that was stored in repository ).
I don't have any idea what is wrong

Sorry for my engilsh.

Edit:
I connected to alfresco using ftp, after download total commander says "Downloading error" but after pressing "OK", i can open file and everything is fine. 

Outcomes