Click here to Skip to main content
15,890,506 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: Encryption/Decryption problem Pin
Julian Bucknall [MSFT]1-Oct-03 5:35
Julian Bucknall [MSFT]1-Oct-03 5:35 
GeneralRe: Encryption/Decryption problem Pin
3ddA1-Oct-03 6:31
3ddA1-Oct-03 6:31 
GeneralRe: Encryption/Decryption problem, prettier code now Pin
3ddA1-Oct-03 7:28
3ddA1-Oct-03 7:28 
I've written code a little easier to understand, and hope it will give some response...

Key and iv are double checked and correct data is received as it was sent.

<br />
public byte[] SecureSerialize(byte[] key, byte[] iv)<br />
{<br />
	byte[] streamBuffer = new byte[BufferSize];<br />
<br />
	MemoryStream ms = new MemoryStream(streamBuffer);<br />
	ms.WriteByte((byte)encrypted);<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 />
	SHA1Managed hashAlg = new SHA1Managed();<br />
	byte[] hash = hashAlg.ComputeHash(streamBuffer, 0, (int)ms.Position);<br />
	bf.Serialize(ms, hash);<br />
<br />
	byte[] returnBuffer = new byte[(int)ms.Position];<br />
	Array.Copy(streamBuffer, 0, returnBuffer, 0, (int)ms.Position);<br />
<br />
	return returnBuffer;<br />
}<br />
<br />
public static UdpMessage SecureDeserialize(byte[] msg, byte[] key, byte[] iv)<br />
{<br />
	MemoryStream ms = new MemoryStream(msg);<br />
	byte enc = (byte)ms.ReadByte();<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); //  <<<----This fails<br />
<br />
	message.encrypted = (Encryption)enc;<br />
<br />
	SHA1Managed hashAlg = new SHA1Managed();<br />
	byte[] calculatedHash = hashAlg.ComputeHash(msg, 0, (int)ms.Position);<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 />


Regards 3ddA
GeneralTCP Remote Endpoints Pin
Tatham30-Sep-03 19:50
Tatham30-Sep-03 19:50 
GeneralRe: TCP Remote Endpoints Pin
Blake Coverett1-Oct-03 1:36
Blake Coverett1-Oct-03 1:36 
GeneralRe: TCP Remote Endpoints Pin
Tatham1-Oct-03 4:03
Tatham1-Oct-03 4:03 
GeneralRe: TCP Remote Endpoints Pin
Blake Coverett1-Oct-03 8:25
Blake Coverett1-Oct-03 8:25 
GeneralRe: TCP Remote Endpoints Pin
Tatham2-Oct-03 3:09
Tatham2-Oct-03 3:09 
GeneralRe: TCP Remote Endpoints Pin
Blake Coverett2-Oct-03 8:50
Blake Coverett2-Oct-03 8:50 
GeneralGeting/seting properties on files. Pin
Mattias Olgerfelt30-Sep-03 14:41
Mattias Olgerfelt30-Sep-03 14:41 
GeneralRe: Geting/seting properties on files. Pin
J. Dunlap30-Sep-03 15:05
J. Dunlap30-Sep-03 15:05 
GeneralRe: Geting/seting properties on files. Pin
Mattias Olgerfelt2-Oct-03 14:46
Mattias Olgerfelt2-Oct-03 14:46 
GeneralTransparent Background for PocketPC Pin
Rowdy12330-Sep-03 14:07
Rowdy12330-Sep-03 14:07 
GeneralRe: Transparent Background for PocketPC Pin
J. Dunlap30-Sep-03 14:12
J. Dunlap30-Sep-03 14:12 
GeneralRe: Transparent Background for PocketPC Pin
Rowdy12330-Sep-03 14:22
Rowdy12330-Sep-03 14:22 
GeneralRe: Transparent Background for PocketPC Pin
J. Dunlap30-Sep-03 14:28
J. Dunlap30-Sep-03 14:28 
QuestionFile/Folder Name Problems - How to get around? Pin
Khang Nguyen30-Sep-03 12:44
Khang Nguyen30-Sep-03 12:44 
AnswerRe: File/Folder Name Problems - How to get around? Pin
Meysam Mahfouzi30-Sep-03 18:03
Meysam Mahfouzi30-Sep-03 18:03 
GeneralProblem with winsock programming with character encoding. Pin
Andrlage30-Sep-03 11:14
Andrlage30-Sep-03 11:14 
GeneralSpecialized Collection/List Pin
J. Dunlap30-Sep-03 10:44
J. Dunlap30-Sep-03 10:44 

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.