Click here to Skip to main content
15,891,372 members
Home / Discussions / C#
   

C#

 
Questionfile sending Pin
Vivek Vijayan18-Aug-09 8:14
Vivek Vijayan18-Aug-09 8:14 
AnswerRe: file sending Pin
Xmen Real 18-Aug-09 8:57
professional Xmen Real 18-Aug-09 8:57 
AnswerRe: file sending Pin
Ian Shlasko18-Aug-09 9:00
Ian Shlasko18-Aug-09 9:00 
AnswerRe: file sending Pin
N a v a n e e t h18-Aug-09 15:39
N a v a n e e t h18-Aug-09 15:39 
AnswerRe: file sending Pin
Rajesh R Subramanian18-Aug-09 17:41
professionalRajesh R Subramanian18-Aug-09 17:41 
QuestionDataTable Find Connected Object Pin
I Believe In GOD18-Aug-09 7:21
I Believe In GOD18-Aug-09 7:21 
AnswerRe: DataTable Find Connected Object Pin
Moreno Airoldi18-Aug-09 7:47
Moreno Airoldi18-Aug-09 7:47 
QuestionInvalid Padding when decrypting Pin
musefan18-Aug-09 6:29
musefan18-Aug-09 6:29 
"Padding is invalid and cannot be removed" - this is the error message I am getting when trying to decrypt a file.

I have an application which will encrypt a file and then copy the file to a network location. (No Problems)
The application can also 'download' the file and decrypt it - This is done by copying the file locally (with .tmp appended to the filename) and then decrypted to another file (so you basically end up with encrypted and decrypted files when you 'download').

If I upload a file, then I can download without problems. But I cannot download a file that another user has uploaded. Now, as I said you get two files when you download. If I use a testing application (which has exactly the same decryption code) then I can successfully decrypt the temporary file that was created during the 'download'

I cannot see the logic here at all. Please see my two methods below for encryption/decryption...

public static bool EncryptFile(string inputFile, string outputFile)
		{
			PasswordDeriveBytes pdb = new PasswordDeriveBytes(fileEncryptionPassword, new System.Security.Cryptography.SHA384Managed().ComputeHash(new System.Text.UnicodeEncoding().GetBytes(fileEncryptionPassword)));
			byte[] key = pdb.GetBytes(32);
			byte[] IV = pdb.GetBytes(16);
			
			RijndaelManaged rm = new RijndaelManaged();
			rm.Key = key;
			rm.IV = IV;
			rm.BlockSize = 128;
			rm.Padding = PaddingMode.PKCS7;
			
			FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
			FileStream fsOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
			
			bool success = true;
			
			try{
				CryptoStream cs = new CryptoStream(fsOut, rm.CreateEncryptor(), CryptoStreamMode.Write);
				
				int bytesToWrite = 0;
				byte[] buffer = new byte[4096];
				
				while(true)
				{
					bytesToWrite = fsIn.Read(buffer, 0, buffer.Length);
					if(bytesToWrite == 0)
						break;
					cs.Write(buffer, 0, bytesToWrite);
				}
				
				cs.Close();
			}
			catch
			{
				success = false;
			}
			fsIn.Close();
			fsOut.Close();
			
			if(!success)
				File.Delete(outputFile);
			
			return success;
		}


public static bool DecryptFile(string inputFile, string outputFile)
		{
			PasswordDeriveBytes pdb = new PasswordDeriveBytes(fileEncryptionPassword, new System.Security.Cryptography.SHA384Managed().ComputeHash(new System.Text.UnicodeEncoding().GetBytes(fileEncryptionPassword)));
			byte[] key = pdb.GetBytes(32);
			byte[] IV = pdb.GetBytes(16);
			
			RijndaelManaged rm = new RijndaelManaged();
			
			rm.Key = key;
			rm.IV = IV;
			rm.BlockSize = 128;
			rm.Padding = PaddingMode.PKCS7;
			
			FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
			FileStream fsOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
			
			bool success = true;
			
			CryptoStream cs = new CryptoStream(fsIn, rm.CreateDecryptor(), CryptoStreamMode.Read);
			
			try{
				int bytesToWrite = 0;
				byte[] buffer = new byte[4096];
				
				while(true)
				{
					bytesToWrite = cs.Read(buffer, 0, buffer.Length);
					if(bytesToWrite == 0)
						break;
					fsOut.Write(buffer, 0, bytesToWrite);
				}
			}
			catch(Exception ex)
			{
				Debug.Show(ex.Message + "\n\n" + ex.StackTrace + "\n\n" + ex.InnerException.Message);
				success = false;
			}
			finally{
				cs.Close();
			}
			
			fsIn.Close();
			fsOut.Close();
			
			if(!success)
				File.Delete(outputFile);
			
			return success;
		}


Sorry for so much code, but I'm sure it will be useful.

The decryption method is called directly after the code that copies the file from the network to a local folder, this is done using File.Copy() with the overwrite property set to true.

Thanks for any help

Life goes very fast. Tomorrow, today is already yesterday.

AnswerRe: Invalid Padding when decrypting Pin
DaveyM6918-Aug-09 7:08
professionalDaveyM6918-Aug-09 7:08 
GeneralRe: Invalid Padding when decrypting Pin
Hristo-Bojilov18-Aug-09 7:21
Hristo-Bojilov18-Aug-09 7:21 
GeneralRe: Invalid Padding when decrypting Pin
musefan18-Aug-09 7:23
musefan18-Aug-09 7:23 
AnswerRe: Invalid Padding when decrypting Pin
musefan19-Aug-09 1:29
musefan19-Aug-09 1:29 
QuestionHow to identify the active tab in IE? Pin
Jacobb Michael18-Aug-09 6:22
Jacobb Michael18-Aug-09 6:22 
Questionsort list Pin
nikhil123418-Aug-09 5:13
nikhil123418-Aug-09 5:13 
AnswerRe: sort list Pin
Hristo-Bojilov18-Aug-09 5:46
Hristo-Bojilov18-Aug-09 5:46 
AnswerRe: sort list Pin
Luc Pattyn18-Aug-09 13:19
sitebuilderLuc Pattyn18-Aug-09 13:19 
QuestionGetting Authentication Failure error message. Pin
V K 218-Aug-09 5:10
V K 218-Aug-09 5:10 
AnswerRe: Getting Authentication Failure error message. Pin
Saksida Bojan18-Aug-09 7:12
Saksida Bojan18-Aug-09 7:12 
AnswerRe: Getting Authentication Failure error message. Pin
Luc Pattyn18-Aug-09 13:20
sitebuilderLuc Pattyn18-Aug-09 13:20 
Question[Message Deleted] Pin
VengefulSakhmet18-Aug-09 5:06
VengefulSakhmet18-Aug-09 5:06 
AnswerRe: SQL Query of HKEY_LOCAL_MACHINE Pin
riced18-Aug-09 6:39
riced18-Aug-09 6:39 
AnswerRe: SQL Query of HKEY_LOCAL_MACHINE Pin
Dave Kreskowiak18-Aug-09 7:12
mveDave Kreskowiak18-Aug-09 7:12 
QuestionCheck when webservice method is completed Pin
kstxx18-Aug-09 4:26
kstxx18-Aug-09 4:26 
AnswerRe: Check when webservice method is completed Pin
Pete O'Hanlon18-Aug-09 4:34
mvePete O'Hanlon18-Aug-09 4:34 
GeneralRe: Check when webservice method is completed Pin
kstxx18-Aug-09 4:40
kstxx18-Aug-09 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.