Encrypt and decrypt a string in C#?
Encrypt and decrypt a string in C#?
Encrypt and decrypt a string in C#?
Re: Encrypt and decrypt a string in C#?
**EDIT 2013-Oct**: Although I've edited this answer over time to address shortcomings, please see [jbtule's answer](https://stackoverflow.com/a/10366194/157247) for a more robust, informed solution.
[https://stackoverflow.com/a/10366194/188474](https://stackoverflow.com/a/10366194/188474)
**Original Answer:**
Here's a working example derived from the ["RijndaelManaged Class" documentation](http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged%28v=VS.90%29.aspx) and the [MCTS Training Kit](https://rads.stackoverflow.com/amzn/click/com/0735626197).
**EDIT 2012-April**: This answer was edited to pre-pend the IV per jbtule's suggestion and as illustrated here:
[http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged%28v=vs.95%29.aspx](http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged%28v=vs.95%29.aspx)
Good luck!
```
public class Crypto
{
//While an app specific salt is not the best practice for
//password based encryption, it's probably safe enough as long as
//it is truly uncommon. Also too much work to alter this answer otherwise.
private static byte[] _salt = __To_Do__("Add a app specific salt here");
///
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
///
/// The text to encrypt.
/// A password used to generate a key for encryption.
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null;
*(Réponse tronquée)*
[https://stackoverflow.com/a/10366194/188474](https://stackoverflow.com/a/10366194/188474)
**Original Answer:**
Here's a working example derived from the ["RijndaelManaged Class" documentation](http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged%28v=VS.90%29.aspx) and the [MCTS Training Kit](https://rads.stackoverflow.com/amzn/click/com/0735626197).
**EDIT 2012-April**: This answer was edited to pre-pend the IV per jbtule's suggestion and as illustrated here:
[http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged%28v=vs.95%29.aspx](http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged%28v=vs.95%29.aspx)
Good luck!
```
public class Crypto
{
//While an app specific salt is not the best practice for
//password based encryption, it's probably safe enough as long as
//it is truly uncommon. Also too much work to alter this answer otherwise.
private static byte[] _salt = __To_Do__("Add a app specific salt here");
///
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
///
/// The text to encrypt.
/// A password used to generate a key for encryption.
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null;
*(Réponse tronquée)*