problem with cryptography. who can help me?


S

Saper\(ek\)

I need to encrypt some data in my program, so I've created 2 functions to
encrypt and decrypt data. I've created a simple program to test it, and...

it crashes. It wors ok on XP, but on win98 it generates an exception
(padding is invalid and cannot be removed)
When I change coding of chars from Unicode, to utf8 or 7, it does not work
even under XP. what can I do, for it to work on XP and win98??

this is the program

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Security.Cryptography;

using System.IO;

using System.Text;

namespace Crypto

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.TextBox textBox2;

private System.Windows.Forms.TextBox textBox3;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;


private SymmetricAlgorithm sa;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

sa=RijndaelManaged.Create();

sa.Key=new byte[]{0x57, 0x91, 0xC6, 0x2C, 0x72, 0x8C, 0xC0, 0xA1, 0xED,
0x23,

0x60, 0x1C, 0x6B, 0x05, 0x62, 0xE4, 0xB2, 0x80, 0xF3, 0xF1,

0xA3, 0x55, 0xF1, 0x9A, 0x6A, 0x7D, 0x03, 0x21, 0xC1, 0x98, 0x29, 0xBD};

sa.IV=new byte[]{0xFD, 0x50, 0xD4, 0x67, 0x56, 0x78, 0x06, 0xB4, 0x70, 0x83,

0xD0, 0xCD, 0xFC, 0xA5, 0x05, 0x20};

sa.Mode=CipherMode.ECB;

sa.Padding=PaddingMode.PKCS7;

}

private string Cipher(string plainText)

{

byte[] cipherbytes;

//establish crypto stream

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(

ms,

sa.CreateEncryptor(),

CryptoStreamMode.Write);

//write plaintext bytes to crypto stream

byte[] plainbytes =

Encoding.Unicode.GetBytes(plainText);

cs.Write(plainbytes, 0, plainbytes.Length);

cs.Close();

cipherbytes = ms.ToArray();

ms.Close();

//display ciphertext byte array in hex format

return Encoding.Uniecode.GetString(cipherbytes);

}

private string DeCipher(string cipherText)

{

byte[] cipherbytes;

cipherbytes=Encoding.Uniecode.GetBytes(cipherText);

//establish crypto stream

MemoryStream ms = new MemoryStream(cipherbytes);

CryptoStream cs = new CryptoStream(

ms,

sa.CreateDecryptor(),

CryptoStreamMode.Read);

//read plaintext bytes from crypto stream

byte[] plainbytes =

new byte[cipherbytes.Length];

cs.Read(plainbytes, 0, cipherbytes.Length);

cs.Close();

ms.Close();

//display recovered plaintext

return Encoding.Uniecode.GetString(plainbytes);

}

/// <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.textBox1 = new System.Windows.Forms.TextBox();

this.textBox2 = new System.Windows.Forms.TextBox();

this.textBox3 = new System.Windows.Forms.TextBox();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(0, 16);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(744, 20);

this.textBox1.TabIndex = 0;

this.textBox1.Text = "";

this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);

//

// textBox2

//

this.textBox2.Location = new System.Drawing.Point(0, 56);

this.textBox2.Name = "textBox2";

this.textBox2.ReadOnly = true;

this.textBox2.Size = new System.Drawing.Size(744, 20);

this.textBox2.TabIndex = 1;

this.textBox2.Text = "";

//

// textBox3

//

this.textBox3.Location = new System.Drawing.Point(0, 96);

this.textBox3.Name = "textBox3";

this.textBox3.ReadOnly = true;

this.textBox3.Size = new System.Drawing.Size(744, 20);

this.textBox3.TabIndex = 2;

this.textBox3.Text = "";

//

// label1

//

this.label1.Location = new System.Drawing.Point(0, 0);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(136, 16);

this.label1.TabIndex = 3;

this.label1.Text = "Tu pisz";

//

// label2

//

this.label2.Location = new System.Drawing.Point(0, 40);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(100, 16);

this.label2.TabIndex = 4;

this.label2.Text = "Text zaszyfrowany";

//

// label3

//

this.label3.Location = new System.Drawing.Point(0, 80);

this.label3.Name = "label3";

this.label3.Size = new System.Drawing.Size(112, 16);

this.label3.TabIndex = 5;

this.label3.Text = "Tekst odszyfrowany";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(744, 126);

this.Controls.Add(this.label3);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.textBox3);

this.Controls.Add(this.textBox2);

this.Controls.Add(this.textBox1);

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 void textBox1_TextChanged(object sender, System.EventArgs e)

{

textBox2.Text=Cipher(textBox1.Text);

textBox3.Text=DeCipher(textBox2.Text);

//System.IO.StreamWriter sw=new StreamWriter("text.txt", true,
System.Text.Encoding.Unicode);

//sw.WriteLine(textBox2.Text);

//sw.WriteLine(textBox3.Text);

//sw.Close();

}

}

}


--


"Zeglarstwo jest koniecznoscia
zycie nia nie jest"

www.saper.infra.pl/

Saper(ek)
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Saper(ek) said:
I need to encrypt some data in my program, so I've created 2 functions to
encrypt and decrypt data. I've created a simple program to test it, and...

it crashes. It wors ok on XP, but on win98 it generates an exception
(padding is invalid and cannot be removed)
When I change coding of chars from Unicode, to utf8 or 7, it does not work
even under XP. what can I do, for it to work on XP and win98??

There are a few problems here:

1) You're using Encoding.Unicode inappropriately - the crypto stream
produces binary data. Just encoding that as unicode isn't a good idea -
you should use Base64 encoding or something similar which is far more
appropriate - or just return the byte array rather than encoding it in
a string at all.

2) You're not calling cryptostream.FlushFinalBlock, which may well be
problem.

As a short point in general for testing this kind of thing - you could
have produced your test program in a *much* shorter form if you'd not
bothered with a GUI and just written a console app - under half of the
code you posted actually has anything to do with encryption. See
http://www.pobox.com/~skeet/csharp/complete.html for a few more
guidelines here. Only MHO though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top