• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

AES Encryption & Decrypt For A File Of Med. Size

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to encryption and am faced with a problem that I need to solve and I just want to dive in and start swimming. I need to understand how to use AES encryption & decryption securely and effectively. I have been reading a lot of AES encryption online trying to understand the basics in which I have developed a foundation but am coming here to present my problem and ask for advice from advanced users.

**
What I am trying to do is encrypt a file that is being uploaded to a server so before its sent out its going to be encrypted and then will be stored in the server encrypted and when the user downloads the file it will be decrypted and sent to the destination path where it will be opened as per normal.


I'll Explain With Code;








And Decrypt






Main question I am purposing is the frame work of this code where to put and how to actually do this... There are a lot of resources on the internet but understanding and tailoring to my need in this context is doesn't seem to work effectively, Would appreciate any help/guidance on the subject, thanks in advance code ranch community
 
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

travis Haycock wrote: What I am trying to do is encrypt a file that is being uploaded to a server...



Well, first of all most of that code has nothing to do with encryption. It all seems to be some kind of machinery for messing with files. It looks like you are trying to read a file from your system, encrypt it, and upload the result to Dropbox.

A good tool to do this would be something which can read data from an InputStream, encrypt the data, and write the encrypted data to an OutputStream. Then you would pass that tool an InputStream pointing to a file on your system and an OutputStream pointing to something on Dropbox. You'd also need the inverse of that, something which can read from an InputStream, decrypt the data, and write it to an OutputStream.

But you seem to be trying to use something which uploads from a file to Dropbox. Do you really have to use that? Because it's forcing you to write your encrypted data back to a file on your local machine before uploading it, which is a waste of resources. Is there something in your Dropbox client which gives you an OutputStream pointing to a Dropbox target?
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

The above statement is correct; I am trying to read the file from the system and upload it to dropbox. Following along with the structure as you mentioned I understand and will now attempt to implement your advice with the Streams... the last part your mentioned I am now searching documentation in context of the OutputStream to DropBox. The way I developed it before works very well in context of the uploading task, I guess back to the drawing board if need to be, thank you for your input!
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually...



This code seems to be something which uploads to Dropbox from an InputStream, even though its name suggests it uploads a file. If that's the case then you could wrap your FileInputStream (which points to the file on your local system) in a CipherInputStream (that's in the javax.crypto package). You'd just have to provide with a Cipher with implemented AES in the right way.

I'm not sure if the AES-encrypted data is going to be the same length as the original data. If not, then you might have problems with the "length" parameter of your "uploadFile" method. But perhaps there's a way of saying "I don't know what the length is going to be".
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Paul mentioning, you could use CipherInputStream which will do Encryption and Decryption of the files. Length of the Encrypted file depend on the
algorithm you have used. For AES/CBC/PKCS5Padding, length of encrypted block is multiple of 16. So if you have length of file not multiple of 16
then resultant encrypted file size would be multiple of 16.

For ex: if initial size is 28 then resultant size would be 32.

ex:




 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tushar,


I have implemented the code appropriately from what i can see is that everything is working on the encrypt side. When I go through my code and upload a file to dropbox and view it all it says is '16 bytes' with no image or thumbnail so I'm thinking that did the trick! by implementing the code provided and modifying it a little... Now with decryption of these files how would an individual go about creating a blow to decrypt this as well? thank you for everyones help.
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

travis Haycock wrote:Now with decryption of these files how would an individual go about creating a blow to decrypt this as well? thank you for everyones help.



Here's how a programmer would think about that. "Well, there's a CipherInputStream for encrypting an InputStream, so maybe there's some similar thing for decrypting an InputStream. Let's have a look in the API documentation and see. I guess I'll start by looking at CipherInputStream and see if it has any hints."

 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading the documentation on the subject on the Oracle site and using examples but to be honest there is so much conflicting code out there and different styles to do this its rather confusing and considering its my first time it's turning out to be frustrating getting this code to work;





Im getting a bunch of errors with this I have tried multiple different ways I really don't know the structure and haven't found a concrete example of encryption & decryption that I can study and make sense of in a workable form. Most of the examples floating around are with strings etc. that doesn't dig deep enough allowing for a full grasp understanding. Im not asking for the answer outright but, please help me understand the decryption better or point to a solid link that is within relevance.
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main trouble with that code is that it tries to do everything all at once and it's turned into a jumbled mess. (Sorry to say that, but I hope you will agree.)

You started out with code which copied a file on your system to a place on Dropbox. So it should have been a simple matter to change that code to insert a CipherInputStream. But it wasn't, because your code was doing everything at once. So here's how I think your code could be restructured. You need three methods:

1. A method to identify a file on your system to be uploaded. It should return an InputStream connected to that file. (Leave encryption out of it to start with, you can add that in later.)

2. A method to identify a Dropbox location to upload to. It should do all the necessary configuring and connecting and return an OutputStream connected to that location.

3. A method to copy all the bytes from an InputStream to an OutputStream. It shouldn't make any assumptions about those streams, just copy the bytes.

Then you need a controller method which just calls those three methods and does nothing else.

(Yes, I've left out error-handling and I've left out resource cleanup too. They shouldn't be left out in a final version but right now it isn't clear how much is involved and where those things should be done.)
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

i'll implement the context you provided
 
travis Haycock
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essentially solved.. All I'm working on now is how to get the output File to go through Whats happening is the method is functional but whats happening as you can probably see is the output from the encrypt method is saving it to my desktop and it is instructed but what I am trying to do is save that var and pass it to dropbox I have tried different code and its a no go , i know its something simple I'm probably overthinking at this point...


reply
    Bookmark Topic Watch Topic
  • New Topic