|
Hi,
Could you spare some time to help me with a small problem?
I need to perform encryption and decryption using the DES algorithm.
This is the way I want the program to work :
My form has 4 textboxes -
1) tbPlaintext --- For the user to enter the plaintext
2) tbCiphertext --- For displaying he ciphertext
3) tbKey --- For displaying the key
4) tbIV --- For displaying the Initialization Vector
and 2 buttons -
1) Button1 --- When clicked performs encryption using key form tbKey and the IV from the string - "init vec"
2) Button2 --- When clicked performs decryption using the key in tbKey and the IV "init vec"
The user enters the plaintext -"Hello world" and the key - "qwertyui" and clicks the Button1. The ciphertext appears in the textbox tbCiphertext ("SGVsbG8gd29ybGQ=").
This part works fine.
Now when the user clicks the Button2 , after clearing the text in tbPlaintext, the plaintext "Hello world" should appear in tbPlaintext. But what I get is "U0dWc2JHOGdkMjl5YkdRPQ==". How do you explain that?
What should I do to get my original plaintext?
The MSDN article "How To: Create an Encryption Library in .NET 1.1" at the following URL helped me with the code-
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp
Is there something I'm missing?
Please help me as soon as possible so I can proceed with my project.
- Sreerag Gopinath P C
EMAIL : sreerag.gopinath@gmail.com
PHONE : +919447489699
*********************************************************
The relevant source code is as follows -
-----------------------------------------------
/* ENCRYPT USING DES ALGORITHM */
private void button1_Click(object sender, System.EventArgs e)
{
if (tbKey.Text == "")
MessageBox.Show("Enter some key");
else
{
if (tbKey.Text.Length == 8)
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
byte[] plaintext = Encoding.ASCII.GetBytes(tbplaintext.Text.ToString());
byte[] key = Encoding.ASCII.GetBytes(tbKey.Text.Trim());
byte[] IV = Encoding.ASCII.GetBytes("init vec");
des.Key = key;
des.IV = IV;
ICryptoTransform transform = des.CreateEncryptor(des.Key,des.IV);
MemoryStream memStreamEncryptedData = new MemoryStream();
memStreamEncryptedData.Write(plaintext,0,plaintext.Length);
CryptoStream encStream = new CryptoStream(memStreamEncryptedData,transform,CryptoStreamMode.Write);
byte[] ciphertext = memStreamEncryptedData.ToArray();
tbCiphertext.Text = Convert.ToBase64String(ciphertext);
string msg = String.Format("KEY : {0}\r\nIV : {1}",Convert.ToBase64String(key),Convert.ToBase64String(IV));
MessageBox.Show(msg);
// tbKey.Text = Convert.ToBase64String(des.Key); // The key seems to have changed
// tbIv.Text = Convert.ToBase64String(des.IV); // So does the Initialization Vector
// stored_key = des.Key; // Storing these for later retrieval
// stored_IV = des.IV; // does not produce the expected results either.
encStream.Close();
}
else
{
MessageBox.Show("Key length should have 8 characters");
}
}
}
/* Decrypt DES encrypted text */
private void button2_Click(object sender, System.EventArgs e)
{
try
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
byte[] ciphertext = Encoding.ASCII.GetBytes(tbCiphertext.Text);
byte[] key = Encoding.ASCII.GetBytes(tbKey.Text); // Prompts user to enter the key
// byte[] key = Encoding.ASCII.GetBytes("qwertyui"); // Even this does not seem to work--Why?
byte[] IV = Encoding.ASCII.GetBytes("init vec");
// byte[] IV = Encoding.ASCII.GetBytes(tbIv.Text); // Does not appear to work either
des.Key = key;
des.IV = IV;
// des.Key = stored_key; // Retrieving the Key and IV form already stored
// des.IV = stored_IV; // variables does not work either
ICryptoTransform transform = des.CreateDecryptor(des.Key,des.IV);
MemoryStream memDecryptStream = new MemoryStream();
memDecryptStream.Write(ciphertext,0,ciphertext.Length);
CryptoStream cs_decrypt = new CryptoStream(memDecryptStream,transform,CryptoStreamMode.Write);
byte[] plaintext = memDecryptStream.ToArray();
tbplaintext.Text = Convert.ToBase64String(plaintext);
cs_decrypt.Close();
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}
}
|
|
|
|
|
you're mixing with the base64 encoding. Your sequence is:
Encryption:
get bytes of plaintext
encrypt
display base64 encoded ciphertext
Decryption:
get bytes of ciphertext
decrypt
display base64 encoded result
but better and working solution ought to be
Decryption:
decode the ciphertext bytes from the base64 (FromBase64String)
decrypt
get string from bytes
|
|
|
|
|
Hi everybody
I'm trying to send a large file accross a network.
the problem is that I'm losing many chunks.
At the beginning I tried to split the file myself and send it, but I had a lot of lost data, so I read about BufferedStream class that can take care of the chuncks by itself.
so, for sending the file I wrote:
stream = client.GetStream();
<br />
BufferedStream buff = new BufferedStream(stream);<br />
buff.Write(fileBytes, 0, fileBytes.Length);
<br />
buff.Flush();<br />
stream.Close();
for getting the file I wrote:
socket = listener.AcceptSocket();<br />
NetworkStream stream = new NetworkStream(socket);<br />
<br />
byte[] filebyte = new byte[filesize];
<br />
BufferedStream buff = new BufferedStream(stream);<br />
int readByte = 1;<br />
<br />
while(readByte > 0)
readByte = buff.Read(filebyte, 0, filebyte.Length);<br />
<br />
File.WriteAllBytes(filename, filebyte);<br />
<br />
buff.Flush();<br />
stream.Close();
The problem is that I still lose data, I really don't know what else to do.
ANY HELP will be highly appriciated!!!
Thanks!
|
|
|
|
|
IS this at all possible? thanks
|
|
|
|
|
im working on an application p2p filesharing and messaging.
im trying to do it over HTTP:80. packets can be dropped routers and firewalls, so i want to use port 80.
is it possible to communicate between clients 2 clients and clients to server over HTTP without installing IIS (except servers)?
thanks!!
asamay
|
|
|
|
|
Hi ,
I have a running
instance of word got from Marshal.GetActiveObject("Word.Application");
in my c# application. So how do i get the absolute path of the word document that is open?
Luck is Opportunity with hardwork
|
|
|
|
|
...
Word.Application w= new Word.Application();
w = (Word.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
MessageBox.Show(w.ActiveDocument.Path);
...
UMS
|
|
|
|
|
Thanks umseker..
It works..
Luck is Opportunity with hardwork
|
|
|
|
|
Hello Everyone:
I am working on an app (windows) in which I need to provide a functionality to create object connectors (using mouse events). Kinda like when you try to make a DTS package in SQL Server you connect two ojbect and it draws a nice graphics to represent the connected objects. Or Table Designer when you try to relate two fields in seperate tables.
Any help will be appreciated. Thanks.
Best Regards,
Robert
|
|
|
|
|
|
Also can you decrypt the content from the database and recreate the file?
thanks in advance.
|
|
|
|
|
|
Hi,
Can you recommend a book on C# 2005 (windows or web) with excel/word please?
I would like to learn programming office applications (excel or word) with c# 2005 (asp.net 2.0 or windows).
Thanks
|
|
|
|
|
|
Does anyone know if there is a way to access command line arguments from one running application in another application. I want to have a windows service that looks for other running apps and runs them when necessary. Sometimes there may be more than one instance running with different arguments, so I need a way to see what arguments the running instance is using. I tried looking at processes that I get when calling GetProcessesByName and then looking at StartInfo Arguements, but it appears to be empty even when I have passed in parameters. I know my parameters have been accepted because I an display them in a message box. Any help on this would be greatly appreciated.
Sean
|
|
|
|
|
Hi guys,
Please I need some help. I am totally lost on this and have used all my thinking power to deal with this issue and no luck. I have an application that uses System.Web.Mail to transmit emails, in every 5-6 emails that go out one stalls giving the above error with inner exception as follows:
The transport failed to connect to the server and sometimes it says The transport lost it's connection to the server.
We have a mail marshal server that also acts as a dummy smtp server to filter the emails and that is where I am directing these emails to. This dummy server acts as a filter to the emails and diverts them to the main email server with a port not 25 hence I can't divert them through that main server. Do you think this could be a problem?
Dummy server is the SMTP server I am using and has a port 25. Our SMTP server configurations are through Exchange.
Please any suggestions will be very helpful.
sasa
|
|
|
|
|
Hi guys, i need to execute such a file and supply it arguments, i've looked around and found the "System.Diagnostics.Process.Start()" method, it works great, but i want controll over the executed application... or at least run it in a minimized window state...
any suggestions?
(The executable is a program written in c)
Thanks in advance,
Erik.
|
|
|
|
|
|
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
proc.StartInfo.FileName = "thefile.exe";
proc.Start();
|
|
|
|
|
Thanks guys!
Ok next question... the executable exe file gets input from the user (using scanf)
is there a way to supply it arguments without changing the source code of the executable?
(by changing the main to receive parameters and modifing the source code accordingly)
I guess it has something to to with adding values to the input buffer of that application...?
Thanks in advance,
Erik.
|
|
|
|
|
You can supply arguments to the executable by changing the ProcessStartInfo.Arguments property. Is that what you mean?
|
|
|
|
|
How about:
myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
|
|
|
|
|
Hi all dear friends,
I have a win. service, and I try to read the current user name,
the following methods DONT work, please help.
1. Way : --> System.Security.Principal.WindowsIdentity.GetCurrent().Name;
2. Way : --> Environment.UserName;
these are dont work, becuase, win service run as a account "system" default.
any other ideas ?
Cheers...
|
|
|
|
|
Alper Sümer Güç wrote: these are dont work, becuase, win service run as a account "system" default
Actually, that is showing the correct result. The current user IS "System". Each process running runs in its given security context and as far as the application is concerned that is the current user.
|
|
|
|
|
hi!
ok here's what i want to do exactly:
i want to keep showing the rows with errors and in the same time, enable the user to add and edit other rows.
that's why, i'm asking if there's a way to put a row in an unbound mode ?
thks in advance.
C# is the future, but who can see the future :p
|
|
|
|