Click here to Skip to main content
15,923,142 members
Home / Discussions / C#
   

C#

 
GeneralSelf Upgrading Executables (C# Windows App) Pin
OBRon1-Oct-03 9:45
OBRon1-Oct-03 9:45 
GeneralRe: Self Upgrading Executables (C# Windows App) Pin
SimonS2-Oct-03 1:24
SimonS2-Oct-03 1:24 
GeneralRe: Self Upgrading Executables (C# Windows App) Pin
OBRon2-Oct-03 2:54
OBRon2-Oct-03 2:54 
QuestionCOM Ports in C# - How ? Pin
raheela1-Oct-03 9:42
raheela1-Oct-03 9:42 
GeneralArray question Pin
Joe Woodbury1-Oct-03 7:56
professionalJoe Woodbury1-Oct-03 7:56 
GeneralRe: Array question Pin
jparsons1-Oct-03 9:20
jparsons1-Oct-03 9:20 
GeneralRe: Array question Pin
Joe Woodbury1-Oct-03 10:59
professionalJoe Woodbury1-Oct-03 10:59 
GeneralRe: Array question Pin
Frank Olorin Rizzi2-Oct-03 4:12
Frank Olorin Rizzi2-Oct-03 4:12 
GeneralRe: Array question Pin
jparsons2-Oct-03 5:29
jparsons2-Oct-03 5:29 
GeneralRe: Array question Pin
Julian Bucknall [MSFT]2-Oct-03 6:13
Julian Bucknall [MSFT]2-Oct-03 6:13 
GeneralRe: Array question Pin
Joe Woodbury2-Oct-03 6:40
professionalJoe Woodbury2-Oct-03 6:40 
GeneralInspiration needed - end user form editing Pin
Member 961-Oct-03 6:59
Member 961-Oct-03 6:59 
GeneralRe: Inspiration needed - end user form editing Pin
Michael P Butler2-Oct-03 1:42
Michael P Butler2-Oct-03 1:42 
GeneralRe: Inspiration needed - end user form editing Pin
Member 962-Oct-03 5:05
Member 962-Oct-03 5:05 
Generalremoting event problem Pin
manusha1-Oct-03 5:37
manusha1-Oct-03 5:37 
Generalimporting VB 6.0 into C# project... Pin
Anonymous1-Oct-03 4:47
Anonymous1-Oct-03 4:47 
GeneralGDI+ font and scalable graphics Pin
Member 5506441-Oct-03 2:33
Member 5506441-Oct-03 2:33 
GeneralRe: GDI+ font and scalable graphics Pin
Mazdak1-Oct-03 3:46
Mazdak1-Oct-03 3:46 
GeneralRe: GDI+ font and scalable graphics Pin
Member 5506441-Oct-03 4:03
Member 5506441-Oct-03 4:03 
GeneralRe: GDI+ font and scalable graphics Pin
azusakt1-Oct-03 15:21
azusakt1-Oct-03 15:21 
Generalthreading issue... Pin
profoundwhispers1-Oct-03 0:53
profoundwhispers1-Oct-03 0:53 
GeneralRe: threading issue... Pin
Blake Coverett1-Oct-03 1:50
Blake Coverett1-Oct-03 1:50 
GeneralRe: threading issue... Pin
profoundwhispers1-Oct-03 1:56
profoundwhispers1-Oct-03 1:56 
GeneralRe: threading issue... Pin
Blake Coverett1-Oct-03 8:22
Blake Coverett1-Oct-03 8:22 
GeneralEncryption/Decryption problem Pin
3ddA30-Sep-03 23:50
3ddA30-Sep-03 23:50 
Hi!

I'm working on a P2P app, with security built in, it's going to be released as open source as soon as I have a working prototype...

I've gotten as far as exchanging messages, encrypting username password with RSA and exchanging a common symmetric key. But when I try to decrypt messages encrypted with the sym-key I get an error:
Binary stream does not contain a valid BinaryHeader, 95 possible causes...

I have the following code:
<br />
public byte[] SecureSerialize(byte[] key, byte[] iv)<br />
{<br />
	if (encrypted == Encryption.NotEncrypted) throw new Exception("Message type should be serialized with Serialize()");<br />
<br />
	byte[] streamBuffer = new byte[BufferSize];<br />
<br />
	streamBuffer[0] = (byte)msgType;<br />
	streamBuffer[1] = (byte)encrypted;<br />
<br />
	MemoryStream ms = new MemoryStream(streamBuffer, sizeOffset, BufferSize - sizeOffset);<br />
<br />
	RijndaelManaged rij = new RijndaelManaged();<br />
	CryptoStream cs = new CryptoStream(ms, rij.CreateEncryptor(key, iv), CryptoStreamMode.Write);<br />
<br />
	BinaryFormatter bf = new BinaryFormatter();<br />
	bf.Serialize(cs, this);<br />
<br />
	cs.Flush();<br />
	ms.Flush();<br />
<br />
	SHA1Managed hashAlg = new SHA1Managed();<br />
	byte[] hash = hashAlg.ComputeHash(streamBuffer, 0, (int)ms.Position + sizeOffset);<br />
	bf.Serialize(ms, hash);<br />
<br />
	byte[] returnBuffer = new byte[(int)ms.Position + sizeOffset];<br />
	Array.Copy(streamBuffer, 0, returnBuffer, 0, (int)ms.Position + sizeOffset);<br />
<br />
	return returnBuffer;<br />
}<br />
<br />
public static UdpMessage SecureDeserialize(byte[] msg, byte[] key, byte[] iv)<br />
{<br />
	if ((Encryption)msg[1] == Encryption.NotEncrypted) throw new Exception("Message type should be deserialized with Deserialize()");<br />
<br />
	byte[] decryptedBuffer = msg;<br />
<br />
	MemoryStream ms = new MemoryStream(decryptedBuffer, sizeOffset, decryptedBuffer.Length - sizeOffset);<br />
<br />
	RijndaelManaged rij = new RijndaelManaged();<br />
	CryptoStream cs = new CryptoStream(ms, rij.CreateDecryptor(key, iv), CryptoStreamMode.Read);<br />
<br />
	BinaryFormatter bf = new BinaryFormatter();<br />
	UdpMessage message = (UdpMessage)bf.Deserialize(cs);<br />
<br />
	cs.Flush();<br />
	ms.Flush();<br />
<br />
	message.msgType = (MessageType)msg[0];<br />
	message.encrypted = (Encryption)msg[1];<br />
<br />
	SHA1Managed hashAlg = new SHA1Managed();<br />
	byte[] calculatedHash = hashAlg.ComputeHash(decryptedBuffer, 0, (int)ms.Position + sizeOffset);<br />
	byte[] msgHash = (byte[])bf.Deserialize(ms);<br />
	for (int i = 0 ; i < calculatedHash.Length ; i++)<br />
		if (calculatedHash[i] != msgHash[i])<br />
			throw new CryptographicException("Hash missmatch");<br />
<br />
	return message;<br />
}<br />


The same code without the encryption stuff works fine.
Anybody has a clue what's wrong?

And while I'm at it... Is there any good way to protect against man in the middle attacks when exchanging keys? Is the application very vunerable with out that protection?

/Regards 3ddA

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.