Rijndael Encryption
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > Microsoft Commerce Server > Commerce Server General > Rijndael Encryption




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Rijndael Encryption  
lee


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-30-04 03:40 PM

Hi, I’m having some problems with Rijndael encryption in C#. I need to sen
d
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.





[ Post a follow-up to this message ]



    RE: Rijndael Encryption  
lee


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-30-04 03:40 PM

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 strin
g
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 s
end
> 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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:20 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register