/// <Summary>
/// Use des to encrypt the string
/// </Summary>
/// <Param name = "stringtoencrypt"> string to be encrypted </param>
/// <Param name = "strkey"> 8-character encryption key </param>
/// <Returns> </returns>
Public static string encryptstringbydes (string stringtoencrypt, string key)
{
If (string. isnullorempty (key) | encoding. utf8.getbytes (key). length! = 8)
Key = "$ ap1bi $ .";
Byte [] Key = {};
Byte [] IV = {0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
Try
{
Key = encoding. utf8.getbytes (key );
Using (descryptoserviceprovider odescrypto = new descryptoserviceprovider ())
{
Byte [] inputbytearray = encoding. utf8.getbytes (stringtoencrypt );
Memorystream omemorystream = new memorystream ();
Cryptostream ocryptostream = new cryptostream (omemorystream,
Odescrypto. createencryptor (Key, IV), cryptostreammode. Write );
Ocryptostream. Write (inputbytearray, 0, inputbytearray. Length );
Ocryptostream. flushfinalblock ();
Return convert. tobase64string (omemorystream. toarray ());
}
}
Catch (exception ex)
{
Throw ex;
}
}
/// <Summary>
/// Use des to decrypt the string
/// </Summary>
/// <Param name = "stringtodecrypt"> string to be decrypted </param>
/// <Param name = "strkey"> 8-bit decryption key </param>
/// <Returns> </returns>
Public static string decryptstringbydes (string stringtodecrypt, string key)
{
If (string. isnullorempty (key) | encoding. utf8.getbytes (key). length! = 8)
Key = "$ apa1i $ .";
Byte [] Key = {};
Byte [] IV = {0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
Stringtodecrypt = stringtodecrypt. Replace ("", "+ ");
Byte [] inputbytearray = new byte [stringtodecrypt. Length];
Try
{
Key = encoding. utf8.getbytes (key );
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (stringtodecrypt );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (Key, IV), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Encoding encoding = encoding. utf8;
Return encoding. getstring (Ms. toarray ());
}
Catch (exception ex)
{
Throw ex;
}
}