Click here to Skip to main content
15,888,286 members
Home / Discussions / C#
   

C#

 
QuestionCreateIPForwardEntry() returning ERROR_INVALID_PARAMETER... please help!! Pin
b_girl29-Sep-06 9:30
b_girl29-Sep-06 9:30 
QuestionHow to recursively search directories Pin
iamnew2C#29-Sep-06 9:24
iamnew2C#29-Sep-06 9:24 
AnswerRe: How to recursively search directories Pin
led mike29-Sep-06 9:30
led mike29-Sep-06 9:30 
AnswerRe: How to recursively search directories Pin
mejojo29-Sep-06 11:26
mejojo29-Sep-06 11:26 
GeneralRe: How to recursively search directories Pin
iamnew2C#2-Oct-06 10:52
iamnew2C#2-Oct-06 10:52 
QuestionSelf Updating Application Pin
wasife29-Sep-06 9:05
wasife29-Sep-06 9:05 
QuestionRemove an attribute from a property at runtime Pin
Ista29-Sep-06 8:36
Ista29-Sep-06 8:36 
QuestionHelp with DES encryption & decryption Pin
Sreerag Gopinath29-Sep-06 8:16
Sreerag Gopinath29-Sep-06 8:16 
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);
}

}

AnswerRe: Help with DES encryption & decryption Pin
geo_m29-Sep-06 21:54
geo_m29-Sep-06 21:54 
Questionquestion regarding BufferedStream Pin
Green Fuze29-Sep-06 8:10
Green Fuze29-Sep-06 8:10 
QuestionIs it possible to store an encrypted files other than .txt in sql server using AES encryption(C#). Pin
TechnoDev29-Sep-06 7:15
TechnoDev29-Sep-06 7:15 
QuestionP2P messaging and file sharing over HTTP Pin
asamay29-Sep-06 6:44
asamay29-Sep-06 6:44 
Questionget Path from running instance of word ? Pin
sundar15629-Sep-06 6:38
sundar15629-Sep-06 6:38 
AnswerRe: get Path from running instance of word ? Pin
umseker29-Sep-06 7:58
umseker29-Sep-06 7:58 
GeneralRe: get Path from running instance of word ? Pin
sundar15629-Sep-06 10:42
sundar15629-Sep-06 10:42 
QuestionDrawing Connectors C# 2.0 Pin
robert11029-Sep-06 6:07
robert11029-Sep-06 6:07 
AnswerRe: Drawing Connectors C# 2.0 Pin
umseker29-Sep-06 8:36
umseker29-Sep-06 8:36 
QuestionIs it possible to store an encrypted txt file in sql server using AES encryption. Pin
TechnoDev29-Sep-06 4:52
TechnoDev29-Sep-06 4:52 
AnswerRe: Is it possible to store an encrypted txt file in sql server using AES encryption. Pin
led mike29-Sep-06 6:20
led mike29-Sep-06 6:20 
Questionoffice development Pin
fmardani29-Sep-06 4:30
fmardani29-Sep-06 4:30 
AnswerRe: office development Pin
Not Active29-Sep-06 5:32
mentorNot Active29-Sep-06 5:32 
QuestionCommand Line Argument access from another app Pin
UTRocketFan29-Sep-06 4:17
UTRocketFan29-Sep-06 4:17 
QuestionSystem.Web.Mail and CDO.Message Error Pin
Saamir29-Sep-06 3:48
Saamir29-Sep-06 3:48 
QuestionExecuting an executable unmanaged exe file... Pin
erikash29-Sep-06 3:21
erikash29-Sep-06 3:21 
AnswerRe: Executing an executable unmanaged exe file... Pin
Andrew Rissing29-Sep-06 5:03
Andrew Rissing29-Sep-06 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.