473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem with cryptography. who can help me?

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)
Nov 15 '05 #1
1 1686
Saper(ek) <sa*****@tlen.pl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) {...
15
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: ...
0
by: Benny Ng | last post by:
Hi,All, When i deploy Enterprise library with my application ,i used XCOPY to deploy it into my test server. But when application runs, shown some error related registry. (But actually I haven't...
7
by: awyl | last post by:
I tried to use validation in asp.net 2.0 with the following code. <%@ Page Language="c#" %> <html> <body> <form runat="server"> <asp:TextBox ID="abc" runat="server"> </asp:TextBox>...
1
by: muthu | last post by:
Hi, I have two web applications running on my machine.The application is developed using asp.net 1.1 and vb.net.When i try to run both the applications in the same browsers, i get the following...
1
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
Hi, everybody here. I am implementing data encryption/decryption, and try to use System.Security.Cryptography.TripleDESCryptoServiceProvider. But I can not find it in MS Visual Studio when I...
0
by: Amelyan | last post by:
Why does this happen? How to fix it? Once in a while I get error in ~/ScriptResource.axd?d=... System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
0
by: tutorialwebs | last post by:
Applied cryptography text books http://www.365x24live.com/Applied%20Cryptography/ewtoc.html http://www.365x24live.com/Applied%20Cryptography/ewtoc.html...
0
by: Rodrigo m. Ferreira | last post by:
Can you help me to solve the following problem? on my loggin page I have the code: protected void LoginButton_Click(object sender, EventArgs e) { if(Membership.ValidateUser(TXTUsuario.Text,...
1
by: Napcrisis | last post by:
Hi guys i need to know if its possible to use visual c++ and make use of .net cryptography to encrypt files using the cryptography algorithms .net has to offer. cuz i have been searching around and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.