Upendra Patel

Upendra Patel

  • 2.1k
  • 38
  • 8.7k

I am not able to decrypt file in AES..

Jun 30 2017 10:16 AM
Dear Team,
 
Below is my code for encryption and decryption. but i am successfully ecrypted the file but not decrypted .
 
Below is my code .
 
Anyone have any idea ? 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var Mode = "";
bool Loop = true;
while (Loop)
{
Console.WriteLine("Encrypt or Decrypt? (E/D)");
Mode = Console.ReadLine();
if (Mode.ToUpper() == "E" || Mode.ToUpper() == "D")
switch (Mode.ToUpper())
{
case "E":
Mode = "Encrypt";
Loop = false;
continue;
case "D":
Mode = "Decrpt";
Loop = false;
continue;
}
else
Console.WriteLine("Must be E OR D");
}
//Console.WriteLine("What directory do you want to encrypt?");
//var fileDirectory = Console.ReadLine();
Console.WriteLine("What password do you want to use?");
var passWord = Console.ReadLine();
string[] files = Directory.GetFiles("C:\\Users\\1722\\Desktop\\t Document");
for (int i = 0; i < files.Length; i++)
{
byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]);
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesCyrpt = new byte[bytesToBeEncrypted.Length];
if (Mode == "Encrypt")
{
bytesCyrpt = AES_Encrypt(bytesCyrpt, passwordBytes);
File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt);
File.Delete(files[i]);
}
else
{
bytesCyrpt = AES_Decrypt(bytesCyrpt, passwordBytes);
File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt);
}
}
}
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
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;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
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.Padding = PaddingMode.None;
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;
}
private static string encryptedFileName(string fileName)
{
string new_file_name = string.Empty;
string extension = fileName.Split('.').Last();
new_file_name = fileName.Replace(extension, "");
new_file_name = new_file_name + "_encrypt." + extension;
return new_file_name;
}
private static string decryptFileName(string fileName)
{
string new_file_name = fileName;
new_file_name = new_file_name.Replace("_encrypt", "");
return new_file_name;
}
}
}
 

Answers (3)