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);
}
}
}
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);
}
}
}