This question already has answers here:
How to remove illegal characters from path and filenames?
(30 answers)
Closed last month.
I have a problem trying to create a file via FileStream, for some reason when the variable time receives the data in a way everything works fine, but if I close the program and open it again and that variable comes from a csv file I get this error : "System.ArgumentException: 'Illegal characters in path.' I did a MessageBox.Show on the pathSave variable and the result is correct "C:\Users\Felipe\AppData\Local\Temp\diskSTLQUZFTOV.vhd" I don't know why it's not showing this way via csv.
private void FileEncrypt(string outFile, string password, string time)
{
byte[] salt = GenerateRandomSalt();
FileStream fsCrypt = new FileStream(outFile, FileMode.Create);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.PKCS7;
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CFB;
fsCrypt.Write(salt, 0, salt.Length);
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
string pathSave = Path.GetTempPath() + "disk" + time + ".vhd";
MessageBox.Show(pathSave);
FileStream fsIn = new FileStream(pathSave, FileMode.Open);
byte[] buffer = new byte[1048576];
int read;
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
Application.DoEvents();
cs.Write(buffer, 0, read);
}
fsIn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Unmount Encryption - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cs.Close();
fsCrypt.Close();
}
}
You can simply replace any invalid characters from the path before saving like this:
char[] invalidChars = Path.GetInvalidFileNameChars();
string correctedFormatTime = time;
foreach (char c in invalidChars)
{
correctedFormatTime = correctedFormatTime.Replace(c.ToString(),"");
}
string pathSave = Path.GetTempPath() + "disk" + sanitizedTime + ".vhd";
Related
I'm getting the infamous "Padding is invalid and cannot be removed." error. However, despite ensuring the padding mode is set as well as verifying the key is the same on both encrypt and decrypt. I can't get past the issue.
What am I doing wrong o' great sages of the internet?
//Targeting .Net 4.5.2
//Key is hard coded, ONLY while working through this error in a minimum repo.
byte[] testKey = Convert.FromBase64String("KN1df3fOkLmSPyOP4r+grlVFDC/JVlWuew1u/hDGvUU=");
//Called to Encrypt ("D:\\Assets\\", "Test.txt")
public void DoWork(string filePath, string fileName){
CryptographyUtil crypto = new CryptographyUtil(testKey);
crypto.EncryptFile(filePath, fileName));
}
//Called to Decrypt ("D:\\Assets\\", "Test.txt.dat")
public void UnDoWork(string filePath, string fileName){
CryptographyUtil crypto = new CryptographyUtil(testKey);
crypto.DecryptFile(filePath, fileName));
}
public class CryptographyUtil(){
RijndaelManaged rjndl;
RNGCryptoServiceProvider cRng;
public CryptographyUtil(byte[] key, int keySize = 256, int blockSize = 256) {
cRng = new RNGCryptoServiceProvider();
rjndl = new RijndaelManaged();
rjndl.Key = key;
rjndl.KeySize = keySize;
rjndl.BlockSize = blockSize;
rjndl.Mode = CipherMode.CBC;
rjndl.Padding = PaddingMode.PKCS7;
}
public void EncryptFile(string filePath, string fileName) {
string inputFilePath = Path.Combine(filePath, fileName);
if(!File.Exists(inputFilePath)) {
throw new FileLoadException("Unable to locate or open file.", inputFilePath);
}
string outputDirectory = Path.Combine(filePath, "Encrypted");
if(!Directory.Exists(outputDirectory)){
Directory.CreateDirectory(outputDirectory);
}
string outputPath = Path.Combine(outputDirectory, fileName + ".dat");
//Create a unique IV each time
byte[] iv = new byte[rjndl.BlockSize / 8]
cRng.GetBytes(iv);
byte[] ivSize = BitConverter.GetBytes(iv.Length);
ICryptoTransform encryptor = rjndl.CreateEncryptor(rjndl.Key, iv);
using(FileStream readStream = File.OpenRead(inputFilePath)) {
using(FileStream writeStream = new FileStream(outputPath, FileMode.Create)) {
using(CryptoStream encryptStream = new CryptoStream(writeStream, encryptor, CryptoStreamMode.Write)) {
//Write the following to the file before the encrypted data:
// - length of the IV
// - the IV
writeStream.Write(ivSize, 0, ivSize.Length);
writeStream.Write(iv, 0, iv.Length);
readStream.CopyTo(encryptStream);
readStream.Flush();
encryptStream.FlushFinalBlock();
encryptStream.Close();
}
writeStream.Close();
}
readStream.Close();
}
}
public void DecryptFile(string filePath, string fileName) {
string outputDirectory = Path.Combine(filePath, "Decrypted");
if(!Directory.Exists(outputDirectory)){
Directory.CreateDirectory(outputDirectory);
}
//Remove the ".dat" from the end of the file
string outputFilePath = Path.Combine(outputDirectory, fileName.Substring(0, fileName.LastIndexOf('.')));
if(File.Exists(outputFilePath)){
File.Delete(outputFilePath);
}
using(FileStream readStream = File.OpenRead(Path.Combine(filePath, fileName))) {
//Size buffer for IV Length (int = 4 bytes)
byte[] buffer = new byte[4];
readStream.Read(buffer, 0, buffer.Length);
int ivLength = BitConverter.ToUInt16(buffer, 0);
//Re-size buffer for IV
buffer = new byte[ivLength];
//Read IV to buffer and use to create decryptor
readStream.Read(buffer, 0, ivLength);
//Use IV in buffer to create decryptor
ICryptoTransform decryptor = rjndl.CreateDecryptor(rjndl.Key, buffer);
buffer = new byte[1024];
using(FileStream writeStream = new FileStream(outputFilePath, FileMode.Create)) {
using(CryptoStream decryptStream = new CryptoStream(readStream, decryptor, CryptoStreamMode.Read)) {
//FIXME: Padding Error
int readIdx = decryptStream.Read(buffer, 0, buffer.Length);
while(readIdx > 0) {
writeStream.Write(buffer, 0, readIdx);
readIdx = decryptStream.Read(buffer, 0, buffer.Length);
}
decryptStream.Flush();
decryptStream.Close();
}
writeStream.Close();
}
readStream.Close();
}
}
}
Using RijndaelManaged as I have to create encrypted packages for legacy software that uses it to decrypt.
I have function for decrypting files using Rijnadel (AES).
Looks like this:
public static bool FileDecrypt(string inputFile, string outputFile, string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] salt = new byte[32];
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
fsCrypt.Read(salt, 0, salt.Length);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CFB;
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int read;
byte[] buffer = new byte[1048576];
try
{
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
fsOut.Write(buffer, 0, read);
}
}
catch (CryptographicException ex_CryptographicException)
{
fsOut.Close();
fsCrypt.Close();
LogWriter loger = new LogWriter("Cryptography error: " + ex_CryptographicException.ToString());
return false;
}
catch (Exception ex)
{
fsOut.Close();
fsCrypt.Close();
LogWriter loger = new LogWriter("Exception error: " + ex.ToString());
return false;
}
try
{
cs.Close();
}
catch (Exception ex)
{
fsCrypt.Close();
fsOut.Close();
LogWriter loger = new LogWriter("Error when closing Cryptostream. Error: " + ex.ToString());
return false;
}
finally
{
fsOut.Close();
fsCrypt.Close();
}
return true;
}
It decrypts the file and creates new decrypted file.
But i would like to decrypt that file and load it only into memory. Mostly xml configuration files so then i would like to read the xml configuration with xmlserializer and use it in my program and then dispose the file from memory. Is that possible? Thanks for all answers.
PS: Dont mind closing of streams in all catches. Finally block didnt work for some reason, still trying to figure that out.
//EDIT:
This is my try:
MemoryStream inMemoryCopy = new MemoryStream();
byte[] salt = new byte[32];
using (FileStream fs = File.OpenRead(inputfile))
{
fs.Read(salt, 0, salt.Length);
fs.CopyTo(inMemoryCopy);
}
byte[] bytesToBeDecrypted = inMemoryCopy.ToArray();
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CFB;
CryptoStream cs = new CryptoStream(bytesToBeDecrypted, AES.CreateDecryptor(), CryptoStreamMode.Read);
Problem is i cannot use byte array in cryptostream. And can i somehow do that even without filestream?
You want to use XmlSerializer kinda like this to get some configuration object from stream. It doesn't matter wether the source stream is a FileStream, NetworkStream or even the CryptoStream itself. So there is no need to work with a byte buffer.
Note the following code is adapted from your code. I stripped the try catch from it to just only show the core code needed for the deserialization.
public SomeConfig ReadEncryptedConfiguration(string inputFile, string password)
{
using (var stream = CreateStream(inputFile, password))
{
var ser = new XmlSerializer(typeof(SomeConfig));
var config = (SomeConfig) ser.Deserialize(stream);
return config;
}
}
The CreateStream method will make the stream which is used and disposed in the previous part. I used another constructor to make sure that the underlying file stream is disposed when the CryptoStream is being disposed.
public CryptoStream CreateStream(string inputFile, string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] salt = new byte[32];
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
fsCrypt.Read(salt, 0, salt.Length);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CFB;
return new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read, false);
}
I have issues to get the data decrypted and not sure what I do wrong as tried almost everything.
private static byte[] AES_Encrypt(byte[] byteArray)
{
byte[] salt = GenerateRandomSalt();
MemoryStream fsCrypt = new MemoryStream();
byte[] ms = new byte[0];
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(keyName);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.None;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CFB;
fsCrypt.Write(salt, 0, salt.Length);
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
MemoryStream fsIn = new MemoryStream(byteArray);
byte[] buffer = new byte[1048576];
int read;
try
{
if (byteArray.Length <= buffer.Length)
cs.Write(buffer, 0, buffer.Length);
else
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
cs.Write(buffer, 0, read);
}
ms = fsCrypt.ToArray();
fsIn.Close();
}
catch (Exception ex)
{
}
finally
{
try
{
cs.Close();
fsCrypt.Close();
}
catch (Exception err)
{
}
}
string response = Encoding.UTF8.GetString(ms.ToArray());
return ms.ToArray();
}
private static byte[] AES_Decrypt(byte[] byteArray)
{
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(keyName);
byte[] salt = new byte[32];
byte[] ms = new byte[0];
MemoryStream fsCrypt = new MemoryStream(byteArray);
fsCrypt.Read(salt, 0, salt.Length);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.None;
AES.Mode = CipherMode.CFB;
MemoryStream fsOut = new MemoryStream();
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
int read;
byte[] buffer = new byte[1048576];
try
{
string response = Encoding.UTF8.GetString(byteArray);
if (byteArray.Length <= buffer.Length)
{
while ((read = cs.Read(byteArray, 0, byteArray.Length)) > 0)
{
fsOut.Read(byteArray, 0, read);
}
string vresponse = Encoding.UTF8.GetString(fsOut.ToArray());
}
else
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
fsOut.Write(buffer, 0, read);
}
}
catch (System.Security.Cryptography.CryptographicException ex_CryptographicException)
{
}
catch (Exception ex)
{
}
try
{
cs.Close();
}
catch (Exception ex)
{
}
finally
{
fsOut.Close();
//fsCrypt.Close();
}
return ms.ToArray();
}
My data is sometimes shorter than the 1MB being used so I want to catch th shorter data to use less byte, however its for later concern.
The data I send to the decryptor is using the same static keys now and the inbound encrypted data is identical as the out type encrypted data.
If I put the PaddingMode.None; to PaddingMode.PKCS7 it gives me a mapping error, which comes as I assume in the shorter string than the buffer actually is, so as far my assumations this is correct, so I use for now 'none'.
I have looked to lots of links as:
https://stackoverflow.com/questions/31486028/decrypting-cryptostream-into-memorystream
https://stackoverflow.com/questions/31486028/decrypting-cryptostream-into-memorystream
https://stackoverflow.com/questions/8583112/padding-is-invalid-and-cannot-be-removed
[Encrypt to memory stream, pass it to file stream
[AES encryption on large files
The problem I cant figure out why the encrypted data wont decrypt as should.
100% sure I do something not correct but cant figure out what, any help would be appriciated.
I have changes some code and tried different options, now using this but on both option I get errors.
using (var msi = new MemoryStream())
{
try
{
using (var cs = new CryptoStream(msi, aesAlg.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytes, 0, bytes.Length); //Option 1, gives error, length is invalid of byte array
do //Option 2, give error, Padding is invalid and cannot be removed.
{
count = msi.Read(bytes, 0, blockSizeBytes);
offset += count;
cs.Write(data, 0, count);
}
while (count > 0);
}
var plainText = msi.ToArray();
var text = Encoding.Unicode.GetString(plainText);
}
catch (Exception e)
{
}
//return text;
}
I checked the data length and using Unicode encoding it is 4258 and the bytes are 8516, which should be from my point of view as using Unicode.
Not sure if using the right encoding as found this https://stackoverflow.com/a/10380166/3763117 and can do without but gives same lengthas unicode. little stucked on this any help with some samples would be appriciated.
I am attempting to decrypt a file to ONLY the process memory. I do not want the actual file to be sent to plain text as it will be storing sensitive data. I do not want the raw text sitting on the system at all.
I am currently testing with a eula file in C:\ BUT get the same issue no matter what file I use.
I am using AES with salting. Decrypting the file does work as right now I am dumping the decrypted data to the text document but when I am attempting to compile the decrpytedBytes into a string, it only outputs 3 characters that are non-existent in that order anywhere inside of the document.
https://i.imgur.com/WAQ2njB.png
Those 2 characters show up while using System.Text.Encoding.UTF8.GetString(bytesDecrypted, 0, bytesDecrypted.Length) to compile the byte array to a string.
I have attempted just a basic .ToString() but that returned System.Byte[] and nothing more
https://i.imgur.com/Gg5Et72.png
While using var str = System.Text.Encoding.Default.GetString(bytesDecrypted) it only outputs ÿþ*
https://i.imgur.com/94hMuB3.png
Here is the code I am using for encryption and decryption
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
public void EncryptFile(string file, string fileEncrypted, string password)
{
byte[] bytesToBeEncrypted = File.ReadAllBytes(file);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
File.WriteAllBytes(fileEncrypted, bytesEncrypted);
listBox1.Items.Add("Enrypted the file");
}
public void DecryptFile(string fileEncrypted, string file, string password)
{
byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
listBox1.Items.Add("Attempting Decryption");
File.WriteAllBytes(file, bytesDecrypted);
var str = System.Text.Encoding.Default.GetString(bytesDecrypted);
richTextBox1.Text = str;
}
If you have any idea/clues on how I could manage to get this working I would greatly appreciate it!
You used the incorrect encoding to to decode your decrypted byte array. The encoding of the original text file is most likely Unicode/UTF-16. Thus, use the Encoding.Unicode encoding to decode the decrypted byte array back to text:
var str = System.Text.Encoding.Unicode.GetString(bytesDecrypted);
Some background information
So, what made me think that the encoding of the original text file is UTF-16/Unicode?
This information from the question gives a crucial hint:
While using var str = System.Text.Encoding.Default.GetString(bytesDecrypted) it only outputs ÿþ*
Note the ÿþ. This is how an UTF-16 LE BOM (*) appears if text data having this BOM is decoded/shown using the ISO/IEC 8859-1 (or CP-1252) code page, which often is the default code page used in many (english/non-localized) Windows installations.
(*) The UTF-16 LE BOM (UTF-16 Little-Endian Byte Order Mark) are two bytes 0xFF,0xFE. To learn more about what BOMs are and what their purpose is, i suggest this Wikipedia article: https://en.wikipedia.org/wiki/Byte_order_mark
Found this answer that I think applies to your issue. Pay special attention to "encryptedData = output.ToArray();"
Reading from a cryptostream to the end of the stream
byte[] encryptedData;
rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
rijCrypto.KeySize = 256;
using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData)))
using (var output = new MemoryStream())
{
var encryptor = rijCrypto.CreateEncryptor();
using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
{
var buffer = new byte[1024];
var read = input.Read(buffer, 0, buffer.Length);
while (read > 0)
{
cryptStream.Write(buffer, 0, read);
read = input.Read(buffer, 0, buffer.Length);
}
cryptStream.FlushFinalBlock();
encryptedData = output.ToArray();
}
}
I am trying to encrypt and decrypt the file. Using AES methods in a MVC web application. I am able to encrypt file and decrypt only once. If I try for second time it gives me "Padding is invalid and cannot be removed error."
I have tried almost every combination with different properties of AES properties.
I have tried using statement for disposing the objects.
I have tried using FlushFinalBlock() after CryptoStream write.
I have tried with using AES.Padding to Zero (gives me no error but the file doesnt decrypt ), AES.Padding to None gives me error('Length of the data to encrypt is invalid.'). AES.Padding to PKCS7 gives me error (padding is invalid.)
Please find my code below.
public class EncryptionDecryption
{
// Call this function to remove the key from memory after use for security
[DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
/// <summary>
/// Creates a random salt that will be used to encrypt your file. This method is required on FileEncrypt.
/// </summary>
/// <returns></returns>
public static byte[] GenerateRandomSalt()
{
byte[] data = new byte[32];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
// Ten iterations.
for (int i = 0; i < 10; i++)
{
// Fill buffer.
rng.GetBytes(data);
}
}
return data;
}
/// <summary>
/// Encrypts a file from its path and a plain password.
/// </summary>
/// <param name="inputFile"></param>
/// <param name="password"></param>
public static void FileEncrypt(string inputFile, string password)
{
//generate random salt
byte[] salt = GenerateRandomSalt();
//create output file name
using (FileStream fsCrypt = new FileStream(inputFile + ".aes", FileMode.Create))
{
//convert password string to byte arrray
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
//Set Rijndael symmetric encryption algorithm
using (AesManaged AES = new AesManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.None;
//http://stackoverflow.com/questions/2659214/why-do-i-need-to-use-the-rfc2898derivebytes-class-in-net-instead-of-directly
//"What it does is repeatedly hash the user password along with the salt." High iteration counts.
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
//Cipher modes: http://security.stackexchange.com/questions/52665/which-is-the-best-cipher-mode-and-padding-mode-for-aes-encryption
AES.Mode = CipherMode.CBC;
// write salt to the begining of the output file, so in this case can be random every time
fsCrypt.Write(salt, 0, salt.Length);
using (CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
//create a buffer (1mb) so only this amount will allocate in the memory and not the whole file
byte[] buffer = new byte[1048576];
int read;
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
// Application.DoEvents(); // -> for responsive GUI, using Task will be better!
cs.Write(buffer, 0, read);
}
// Close up
fsIn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
if (!cs.HasFlushedFinalBlock)
cs.FlushFinalBlock();
cs.Close();
fsCrypt.Close();
}
}
}
}
}
}
/// <summary>
/// Decrypts an encrypted file with the FileEncrypt method through its path and the plain password.
/// </summary>
/// <param name="inputFile"></param>
/// <param name="outputFile"></param>
/// <param name="password"></param>
public static void FileDecrypt(string inputFile, string outputFile, string password)
{
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] salt = new byte[32];
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
fsCrypt.Read(salt, 0, salt.Length);
using (AesManaged AES = new AesManaged ())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CBC;
using (CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
int read;
byte[] buffer = new byte[1048576];
try
{
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
//Application.DoEvents();
fsOut.Write(buffer, 0, read);
//if (!cs.HasFlushedFinalBlock)
cs.FlushFinalBlock();
}
}
catch (CryptographicException ex_CryptographicException)
{
Console.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
try
{
cs.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error by closing CryptoStream: " + ex.Message);
}
finally
{
fsOut.Close();
fsCrypt.Close();
}
}
}
}
}
}
}
calling methods
Encryption
string password = "ThePasswordToDecryptAndEncryptTheFile";
// For additional security Pin the password of your files
GCHandle gch = GCHandle.Alloc(password, GCHandleType.Pinned);
// Encrypt the file
EncryptionDecryption.FileEncrypt(inputFilePath, password);
// To increase the security of the encryption, delete the given password from the memory !
EncryptionDecryption.ZeroMemory(gch.AddrOfPinnedObject(), password.Length * 2);
gch.Free();
Decryption
GCHandle gch2 = GCHandle.Alloc(password, GCHandleType.Pinned);
// Decrypt the file
EncryptionDecryption.FileDecrypt(encryptedFilePath, outputPath, password);
// To increase the security of the decryption, delete the used password from the memory !
EncryptionDecryption.ZeroMemory(gch2.AddrOfPinnedObject(), password.Length * 2);
gch2.Free();
I got it resolved by removing all the AES properties to default and just keeping few. Find the code below. Specially the padding fields.
public static void FileEncrypt(string inputFile, string outputFile, string password, byte[] salt)
{
try
{
using (RijndaelManaged AES = new RijndaelManaged())
{
byte[] passwordBytes = ASCIIEncoding.UTF8.GetBytes(password);
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
/* This is for demostrating purposes only.
* Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
//byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform encryptor = AES.CreateEncryptor(AES.Key, AES.IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
{
using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
{
int data;
while ((data = fsIn.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
if (!cs.HasFlushedFinalBlock)
cs.FlushFinalBlock();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void FileDecrypt(string inputFile, string outputFile, string password, byte[] salt)
{
try
{
using (RijndaelManaged AES = new RijndaelManaged())
{
//byte[] key = ASCIIEncoding.UTF8.GetBytes(password);
/* This is for demostrating purposes only.
* Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/
//byte[] IV = ASCIIEncoding.UTF8.GetBytes(password);
byte[] passwordBytes = ASCIIEncoding.UTF8.GetBytes(password);
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
using (ICryptoTransform decryptor = AES.CreateDecryptor(AES.Key, AES.IV))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
if (!cs.HasFlushedFinalBlock)
cs.FlushFinalBlock();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// failed to decrypt file
}
}
}