|
It is recommended to use DES or RSA, this is really encryption.
MD5 is a hash value, not really encrypted, just a hash
Posting a DES method gives you:
// Initialize the DES encryption key and a random, 8-bit initialization vector (IV)
private byte [] key_8 = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
private byte [] IV_8 = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
/// <summary>
/// DES decryption string
/// </ summary>
/// <param name = "decryptString"> string to be decrypted </ param>
/// <param name = "rgbKey"> Decryption key, requires 8 bits, same as encryption key </ param>
/// <param name = "rgbIV"> key vector </ param>
/// <returns> Successful decryption returns the decrypted string, failure to return to the source string </ returns>
private string DES_Decrypt (string decryptString, byte [] rgbKey, byte [] rgbIV)
{
try
{
byte [] inputByteArray = Convert.FromBase64String (decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mStream = new MemoryStream ();
CryptoStream cStream = new CryptoStream (mStream, DCSP.CreateDecryptor (rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write (inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock ();
return Encoding.UTF8.GetString (mStream.ToArray ());
}
catch
{
return decryptString;
}
}
/// <summary>
/// TripleDES encrypted string
/// </ summary>
/// <param name = "encryptString"> string to be encrypted </ param>
/// <param name = "rgbKey"> key </ param>
/// <param name = "rgbIV"> key vector </ param>
/// <returns> The encrypted string is returned after the encryption is successful, or the source string is returned if the encryption fails </ returns>
private string TripleDES_Encrypt (string encryptString, byte [] rgbKey, byte [] rgbIV)
{
try
{
byte [] inputByteArray = Encoding.UTF8.GetBytes (encryptString);
TripleDESCryptoServiceProvider dCSP = new TripleDESCryptoServiceProvider ();
MemoryStream mStream = new MemoryStream ();
CryptoStream cStream = new CryptoStream (mStream, dCSP.CreateEncryptor (rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write (inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock ();
return Convert.ToBase64String (mStream.ToArray ());
}
catch
{
return encryptString;
}
} |
|