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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r 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 SymmetricAlgori thm sa;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
sa=RijndaelMana ged.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=CipherM ode.ECB;
sa.Padding=Padd ingMode.PKCS7;
}
private string Cipher(string plainText)
{
byte[] cipherbytes;
//establish crypto stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(
ms,
sa.CreateEncryp tor(),
CryptoStreamMod e.Write);
//write plaintext bytes to crypto stream
byte[] plainbytes =
Encoding.Unicod e.GetBytes(plai nText);
cs.Write(plainb ytes, 0, plainbytes.Leng th);
cs.Close();
cipherbytes = ms.ToArray();
ms.Close();
//display ciphertext byte array in hex format
return Encoding.Unieco de.GetString(ci pherbytes);
}
private string DeCipher(string cipherText)
{
byte[] cipherbytes;
cipherbytes=Enc oding.Uniecode. GetBytes(cipher Text);
//establish crypto stream
MemoryStream ms = new MemoryStream(ci pherbytes);
CryptoStream cs = new CryptoStream(
ms,
sa.CreateDecryp tor(),
CryptoStreamMod e.Read);
//read plaintext bytes from crypto stream
byte[] plainbytes =
new byte[cipherbytes.Len gth];
cs.Read(plainby tes, 0, cipherbytes.Len gth);
cs.Close();
ms.Close();
//display recovered plaintext
return Encoding.Unieco de.GetString(pl ainbytes);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
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.SuspendLay out();
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(0, 16);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(744, 20);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
this.textBox1.T extChanged += new
System.EventHan dler(this.textB ox1_TextChanged );
//
// textBox2
//
this.textBox2.L ocation = new System.Drawing. Point(0, 56);
this.textBox2.N ame = "textBox2";
this.textBox2.R eadOnly = true;
this.textBox2.S ize = new System.Drawing. Size(744, 20);
this.textBox2.T abIndex = 1;
this.textBox2.T ext = "";
//
// textBox3
//
this.textBox3.L ocation = new System.Drawing. Point(0, 96);
this.textBox3.N ame = "textBox3";
this.textBox3.R eadOnly = true;
this.textBox3.S ize = new System.Drawing. Size(744, 20);
this.textBox3.T abIndex = 2;
this.textBox3.T ext = "";
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(0, 0);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(136, 16);
this.label1.Tab Index = 3;
this.label1.Tex t = "Tu pisz";
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(0, 40);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(100, 16);
this.label2.Tab Index = 4;
this.label2.Tex t = "Text zaszyfrowany";
//
// label3
//
this.label3.Loc ation = new System.Drawing. Point(0, 80);
this.label3.Nam e = "label3";
this.label3.Siz e = new System.Drawing. Size(112, 16);
this.label3.Tab Index = 5;
this.label3.Tex t = "Tekst odszyfrowany";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(744, 126);
this.Controls.A dd(this.label3) ;
this.Controls.A dd(this.label2) ;
this.Controls.A dd(this.label1) ;
this.Controls.A dd(this.textBox 3);
this.Controls.A dd(this.textBox 2);
this.Controls.A dd(this.textBox 1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void textBox1_TextCh anged(object sender, System.EventArg s e)
{
textBox2.Text=C ipher(textBox1. Text);
textBox3.Text=D eCipher(textBox 2.Text);
//System.IO.Strea mWriter sw=new StreamWriter("t ext.txt", true,
System.Text.Enc oding.Unicode);
//sw.WriteLine(te xtBox2.Text);
//sw.WriteLine(te xtBox3.Text);
//sw.Close();
}
}
}
--
"Zeglarstwo jest koniecznoscia
zycie nia nie jest"
www.saper.infra.pl/
Saper(ek)