Skip to content

Instantly share code, notes, and snippets.

@anishmm
Created October 28, 2017 09:40
Show Gist options
  • Save anishmm/7651927c1a151713e33a47ed2ea033d5 to your computer and use it in GitHub Desktop.
Save anishmm/7651927c1a151713e33a47ed2ea033d5 to your computer and use it in GitHub Desktop.
file decrypt code in C#
public static bool Decrypte(string fileIn, string fileOut)
{
string Password = "mypassword";
bool msg = true;
FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read);
FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write);
try
{
int bufferLen = 2048;
byte[] buffer = new byte[bufferLen];
int bytesRead;
long FileLenght = fsIn.Length;
bool isPercent = FileLenght > int.MaxValue ? true : false;
int inc = 0;
long totalBytes = 0;
byte[] dummybuffer = { 34 };
int ittaration = 0;
int dummayinc = 1;
do
{
if (ittaration == 0)
{
bytesRead = fsIn.Read(buffer, 0, dummayinc);
ittaration = 1;
}
else
{
dummayinc = dummayinc + 1;
if (dummayinc > 10)
dummayinc = 1;
bytesRead = fsIn.Read(buffer, 0, bufferLen);
ittaration = 0;
}
totalBytes = totalBytes + bytesRead;
if (isPercent)
{
double d = Convert.ToDouble(totalBytes * 100.0 / FileLenght);
if (d > 100)
d = 100;
inc = (int)d;
}
else
inc = (int)totalBytes;
// Decrypt it
if (ittaration == 0)
{
fsOut.Write(buffer, 0, bytesRead);
}
fsOut.Flush();
} while (bytesRead != 0);
if (fsIn != null)
fsIn.Dispose();
if (fsOut != null)
fsOut.Dispose();
msg = true;
}
catch
{
if (fsIn != null)
fsIn.Dispose();
msg = false;
}
return msg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment