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] "Patrick" <questions@newsgroup.nospam> wrote in message
news:2863A9DC-B695-4F8A-83C8-131A2B8723AD@microsoft.com...
>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?
>
> "Patrick" wrote:
>
>> 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).
>>
>>