When I try to return the value in bytes in the decrypt after the encrytion is done, it gives an error as "length of the data to decrypt is invalid."or"Bad Data"
The code is as follows
#region Decrypt
internal static byte[] Decrypt(string inFile)
{
FileStream inFileStream = new FileStream(inFile, FileMode.Open, FileAccess.Read);
MemoryStream outMemoryStream = new MemoryStream();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
Dictionary<string,> memDict = new Dictionary<string,>();
CryptoStream cs = new CryptoStream(outMemoryStream, tdes.CreateDecryptor(Encoding.UTF8.GetBytes(sharedkey), sharedvector), CryptoStreamMode.Write);
int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = inFileStream.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
cs.FlushFinalBlock();
cs.Close();
inFileStream.Close();
byte[] inByte = new byte[8192];
inByte = outMemoryStream.ToArray();
return inByte;
}
#endregion
#region Encrypt
public static byte[] Encrypt(string inFile)
{
FileStream inFileStream = new FileStream(inFile, FileMode.Open, FileAccess.Read);
MemoryStream outMemoryStream = new MemoryStream();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
Dictionary<string,> memDict = new Dictionary<string,>();
CryptoStream cs = new CryptoStream(outMemoryStream, tdes.CreateEncryptor(Encoding.UTF8.GetBytes(sharedkey), sharedvector), CryptoStreamMode.Write);
int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = inFileStream.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
cs.FlushFinalBlock();
cs.Close();
inFileStream.Close();
byte[] inByte = new byte[8192];
inByte = outMemoryStream.ToArray();
return inByte;
}
#endregion