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

F

fineman

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;
}
}
 
K

Kelly Ethridge

Hello,

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

Kelly
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;
}
}
 
F

fineman

Thanks for your instruction and help.

The problem had been solved.

Kelly Ethridge said:
Hello,

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

Kelly
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top