all groups > visual studio .net enterprise tools > september 2005 >
You're in the

visual studio .net enterprise tools

group:

Encrypting a Web.Config value using Microsoft Enterprise Library-J



Re: Encrypting a Web.Config value using Microsoft Enterprise Library-J William Stacey [MVP]
9/6/2005 12:00:00 AM
visual studio .net enterprise tools: I put methods like these in my Utils class. Easy way to encrypt strings or
byte[]:

/// <summary>
/// Use AES to encrypt data string. The output string is the
encrypted bytes as a base64 string.
/// The same password must be used to decrypt the string.
/// </summary>
/// <param name="data">Clear string to encrypt.</param>
/// <param name="password">Password used to encrypt the
string.</param>
/// <returns>Encrypted result as Base64 string.</returns>
public static string EncryptData(string data, string password)
{
if ( data == null )
throw new ArgumentNullException("data");
if ( password == null )
throw new ArgumentNullException("password");

byte[] encBytes = EncryptData(Encoding.UTF8.GetBytes(data),
password, PaddingMode.ISO10126);
return Convert.ToBase64String(encBytes);
}

/// <summary>
/// Decrypt the data string to the original string. The data must
be the base64 string
/// returned from the EncryptData method.
/// </summary>
/// <param name="data">Encrypted data generated from EncryptData
method.</param>
/// <param name="password">Password used to decrypt the
string.</param>
/// <returns>Decrypted string.</returns>
public static string DecryptData(string data, string password)
{
if ( data == null )
throw new ArgumentNullException("data");
if ( password == null )
throw new ArgumentNullException("password");

byte[] encBytes = Convert.FromBase64String(data);
byte[] decBytes = DecryptData(encBytes, password,
PaddingMode.ISO10126);
return Encoding.UTF8.GetString(decBytes);
}

public static byte[] EncryptData(byte[] data, string password,
PaddingMode paddingMode)
{
if ( data == null || data.Length == 0 )
throw new ArgumentNullException("data");
if ( password == null )
throw new ArgumentNullException("password");

PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
Encoding.UTF8.GetBytes("Salt"));
RijndaelManaged rm = new RijndaelManaged();
rm.Padding = paddingMode;
ICryptoTransform encryptor =
rm.CreateEncryptor(pdb.GetBytes(16), pdb.GetBytes(16));

using ( MemoryStream msEncrypt = new MemoryStream() )
using ( CryptoStream encStream = new CryptoStream(msEncrypt,
encryptor, CryptoStreamMode.Write) )
{
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
return msEncrypt.ToArray();
}
}

public static byte[] DecryptData(byte[] data, string password,
PaddingMode paddingMode)
{
if ( data == null || data.Length == 0 )
throw new ArgumentNullException("data");
if ( password == null )
throw new ArgumentNullException("password");

PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
Encoding.UTF8.GetBytes("Salt"));
RijndaelManaged rm = new RijndaelManaged();
rm.Padding = paddingMode;
ICryptoTransform decryptor =
rm.CreateDecryptor(pdb.GetBytes(16), pdb.GetBytes(16));

using ( MemoryStream msDecrypt = new MemoryStream(data) )
using ( CryptoStream csDecrypt = new CryptoStream(msDecrypt,
decryptor, CryptoStreamMode.Read) )
{
// Decrypted bytes will always be less then encrypted bytes,
so len of encrypted data will be big enouph for buffer.
byte[] fromEncrypt = new byte[data.Length];

// Read as many bytes as possible.
int read = csDecrypt.Read(fromEncrypt, 0,
fromEncrypt.Length);
if ( read < fromEncrypt.Length )
{
// Return a byte array of proper size.
byte[] clearBytes = new byte[read];
Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
return clearBytes;
}
return fromEncrypt;
}
}

--
William Stacey [MVP]

[quoted text, click to view]

Encrypting a Web.Config value using Microsoft Enterprise Library-J Patrick
9/6/2005 3:56:04 AM
The Data EDRA is execellent in allowing Connection String to be encrypted,
but as yet I could not find any example of how to encrypt the following entry:
<appSettings>
<add key="SpecialPassword" value="aPasswordIwantEncrypted" />
</appSettings>

How could I do this? I already have
securitycryptographyconfiguration.config and the encryption .key file setup
(when I set up my Data EDRA to encrypt connection sting password).

RE: Encrypting a Web.Config value using Microsoft Enterprise Library-J Patrick
9/6/2005 8:55:23 AM
I tried using the "Crytography Application Block Quick Start" to encrypt
(getting it the App.Config to point to my own
securityCryptographyconfiguration.config) to encrypt the password into
something like
XVgnl8i//tBn4t3QGMl02TDtqCtMN/0ER/LKe1Burvy/oODQ7N1U9asm2jlOyqUx

I tried to decrypt it in as follows:
String password = System.Text.Encoding.Unicode.GetString(
Cryptographer.DecryptSymmetric("symprovider",
System.Text.Encoding.Unicode.GetBytes
(ConfigurationSettings.AppSettings["ADQuerySysUserPassword"])));

Unfortunately:
the code throws the following exception:
ystem.Security.Cryptography.CryptographicException: PKCS7 padding is invalid
and cannot be removed.
at
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Close()
at
Microsoft.Practices.EnterpriseLibrary.Common.Cryptography.SymmetricCryptographer.Transform(ICryptoTransform transform, Byte[] buffer)
at
Microsoft.Practices.EnterpriseLibrary.Common.Cryptography.SymmetricCryptographer.Decrypt(Byte[] encryptedText)
at
Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.SymmetricAlgorithmProvider.Decrypt(Byte[] ciphertext)
at
Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Cryptographer.DecryptSymmetric(String
symmetricInstance, Byte[] ciphertext, ConfigurationContext context)

Why? and how do I resolve?

[quoted text, click to view]
Re: Encrypting a Web.Config value using Microsoft Enterprise Libra Patrick
9/7/2005 3:12:03 AM
Thanks for the code William, but I am quite interested to know what is wrong
with my original 1 line code. It is simply built on the Microsoft
Cryptography EDRA quick start example!

Something wrong with the getbyte method, perhaps??

[quoted text, click to view]
Re: Encrypting a Web.Config value using Microsoft Enterprise Libra v-garych NO[at]SPAM online.microsoft.com (
9/9/2005 12:00:00 AM
Hi Patrick

Our support engineer Steven Cheng had already posted a response to your
latest question in the thread of microsoft.public.dotnet.framework
newsgroup, please check it there.


Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button