sealed public class CryptoHelper
{
/// <summary>
/// Encrypts the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="key">key</param>
/// <param name="iv">iv</param>
/// <returns></returns>
public static string EncryptDes(string input, byte[] key, byte[] iv)
{
if (input == null || == 0)
return ;
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = null;
CryptoStream encStream = null;
StreamWriter sw = null;
string result = ;
try
{
ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP DES key.
// = ;
// = PaddingMode.PKCS7;
encStream = new CryptoStream(ms, (key, iv), );
// Create a StreamWriter to write a string
// to the stream.
sw = new StreamWriter(encStream);
// Write the plaintext to the stream.
(input);
();
();
();
result = Convert.ToBase64String((), 0, Convert.ToInt32(, ));
}
finally
{
//close objects
if (sw != null)
();
if (encStream != null)
();
if (ms != null)
();
}
// Return the encrypted string
return result;
}
/// <summary>
/// Decrypts the specified input.
/// </summary>
/// <param name="input">the input.</param>
/// <param name="key">key</param>
/// <param name="iv">iv</param>
/// <returns></returns>
public static string DecryptDes(string input, byte[] key, byte[] iv)
{
byte[] buffer;
try { buffer = Convert.FromBase64String(input); }
catch () { return ; }
// length is zero, or not an even multiple of four (plus a few other cases)
catch () { return ; }
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = null;
CryptoStream encStream = null;
StreamReader sr = null;
string result = ;
try
{
ms = new MemoryStream(buffer);
// Create a CryptoStream using the memory stream and the
// CSP DES key.
encStream = new CryptoStream(ms, (key, iv), );
// Create a StreamReader for reading the stream.
sr = new StreamReader(encStream);
// Read the stream as a string.
result = ();
}
finally
{
//close objects
if (sr != null)
();
if (encStream != null)
();
if (ms != null)
();
}
return result;
}
}
//Call
string key = "abcdefgh";
string iv = "abcdefgh";
string msg="test string";
string rs1 = (msg, (key), (iv));
string rs2 = (rs1, (key), (iv));