Reputation: 617
I used to make games in Microsoft's XNA. XNA had a ContentPipeline project built into it that automatically built all your image files to .XNB files. This had the advantage that users couldn't trivially modify image files (e.g. in Paint).
Now that I'm coding in C#/OpenTK, I use the Copy-To-Output functionality in Visual Studio to copy my image, sound and font files for runtime use by my application. However I lose the protection that a custom format granted.
My question is how can I create something similar? Is there an off-the-shelf library I can use, or code I can write to create this?
Compression isn't especially important to me - just changing the extension to something custom is all I'm really after.
Upvotes: 1
Views: 121
Reputation: 4146
This expands upon Kyle's answer and your question to him about encryption routines.
If you want a simple Encrypt/Decrypt pattern, you can use this one. The keyString is your Salt that will allow you to decrypt the data at a later time. I got this code from a blog post about encryption, and while there may be better routines out there today, this one works well.
public static byte[] EncryptData(string keyString, byte[] dataToEncrypt)
{
if (keyString.IsNullOrEmptyTrimmed())
throw new ArgumentNullException("keyString", "You must provide a key string for salting.");
if (dataToEncrypt == null)
return null;
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encryptedData;
MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider();
byte[] tdesKey = hashProvider.ComputeHash(utf8.GetBytes(keyString));
TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider();
tdesAlgorithm.Key = tdesKey;
tdesAlgorithm.Mode = CipherMode.ECB;
tdesAlgorithm.Padding = PaddingMode.PKCS7;
try
{
ICryptoTransform encryptor = tdesAlgorithm.CreateEncryptor();
encryptedData = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
}
finally
{
tdesAlgorithm.Clear();
hashProvider.Clear();
}
return encryptedData;
}
public static byte[] DecryptData(string keyString, byte[] encyptedData)
{
if (encyptedData == null)
return null;
byte[] decryptedData;
UTF8Encoding utf8 = new UTF8Encoding();
MD5CryptoServiceProvider hashProvider = new MD5CryptoServiceProvider();
byte[] tdesKey = hashProvider.ComputeHash(utf8.GetBytes(keyString));
TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider();
tdesAlgorithm.Key = tdesKey;
tdesAlgorithm.Mode = CipherMode.ECB;
tdesAlgorithm.Padding = PaddingMode.PKCS7;
try
{
ICryptoTransform decryptor = tdesAlgorithm.CreateDecryptor();
decryptedData = decryptor.TransformFinalBlock(encyptedData, 0, encyptedData.Length);
}
finally
{
tdesAlgorithm.Clear();
hashProvider.Clear();
}
return decryptedData;
}
Upvotes: 1
Reputation: 2018
The easiest option is simply don't worry about it since custom extensions grant little to no protection anyway. Many programs will ignore the extension and let you edit the file regardless.
If you do need some (pseudo) protection, a common option is to encrypt the files and decrypt them at runtime. However, malicious users could still get to the files by reverse engineering your code.
Upvotes: 0