473,395 Members | 1,452 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,395 software developers and data experts.

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

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
2 3158
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.64bit This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.64bit as an unmoderated...
8
by: Nathan Waddington | last post by:
Hello Everyone, My program receives a string representation of a 64bit hex number that needs to be displayed as the decimal number it represents. The compiler that is being used is Dynamic C...
8
by: Jarod | last post by:
Hey Is VS2005 optimized for Intel or AMD processor ? What gives best speed when I develop webprojects ? What really means in the term of speed 2 cores or frequency I assume 64bits. Jarod
13
by: John J. Hughes II | last post by:
All, General question. I am working on upgrading my system from a 3.2GHz P4 to something better. 1) Well switching to Windows XP Pro 64 bit cause me massive problem with VS? 2) Is the...
6
by: yxq | last post by:
Hello, The File.Delete(VS2005) function can not delete file on Vista-64bit, why? And, what changes of API between 32-bit and 64-bit? Thank you
7
by: BogusException | last post by:
Why is it that VS 2005, VC++.NET needs 12 bytes to store an int, when it can be done natively with 4? Mind you, even on 64-bit systems the int is still supposed to be 4 bytes. An yes, the _pointer_...
5
Plater
by: Plater | last post by:
So I have been working with the SNTP protocol (RFC4330) And it is saying that the timestamp format of 64bits is: . 1 2 3 0 1...
18
by: cman | last post by:
Hi guys, why does this fail raising bad_alloc int *v = new int ; if this succeeds int *v = (int *) malloc((unsigned)6000000000) both on the same machine, same compiler g++, 64bit linux red...
3
by: Udi | last post by:
Hi All, I'm not sure I understand the difference between placing the __unaligned before or after the *: I was trying to handle the C4366 warning - "The result of the unary '&' operator may be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.