Share the AES reversible encryption algorithm you saw in the previous period.
In addition to the common encryption methods such as MD5, if you want to use some more subtle encryption, you can use AES's RijndaelManaged encryption algorithm.
About encryption, there are many complex algorithms, today only to share with you a extract of a combination of dynamic key symmetric AES RijndaelManaged encryption and decryption algorithm, if you are interested to learn more, we can study together in depth ...
static void Main (string[] args)
{
string key = "8h[=}[email protected] (";
Cryptohelper helper = new Cryptohelper (key);
if (args. Length = = 2 && args[0] = = "-E")
{
Console.WriteLine ("Encrypt password: {0}", args[1]);
String a = helper. Encrypt (Args[1]);
Console.WriteLine ("Encrypt result is: {0}", a);
}
else if (args. Length = = 2 && args[0] = = "-D")
{
Console.WriteLine ("Decrypt password: {0}", args[1]);
String a = helper. Decrypt (Args[1]);
Console.WriteLine ("Decrypt result is: {0}", a);
}
}
Public class Cryptohelper
{
Private Const string _defaultintializationvector = "%[email protected]) 5:-";
Private RijndaelManaged _crypto;
private static Encoding _encoding = Encoding.ascii;
Private Cryptohelper ()
{
}
<summary>
RijndaelManaged (AES)
</summary>
<param name= "key" >the key for Encrypt</param>
Public Cryptohelper (String key)
{
This._crypto = new RijndaelManaged ();
This._crypto. Key = cryptohelper._encoding. GetBytes (key);
THIS._CRYPTO.IV = cryptohelper._encoding. GetBytes ("%[email protected]) 5:-");
}
<summary>
Encrypt the string
</summary>
<param name= "s" >encrypt string</param>
<returns>encrypt result</returns>
public string Encrypt (string s)
{
byte[] bytes = cryptohelper._encoding. GetBytes (s);
MemoryStream MemoryStream = new MemoryStream ();
CryptoStream cryptostream = new CryptoStream (MemoryStream, This._crypto. CreateEncryptor (), cryptostreammode.write);
Cryptostream.write (bytes, 0, bytes. Length);
Cryptostream.close ();
Memorystream.close ();
byte[] array = Memorystream.toarray ();
if (array = = NULL | | array. Length = = 0)
{
Return "";
}
StringBuilder StringBuilder = new StringBuilder ();
Byte[] Array2 = array;
for (int i = 0; i < array2. Length; i++)
{
byte B = array2[i];
Stringbuilder.append (String. Format ("{0:x2}", b));
}
return stringbuilder.tostring ();
}
<summary>
Decrypt string
</summary>
<param name= "Hexvalue" ></param>
<returns></returns>
public string Decrypt (string hexvalue)
{
Byte[] Array;
if (Hexvalue = = NULL | | hexvalue.length = = 0)
{
Array = null;
}
Else
{
int num = Convert.ToInt32 (HEXVALUE.LENGTH/2);
Array = new Byte[num];
for (int i = 0; I <= num-1; i++)
{
Array[i] = Convert.tobyte (hexvalue.substring (i * 2, 2), 16);
}
}
MemoryStream stream = new MemoryStream (array, 0, array. Length);
byte[] array2 = new Byte[array. LENGTH-1];
CryptoStream cryptostream = new CryptoStream (stream, This._crypto. CreateDecryptor (), cryptostreammode.read);
Try
{
Cryptostream.read (array2, 0, array. LENGTH-1);
}
catch (Cryptographicexception inner)
{
throw new Cryptographicexception ("Unable to decrypt data. The provided key may be invalid. ", inner);
}
Finally
{
Cryptostream.close ();
}
int num2 = array.indexof<byte> (array2, 0);
if (num2 >= 0)
{
Return cryptohelper._encoding. GetString (array2, 0, num2);
}
Return cryptohelper._encoding. GetString (array2);
}
}
AES Reversible encryption algorithm