Encrypting contents of a file when uploading - c#

Am using asp.net's FileUpload control to upload a word file to the server.
I want to encrypt the contents of this file (using our custom encryption API) and then save it in the server.
How do i achieve this?what should be my approach?
Thanks.

Whatever you do on the server side, do realize that while the data is transfering over the wire, it will not be encrypted with your custom api. You will need to upload the file using an ssl connection in order ensure the data transfer is secure.

if you have your own custom encryption API, you should use it to encrypt in a temporary file and then upload the temporary encrypted result, and delete it after upload complete.

The FileUpload control gives you a handle to the file. Read the data from the file stream, encrypt it and write it to a FileStream. If the encryption process is CPU intensive and/or takes a lot of time, save the file in a temp directory and start a new thread that reads the file, encrypts its contents, saves it to a new file and deletes the temp file.

maybe u should write your own encryptor as an app and then decrypt that file on the server
this article could help u : file encrypt / decrypt

Related

C# open an encrypted file without writing the file to the disk?

I have encrypted a file with c# and gave it the extention .crypted. The file extention .crypted is associated with my program. So if i double click the file i get a password request dialog from my program. After entering the correct password my program decrypts the file and writes it unencrypted to the disk without the .crypted extention, so that i can run it. After using the file i just delete the unencrypted file and if the file was altered i use my program to reencrypt the file first. But u can easily use a file recovery program to get unauthorized access to the unencrypted file. Thats why i dont want to write unencrypted files to the disk after my program decrypts them. Is it possible to open an encrypted file without ever writing it to the disk? Like saving the unencrypted file to the ram/memory and opening it from there? I dont want to write a whole file system for that. The encrypted files should stay on NTFS or ext4. I think i need to change the code at this point:
using(CryptoStream cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
{
// write unencrypted file to memory and execute or something else?
}

How do you open a file that is stored as blob in the db (SQL server) in new browser window?

When opening a file in new browser window that is stored in the file system with the url stored in the database, the solution is simple with the javascript method:
[var windowObjectReference = window.open(strUrl, strWindowName\[, strWindowFeatures\]);][1]
However, when storing files as blob in the database (SQL Server) there is no url pointing to the file. What is the best practice for retrieving the file for the user? Do I need to copy the file to a temp folder and then provide the url dynamically? Then delete the file after a certain point?
I know that storing the file in the file system would be easier to code, but I have chosen to store it as a blob for all the benefits that come with a DBMS.
Thanks for your help
Your web server will have to write the BLOB's bytes to a response stream, plus an appropriate Content-Type and Content-Length header so that the browser knows how to treat those bytes. Use a tool like Fiddler and browse to google.com. You can see what Google's responses contains for images on the page. Your goal is to generate a response in a similar fashion.

ASP.NET data corruption via WebClient.OpenWriteCompleted

Trying to upload a data byte[] to ASP.NET via "WebClient" and OpenWriteCompleted in Silverlight5. If the data size is smaller all the data gets written correctly. If I upload say a 500kb file I get data corruption.
How do I upload a 500kb file to ASP.NET ??
NOTE: I'm trying to upload a zip file into a MSSQL varbinary(MAX) column in my database.
Never mind, it was a zip compression bug on my part.

C# - Accessing a DBF file in a Stream

I have a WebMethod which I use to allow a user to upload a database file (*.DBF). I know I can access the *.DBF file using a OleDbConnection with the appropriate connection string. But I dont really want to save the file on the server, my WebMethod has the file contents in a stream. Is there anyway I can access the *.DBF file while it is in the stream ?

Upload/Download data directly to Sql Server

I'm working on a server/client application, in which the client has the ability to upload/download a file, which will be stored in SQL server (2012) as varbinay(max). The problem is that I want that the file will be uploaded directly to the database without saving the file on the server's hard drive, using ReadAllBytes method, which only accepts a path parameter.
Here is a fragment of the code used in the server side:
HttpPostedFile file1 = context.Request.Files[0];
byte[] buffer = new byte[file1.ContentLength];
file1.InputStream.Read(buffer, 0, file1.ContentLength);
Here is the code user in order to write into a file from data in the database:
foreach (var file in list)
{
System.IO.File.WriteAllBytes(path + file.FileName, file.FileContents);
}
The HttpPostedFile comes with an input stream you can read from. Read it into a byte array, then store it in the database. Or if your database interface (in the case of EF this is unlikely) directly accepts a stream, just put it in as-is,
Do note that ASP.NET (or was it IIS?) may store a file upload temporarily on disk anyways, if it's very large.

Categories