how can i encrypt/decrypt binary data?

  • Thread starter Thread starter assaf
  • Start date Start date
A

assaf

hi all

the folowing code demonstrates
a simple app
that tries to encrypt a serialized integer.

it is devided into 4 methods:
1. serializing the integer.
2. encrypting the buffer.
3. decrypting the buffer.
4. desirializing the buffer.

it seems like the encryption succeeds,
but in method 3,
when i try to decrypt
(by reading from the CryptoStream to the buffer)
the decryption fails
and i get a buffer of zeroes.

how can i encrypt/decrypt binary data?


assaf



dot net C# code:


using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
//this._IFormatter.Serialize(ms, i);
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write);

cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
this._EncrySerializedBuffer = ms.ToArray();
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
//MemoryStream ms = new MemoryStream(this._EncrySerializedBuffer);
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write(b.ToString());
}

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
//BinaryReader br = new BinaryReader(cryptostreamDecr);
this._DecrySerializedBuffer = new byte[1000];
//br.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
// ms.Write(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
ms.Position = 0;
//int j = (int)this._IFormatter.Deserialize(ms);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}
 
Did you reset the CryptoStream back to the start of the stream after step 2?

myCryptoStream.Seek(0, SeekOrigin.Begin);

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

hi all

the folowing code demonstrates
a simple app
that tries to encrypt a serialized integer.

it is devided into 4 methods:
1. serializing the integer.
2. encrypting the buffer.
3. decrypting the buffer.
4. desirializing the buffer.

it seems like the encryption succeeds,
but in method 3,
when i try to decrypt
(by reading from the CryptoStream to the buffer)
the decryption fails
and i get a buffer of zeroes.

how can i encrypt/decrypt binary data?


assaf



dot net C# code:


using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
//this._IFormatter.Serialize(ms, i);
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write);

cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
this._EncrySerializedBuffer = ms.ToArray();
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
//MemoryStream ms = new MemoryStream(this._EncrySerializedBuffer);
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write(b.ToString());
}

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
//BinaryReader br = new BinaryReader(cryptostreamDecr);
this._DecrySerializedBuffer = new byte[1000];
//br.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
// ms.Write(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
ms.Position = 0;
//int j = (int)this._IFormatter.Deserialize(ms);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
assaf said:
the folowing code demonstrates
a simple app
that tries to encrypt a serialized integer.

it is devided into 4 methods:
1. serializing the integer.
2. encrypting the buffer.
3. decrypting the buffer.
4. desirializing the buffer.

it seems like the encryption succeeds,
but in method 3,
when i try to decrypt
(by reading from the CryptoStream to the buffer)
the decryption fails
and i get a buffer of zeroes.

how can i encrypt/decrypt binary data?

Two problems:

1) You never close the writing cryptostream. You should always close
all streams, IMO, but cryptostreams need to be closed so they can write
the final block. Using statements make this much easier.

2) You're reading into a fixed size buffer, and then treating the
*whole* buffer as if it formed the serialized data. Personally I find
it easier to use another "writing" CryptoStream, but a decrypting one,
writing into another MemoryStream. Write the encrypted data through
that, and then use ToArray to get the decrypted data at the end.

3) You're not calling FlushFinalBlock() on your cryptostreams. While I
didn't *think* it was necessary - I thought that Disposing of the
stream flushed it - it appears it is in at least some situations.
 
hi richard

we tried your recommendation.
but it crashed with 'cannot seek'.
so maybe u can tell us exactly what u mean?


assaf


new improved code:



using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();

Console.WriteLine("_SerializedBuffer: ");
foreach(byte b in this._SerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
using(CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write))
{
cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
cryptostream.FlushFinalBlock();
cryptostream.Flush();
cryptostream.Close();
}

this._EncrySerializedBuffer = ms.ToArray();

Console.WriteLine("\n_EncrySerializedBuffer ({0:3###}) ",
this._EncrySerializedBuffer.Length);
foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write("{0} ", b);
}
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
this._DecrySerializedBuffer = new
byte[this._EncrySerializedBuffer.Length];
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

Console.WriteLine("\n_DecrySerializedBuffer: ");
foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}



end of new improved code!!!






Richard Blewett said:
Did you reset the CryptoStream back to the start of the stream after step 2?

myCryptoStream.Seek(0, SeekOrigin.Begin);

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
hi all

the folowing code demonstrates
a simple app
that tries to encrypt a serialized integer.

it is devided into 4 methods:
1. serializing the integer.
2. encrypting the buffer.
3. decrypting the buffer.
4. desirializing the buffer.

it seems like the encryption succeeds,
but in method 3,
when i try to decrypt
(by reading from the CryptoStream to the buffer)
the decryption fails
and i get a buffer of zeroes.

how can i encrypt/decrypt binary data?


assaf



dot net C# code:


using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
//this._IFormatter.Serialize(ms, i);
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write);

cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
this._EncrySerializedBuffer = ms.ToArray();
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
//MemoryStream ms = new MemoryStream(this._EncrySerializedBuffer);
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write(b.ToString());
}

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
//BinaryReader br = new BinaryReader(cryptostreamDecr);
this._DecrySerializedBuffer = new byte[1000];
//br.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
// ms.Write(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
ms.Position = 0;
//int j = (int)this._IFormatter.Deserialize(ms);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Ah, my mistake, I thought the CryptoStream supported Seek operations - obviously not.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<#[email protected]>

hi richard

we tried your recommendation.
but it crashed with 'cannot seek'.
so maybe u can tell us exactly what u mean?


assaf


new improved code:



using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();

Console.WriteLine("_SerializedBuffer: ");
foreach(byte b in this._SerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
using(CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write))
{
cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
cryptostream.FlushFinalBlock();
cryptostream.Flush();
cryptostream.Close();
}

this._EncrySerializedBuffer = ms.ToArray();

Console.WriteLine("\n_EncrySerializedBuffer ({0:3###}) ",
this._EncrySerializedBuffer.Length);
foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write("{0} ", b);
}
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
this._DecrySerializedBuffer = new
byte[this._EncrySerializedBuffer.Length];
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

Console.WriteLine("\n_DecrySerializedBuffer: ");
foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}



end of new improved code!!!






Richard Blewett said:
Did you reset the CryptoStream back to the start of the stream after step 2?

myCryptoStream.Seek(0, SeekOrigin.Begin);

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
hi all

the folowing code demonstrates
a simple app
that tries to encrypt a serialized integer.

it is devided into 4 methods:
1. serializing the integer.
2. encrypting the buffer.
3. decrypting the buffer.
4. desirializing the buffer.

it seems like the encryption succeeds,
but in method 3,
when i try to decrypt
(by reading from the CryptoStream to the buffer)
the decryption fails
and i get a buffer of zeroes.

how can i encrypt/decrypt binary data?


assaf



dot net C# code:


using System;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace Encrypetion
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonSerialization;
private System.Windows.Forms.Button buttonEncryption;
private System.Windows.Forms.Button buttonDecryption;
private System.Windows.Forms.Button buttonDeserialize;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonSerialization = new System.Windows.Forms.Button();
this.buttonEncryption = new System.Windows.Forms.Button();
this.buttonDecryption = new System.Windows.Forms.Button();
this.buttonDeserialize = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonSerialization
//
this.buttonSerialization.Location = new System.Drawing.Point(16, 8);
this.buttonSerialization.Name = "buttonSerialization";
this.buttonSerialization.TabIndex = 0;
this.buttonSerialization.Text = "Serialization";
this.buttonSerialization.Click += new
System.EventHandler(this.buttonSerialization_Click);
//
// buttonEncryption
//
this.buttonEncryption.Location = new System.Drawing.Point(16, 40);
this.buttonEncryption.Name = "buttonEncryption";
this.buttonEncryption.TabIndex = 1;
this.buttonEncryption.Text = "Encryption";
this.buttonEncryption.Click += new
System.EventHandler(this.buttonEncryption_Click);
//
// buttonDecryption
//
this.buttonDecryption.Location = new System.Drawing.Point(16, 72);
this.buttonDecryption.Name = "buttonDecryption";
this.buttonDecryption.TabIndex = 2;
this.buttonDecryption.Text = "Decryption";
this.buttonDecryption.Click += new
System.EventHandler(this.buttonDecryption_Click);
//
// buttonDeserialize
//
this.buttonDeserialize.Location = new System.Drawing.Point(16, 104);
this.buttonDeserialize.Name = "buttonDeserialize";
this.buttonDeserialize.TabIndex = 3;
this.buttonDeserialize.Text = "Deserialize";
this.buttonDeserialize.Click += new
System.EventHandler(this.buttonDeserialize_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(104, 157);
this.Controls.Add(this.buttonDeserialize);
this.Controls.Add(this.buttonDecryption);
this.Controls.Add(this.buttonEncryption);
this.Controls.Add(this.buttonSerialization);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private byte[] _SerializedBuffer;
private byte[] _EncrySerializedBuffer;
private byte[] _DecrySerializedBuffer;
private XmlSerializer _XmlSerializer = new
XmlSerializer(typeof(int));
private IFormatter _IFormatter = new BinaryFormatter();
private DESCryptoServiceProvider _DESCryptoServiceProvider = new
DESCryptoServiceProvider();

private void buttonSerialization_Click(object sender, System.EventArgs e)
{
int i = 8;
// Serialization

MemoryStream ms = new MemoryStream();
//this._IFormatter.Serialize(ms, i);
this._XmlSerializer.Serialize(ms, i);
this._SerializedBuffer = ms.ToArray();
}

private void buttonEncryption_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream();
ICryptoTransform desencrypt =
this._DESCryptoServiceProvider.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(ms, desencrypt,
CryptoStreamMode.Write);

cryptostream.Write(this._SerializedBuffer, 0,
this._SerializedBuffer.Length);
this._EncrySerializedBuffer = ms.ToArray();
}

private void buttonDecryption_Click(object sender, System.EventArgs e)
{
//MemoryStream ms = new MemoryStream(this._EncrySerializedBuffer);
MemoryStream ms = new MemoryStream();
ms.Write(this._EncrySerializedBuffer, 0,
this._EncrySerializedBuffer.Length);

foreach(byte b in this._EncrySerializedBuffer)
{
Console.Write(b.ToString());
}

ICryptoTransform desdecrypt =
this._DESCryptoServiceProvider.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt,
CryptoStreamMode.Read);
//BinaryReader br = new BinaryReader(cryptostreamDecr);
this._DecrySerializedBuffer = new byte[1000];
//br.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
cryptostreamDecr.Read(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);

foreach(byte b in this._DecrySerializedBuffer)
{
Console.Write(b.ToString());
}
}

private void buttonDeserialize_Click(object sender, System.EventArgs e)
{
MemoryStream ms = new MemoryStream(this._DecrySerializedBuffer);
// ms.Write(this._DecrySerializedBuffer, 0,
this._DecrySerializedBuffer.Length);
ms.Position = 0;
//int j = (int)this._IFormatter.Deserialize(ms);
foreach(byte b in ms.ToArray())
{
Console.Write(b.ToString());
}
int j = (int)this._XmlSerializer.Deserialize(ms);
}
}
}



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
assaf said:
hi jon

we followed recommendation 1 and 3
but 2 was really confusing.

also, maybe u can give us sample code?
also, maybe u can run our sample code
and see for yourself that it does not work!

I did, and modified it in the three ways I mentioned, and it then
worked.

I would still advocate using "using" statements for all your streams,
by the way - then you just need to call FlushFinalBlock (for reasons
that are still unclear to me, I admit) manually just before the end of
the using statement.

Point 2 was to change your decryption code to something like:

using (MemoryStream ms = new MemoryStream())
{
ICryptoTransform desdecrypt = [...];

using (CryptoStream cs = new CryptoStream (ms, desdecrypt,
CryptoStreamMode.Write))
{
cs.Write(_EncrySerializedBuffer, 0,
_EncrySerializedBuffer.Length);
cs.FlushFinalBlock();
}

_DecrySerializedBuffer = ms.ToArray();
}
 
hi jon

thank u so much.
finally it works.


assaf



Jon Skeet said:
assaf said:
hi jon

we followed recommendation 1 and 3
but 2 was really confusing.

also, maybe u can give us sample code?
also, maybe u can run our sample code
and see for yourself that it does not work!

I did, and modified it in the three ways I mentioned, and it then
worked.

I would still advocate using "using" statements for all your streams,
by the way - then you just need to call FlushFinalBlock (for reasons
that are still unclear to me, I admit) manually just before the end of
the using statement.

Point 2 was to change your decryption code to something like:

using (MemoryStream ms = new MemoryStream())
{
ICryptoTransform desdecrypt = [...];

using (CryptoStream cs = new CryptoStream (ms, desdecrypt,
CryptoStreamMode.Write))
{
cs.Write(_EncrySerializedBuffer, 0,
_EncrySerializedBuffer.Length);
cs.FlushFinalBlock();
}

_DecrySerializedBuffer = ms.ToArray();
}
 
Jon Skeet said:
I would still advocate using "using" statements for all your streams,
by the way - then you just need to call FlushFinalBlock (for reasons
that are still unclear to me, I admit) manually just before the end of
the using statement.

<snip>

The reason is now clear to me - there's a bug in CryptoStream. I
reported it to MS, and they've now fixed it for Whidbey, apparently.
 
tnx again sir


assaf


Jon Skeet said:
<snip>

The reason is now clear to me - there's a bug in CryptoStream. I
reported it to MS, and they've now fixed it for Whidbey, apparently.
 
Back
Top