By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

How to get a 64bit(8 bytes) encrypt result using DES class in the VS2005?

P: n/a
Hi all,
I want to get a 64bit(8 bytes) Encrypt result use DES class in the
VS2005.
Though I encrypt data is 64bit(8 bytes), but DES return encrypt result
that always is 128bit(16 bytes),
I don't know why?

How to get a 64bit(8 bytes) encrypt result using DES class in the
VS2005?

thanx in advance,

Finema from China.
--------------------------------------------
My program environment:
Windows server 2003.
VS2005.

C# Code list:
The "EncryptTextToMemory" funciton return data always is 128bit(16 bytes).

static void Main()
{
try
{
// Create a new DESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
DESCryptoServiceProvider DESalg = new
DESCryptoServiceProvider();

// Create a string to encrypt.
//string sData = "Here is some data to encrypt.";
string sData = "abcdefgh";

// Encrypt the string to an in-memory buffer.
byte[] Data = EncryptTextToMemory(sData, DESalg.Key, DESalg.IV);

// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(Data, DESalg.Key,
DESalg.IV);

// Display the decrypted string to the console.
Console.WriteLine(Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadKey();

}

public static byte[] EncryptTextToMemory(string Data, byte[] Key,
byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
new DESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Convert the passed string to a byte array.
byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();

// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();

// Close the streams.
cStream.Close();
mStream.Close();

// Return the encrypted buffer.
return ret;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}

}

public static string DecryptTextFromMemory(byte[] Data, byte[] Key,
byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];

// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}
}

Sep 28 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hello,

You need to set the DESCryptoServiceProvider Padding to PaddingMode.None
in your EncryptTextToMemory and DecryptTextFromMemory methods.

Kelly

fineman wrote:
Hi all,
I want to get a 64bit(8 bytes) Encrypt result use DES class in the
VS2005.
Though I encrypt data is 64bit(8 bytes), but DES return encrypt result
that always is 128bit(16 bytes),
I don't know why?

How to get a 64bit(8 bytes) encrypt result using DES class in the
VS2005?

thanx in advance,

Finema from China.
--------------------------------------------
My program environment:
Windows server 2003.
VS2005.

C# Code list:
The "EncryptTextToMemory" funciton return data always is 128bit(16 bytes).

static void Main()
{
try
{
// Create a new DESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
DESCryptoServiceProvider DESalg = new
DESCryptoServiceProvider();

// Create a string to encrypt.
//string sData = "Here is some data to encrypt.";
string sData = "abcdefgh";

// Encrypt the string to an in-memory buffer.
byte[] Data = EncryptTextToMemory(sData, DESalg.Key, DESalg.IV);

// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(Data, DESalg.Key,
DESalg.IV);

// Display the decrypted string to the console.
Console.WriteLine(Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadKey();

}

public static byte[] EncryptTextToMemory(string Data, byte[] Key,
byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
new DESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Convert the passed string to a byte array.
byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();

// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();

// Close the streams.
cStream.Close();
mStream.Close();

// Return the encrypted buffer.
return ret;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}

}

public static string DecryptTextFromMemory(byte[] Data, byte[] Key,
byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];

// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}
}
Sep 29 '06 #2

P: n/a
Thanks for your instruction and help.

The problem had been solved.

"Kelly Ethridge" <ke***@kellyethridge.com????
news:45**************@kellyethridge.com...
Hello,

You need to set the DESCryptoServiceProvider Padding to PaddingMode.None
in your EncryptTextToMemory and DecryptTextFromMemory methods.

Kelly

fineman wrote:
>Hi all,
I want to get a 64bit(8 bytes) Encrypt result use DES class in the
VS2005.
Though I encrypt data is 64bit(8 bytes), but DES return encrypt
result that always is 128bit(16 bytes),
I don't know why?

How to get a 64bit(8 bytes) encrypt result using DES class in the
VS2005?

thanx in advance,

Finema from China.
--------------------------------------------
My program environment:
Windows server 2003.
VS2005.

C# Code list:
The "EncryptTextToMemory" funciton return data always is 128bit(16
bytes).

static void Main()
{
try
{
// Create a new DESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
DESCryptoServiceProvider DESalg = new
DESCryptoServiceProvider();

// Create a string to encrypt.
//string sData = "Here is some data to encrypt.";
string sData = "abcdefgh";

// Encrypt the string to an in-memory buffer.
byte[] Data = EncryptTextToMemory(sData, DESalg.Key,
DESalg.IV);

// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(Data, DESalg.Key,
DESalg.IV);

// Display the decrypted string to the console.
Console.WriteLine(Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.ReadKey();

}

public static byte[] EncryptTextToMemory(string Data, byte[] Key,
byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
new DESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Convert the passed string to a byte array.
byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();

// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();

// Close the streams.
cStream.Close();
mStream.Close();

// Return the encrypted buffer.
return ret;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}

}

public static string DecryptTextFromMemory(byte[] Data, byte[] Key,
byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new DESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];

// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}",
e.Message);
return null;
}
}

Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.