Using system;
Using system. diagnostics;
Using system. Security. cryptography;
Using system. text;
Using system. IO;
Namespace cryptoutil
{
/// <Summary>
/// Summary of cryptoutil.
/// </Summary>
Public class cryptoutil
{
// Randomly select 8 bytes as both the key and initial vector
Byte [] bykey64 = {42, 16, 93,156, 78, 4,218, 32 };
Byte [] iv64 = {55,103,246, 79, 36, 99,167, 3 };
Byte [] bykey192 = {42, 16, 93,156, 78, 4,218, 167, 26,250,155,112, 44, 80, 11,204,119, 35,184,197 };
Byte [] iv192 = {55,103,246, 79, 36, 99,167, 83,184, 7,209, 5, 62, 13,145, 23,200, 58,173, 10,121,222 };
Public cryptoutil ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Standard DES encryption
/// </Summary>
/// <Param name = "str_ SQL"> standard DES encryption </param>
Private string encrypt (string strtext)
{
// Byte [] bykey = {};
// Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Try
{
// Bykey = system. Text. encoding. utf8.getbytes (strencrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. utf8.getbytes (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (bykey64, iv64), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (exception ex)
{
Return ex. message;
}
}
/// <Summary>
/// Standard des decryption
/// </Summary>
/// <Param name = "str_ SQL"> standard des decryption </param>
Private string decrypt (string strtext)
{
// Byte [] bykey = {};
// Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Byte [] inputbytearray = new byte [strtext. Length];
Try
{
// Bykey = system. Text. encoding. utf8.getbytes (sdecrkey. substring (0, 8 ));
Descryptoserviceprovider des = new descryptoserviceprovider ();
Inputbytearray = convert. frombase64string (strtext );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (bykey64, iv64), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
System. Text. Encoding encoding = system. Text. encoding. utf8;
Return encoding. getstring (Ms. toarray ());
}
Catch (exception ex)
{
Return ex. message;
}
}
}
}