Commerce Server General - Rijndael Encryption

This is Interesting: Free IT Magazines  
Home > Archive > Commerce Server General > September 2004 > Rijndael Encryption





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Rijndael Encryption
lee

2004-09-30, 10:40 am

Hi, I’m having some problems with Rijndael encryption in C#. I need to send
an encrypted message to a device which is using the standard AES 128 bit
algorithm to decrypt the message, here is an example of what I’m trying to
achieve.

Unencrypted message = C87857C90B5413070000000000000000

The encrypted message should look like this.

Encrypted message = F4F4EE2055B928DAF8891E34A4D9110B

The key used = 0A00123456789ABC0123456789ABCDEF

The device I’m sending the message to is using the standard AES 128 bit
algorithm, there is no IV and I believe the cipher mode is ECB.

What I actually get when I’ve encrypted the message is.

Encrypted message = 9fTuIFW5KNr4iR40pNkRCw==

Here is my encryption method.

/// <summary>
/// Encrypt a plain text string with the given 128 bit key.
/// </summary>
/// <param name="plainText">Plain text string</param>
/// <param name="key">16 byte hex string</param>
/// <returns>Encrypted string</returns>
public static string Encrypt(string plainText,string key)
{
// First we need to turn the input strings into a byte array.
byte[] PlainText = StringToByteArray(plainText);

// We are now going to create an instance of the
// Rihndael class.
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.KeySize = 128;
RijndaelCipher.BlockSize = 128;
RijndaelCipher.Mode = CipherMode.ECB;
RijndaelCipher.Padding = PaddingMode.None;

// Create an encrypter
RijndaelCipher.Key = StringToByteArray(key);
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor();

// Create a MemoryStream to hold the encrypted bytes
MemoryStream memoryStream = new MemoryStream();

// Create a CryptoStream.
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor,
CryptoStreamMode.Write);

// Start the encryption process.
cryptoStream.Write(PlainText, 0, PlainText.Length);

// Finish encrypting.
cryptoStream.FlushFinalBlock();

// Convert our encrypted data from a memoryStream into a byte array.
byte[] CipherBytes = memoryStream.ToArray();

// Close both streams.
memoryStream.Close();
cryptoStream.Close();

// Convert encrypted data into a base64-encoded string.
string EncryptedData = Convert.ToBase64String(CipherBytes);

// Return encrypted string.
return EncryptedData;
}

/// <summary>
/// Convert a string to a byte array
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] StringToByteArray(string value)
{
string inValue = value;
byte[] outValue;
int bc = 0;

outValue = new byte[inValue.Length/2];

for(int lc=0; lc<inValue.Length; lc+=2)
{
outValue[bc++] = Convert.ToByte(inValue.Substring(lc,2),16);
}
return outValue;
}

--
Lee.
lee

2004-09-30, 10:40 am

I found the probllem. Its the Convert.ToBase64String thats scewing up the
encryption. I used a method I wrote to convert the message back into a string
and it works fine.

/// <summary>
/// Convert byte array to a string
/// </summary>
/// <param name="value">Byte array</param>
/// <returns>String</returns>
public static string ByteArrayToString(byte[] value)
{
StringBuilder inValue = new StringBuilder(value.Length*2);

for(int lc=0; lc<value.Length; lc++)
{
inValue.AppendFormat(string.Format("{0:X2}",value[lc]));
}
return inValue.ToString();
}


"lee" wrote:

> Hi, I’m having some problems with Rijndael encryption in C#. I need to send
> an encrypted message to a device which is using the standard AES 128 bit
> algorithm to decrypt the message, here is an example of what I’m trying to
> achieve.
>
> Unencrypted message = C87857C90B5413070000000000000000
>
> The encrypted message should look like this.
>
> Encrypted message = F4F4EE2055B928DAF8891E34A4D9110B
>
> The key used = 0A00123456789ABC0123456789ABCDEF
>
> The device I’m sending the message to is using the standard AES 128 bit
> algorithm, there is no IV and I believe the cipher mode is ECB.
>
> What I actually get when I’ve encrypted the message is.
>
> Encrypted message = 9fTuIFW5KNr4iR40pNkRCw==
>
> Here is my encryption method.
>
> /// <summary>
> /// Encrypt a plain text string with the given 128 bit key.
> /// </summary>
> /// <param name="plainText">Plain text string</param>
> /// <param name="key">16 byte hex string</param>
> /// <returns>Encrypted string</returns>
> public static string Encrypt(string plainText,string key)
> {
> // First we need to turn the input strings into a byte array.
> byte[] PlainText = StringToByteArray(plainText);
>
> // We are now going to create an instance of the
> // Rihndael class.
> RijndaelManaged RijndaelCipher = new RijndaelManaged();
> RijndaelCipher.KeySize = 128;
> RijndaelCipher.BlockSize = 128;
> RijndaelCipher.Mode = CipherMode.ECB;
> RijndaelCipher.Padding = PaddingMode.None;
>
> // Create an encrypter
> RijndaelCipher.Key = StringToByteArray(key);
> ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor();
>
> // Create a MemoryStream to hold the encrypted bytes
> MemoryStream memoryStream = new MemoryStream();
>
> // Create a CryptoStream.
> CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor,
> CryptoStreamMode.Write);
>
> // Start the encryption process.
> cryptoStream.Write(PlainText, 0, PlainText.Length);
>
> // Finish encrypting.
> cryptoStream.FlushFinalBlock();
>
> // Convert our encrypted data from a memoryStream into a byte array.
> byte[] CipherBytes = memoryStream.ToArray();
>
> // Close both streams.
> memoryStream.Close();
> cryptoStream.Close();
>
> // Convert encrypted data into a base64-encoded string.
> string EncryptedData = Convert.ToBase64String(CipherBytes);
>
> // Return encrypted string.
> return EncryptedData;
> }
>
> /// <summary>
> /// Convert a string to a byte array
> /// </summary>
> /// <param name="value"></param>
> /// <returns></returns>
> public static byte[] StringToByteArray(string value)
> {
> string inValue = value;
> byte[] outValue;
> int bc = 0;
>
> outValue = new byte[inValue.Length/2];
>
> for(int lc=0; lc<inValue.Length; lc+=2)
> {
> outValue[bc++] = Convert.ToByte(inValue.Substring(lc,2),16);
> }
> return outValue;
> }
>
> --
> Lee.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com