Reputation: 2308
I need to create a encrypted file container in an asp net mvc app.
I found this program that does the job
http://www.michaelcodes.net/post/TrueCrypt-6a-System-Encryption-Benchmarks.aspx
but it should be integrated with .net
Is there any way to accomplish that using .NET libraries? I have not found how.
Features:
Upvotes: 3
Views: 3389
Reputation: 6416
You can put your files in a list of custom class:
[Serializable]
public class FileEntry
{
public string FileName {get;set;}
public string FileRelativePath {get;set;}
public byte[] FileContents {get;set;}
}
Add the files to a list:
List<FileEntry> files = new List<FileEntry> ();
for .......
{
files.Add(new FileEntry()
{
FileName = .....,
FileRelativePath = .....,
FileContents = File.ReadAllBytes(......),
};
}
And then, use BinarryFormatter to convert this structure to byte-array:
byte[] filesBytes;
BinaryFormatter ser = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream())
{
ser.Serialize(ms, files);
filesBytes = ms.ToArray();
}
Now, you have your structure as byte[], you can easily encrypt them with some easy way as this:
filesBytes = Encrypt(filesBytes , ......);
Then save the encrypted bytes to some location with some custom extension:
File.WriteAllBytes(".........\.....encr",filesBytes);
Then, when you want to re-open the file and read the clear data:
byte[] encryptedData = File.ReadAllBytes(".......\.....encr");
Decrypt the contents with the same algorithm:
byte[] clearContent = Decrypt(encryptedData, ......);
And deserialize the contents into the primary structure:
BinaryFormatter ser = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream(clearContent))
{
List<FileEntry> files = ser.Deserialize(ms) as List<FileEntry>;
}
And then, write the content of the files to some location if you want:
foreach(var file in files)
{
File.WriteAllBytes(string.Format("........{0}...{1}",file.FileRelativePath , file.FileName), file.FileContents)
}
You can use this question about encryption:
Easy way to encrypt/obfuscate a byte array using a secret in .NET?
And this is an example about binary formatter:
I have posted this answer to my blog :)
Upvotes: 3
Reputation: 18543
If you want to use TrueCrypt, you can automatically mount on system startup to a drive and then use regular file operations on the mapped drive.
If you don't want to mount the volume with public access (access external to the application in question), then you will indeed need more in-depth API access.
I wouldn't recommend rolling your own C# implementation from scratch. To get the level of security refined in TrueCrypt you need skill AND time - both of which have gone into TC. If you don't use both you can easily open yourself up to big security risks.
You may be able to simply use auto-interop by importing a dll (although I cannot be sure). I believe TC is open source, if so you may outsource a C/C++ coder to write a .Net wrapper - one that doesn't require mounting the volume, but allows internal-application-only file-based access.
Upvotes: 1