Content Overview: Title
Language used: C #
Difficulty: Medium
Using system;
Using system. Security. cryptography;
Using system. IO;
Using system. text;
Namespace netoffice. Component
{
/// <Summary>
/// Summary of encryt.
/// </Summary>
Public class encryt
{
Private response ricalgorithm MCSP = new descryptoserviceprovider ();
// Public encryt ()
//{
//
//}
/// <Summary>
/// Constructor with Parameters
/// </Summary>
/// <Param name = "key"> set the secret key of the symmetric algorithm </param>
/// <Param name = "IV"> set the initialization vector of the symmetric algorithm </param>
Public encryt (string key, string IV)
{
MCSP. Key = convert. frombase64string (key );
MCSP. IV = convert. frombase64string (IV );
}
// Encryption function
Public String encrystr (string encryed)
{
Icryptotransform CT; // defines the basic encryption and conversion operations
Memorystream MS; // creates a stream that supports the storage as memory
Cryptostream Cs; // defines the stream that links the data stream to the encrypted conversion stream.
Byte [] BYT;
// Create a symmetric encryptor object using the specified key and initialization Vector
Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );
// Convert incoming values into byte Arrays
Byt = encoding. utf8.getbytes (encryed );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return convert. tobase64string (Ms. toarray ());
}
// Decryption Function
Public String denstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
// Create a symmetric cipher object
Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
Byt = convert. frombase64string (value );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return encoding. utf8.getstring (Ms. toarray ());
}
}
}
<Deleetask>
<Add key = "connectionstring" value = "Server =.; uid = sa; database = netoffice"/>
<Add key = "mykey" value = "Til + b4fj3g4 ="/>
<Add key = "myiv" value = "+ hz8mzpg5c ="/>
</Appsettings>