473,804 Members | 3,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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)
Nov 15 '05 #1
1 1710
Saper(ek) <sa*****@tlen.p l> 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.Unicod e 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.Fl ushFinalBlock, 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.co m>
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
5494
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) { Byte bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); //DES instance System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
15
2442
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: "pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan= BU:shp_Term=2"
0
2648
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 any method or component to wrote registry) After I used "InstallUtil" to registry Enterprise Library (those DLLs) to the deployment server, the application runs smoothly. Now I wonder why i need to registry those Enterprise Library DLLs into
7
11022
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> <asp:RequiredFieldValidator ControlToValidate="abc" runat="server"></asp:RequiredFieldValidator>
1
2773
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 error. (System.Security.Cryptography.CryptographicException: Bad Data. at System.Security.Cryptography.CryptoAPITransform._DecryptData(IntPtr hKey, Byte rgb, Int32 ib, Int32 cb, Boolean fDone) at...
1
2267
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 wrote code like (C#) using System.Security.Cryptography.TripleDESCryptoServiceProvider; Under System.Security.Cryptography, only have X509Certificates.
0
3385
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 invocation. ---> System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed. at
0
1319
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 http://www.365x24live.com/Applied%20Cryptography/ewtoc.html Applied cryptography text books Applied cryptography text books http://www.365x24live.com http://www.365x24live.com http://www.365x24live.com http://www.365x24live.com
0
2892
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, TXTSenha.Text)) {
1
2119
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 only seem to managed to find c# cryptography material. Btw what cryptography algorithms does .net have? Thx in advance for any help regards Tom
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.