A class that can be used to encrypt/decrypt
Last Update:2017-02-28
Source: Internet
Author: User
Encryption | Decryption can be used to add/Decrypt database users, passwords, etc.
Using System;
Using System.IO;
Using System.Text;
Using System.Security.Cryptography;
Namespace Common
{
<summary>
Summary description of the Securityservice.
</summary>
public class Securityservice
{
Static protected byte[] Bytekey = {125, 14, 45, 67, 112, 79, 77, 99, 37, 104, 13, 9, 118, 51, 87, 108};
Static protected byte[] Byteiv = {86, 19, 79, 15, 72, 58, 117, 45};
static public string Symmetricencrypt (string splaintext)
{
Byte[] Byteplaintext;
MemoryStream Encryptedstream;
ICryptoTransform encryptor;
CryptoStream Thecryptostream;
if (splaintext== "") return "";
Byteplaintext = Encoding.ASCII.GetBytes (Splaintext);
Encryptedstream = new MemoryStream (splaintext.length);
Encryptor = Getencryptor ();
Thecryptostream = new CryptoStream (Encryptedstream, Encryptor, CryptoStreamMode.Write);
Thecryptostream.write (byteplaintext, 0, byteplaintext.length);
Thecryptostream.flushfinalblock ();
Thecryptostream.close ();
Return convert.tobase64string (Encryptedstream.toarray ());
}//end Function
static public string Symmetricdecrypt (string sencryptedtext)
{
Byte[] byteencrypted;
MemoryStream Plaintextstream;
ICryptoTransform decryptor;
CryptoStream Thecryptostream;
if (Sencryptedtext = = "") return "";
byteencrypted = convert.frombase64string (Sencryptedtext.trim ());
Plaintextstream = new MemoryStream (sencryptedtext.length);
Decryptor = Getdecryptor ();
Thecryptostream = new CryptoStream (Plaintextstream, Decryptor, CryptoStreamMode.Write);
Thecryptostream.write (byteencrypted, 0, byteencrypted.length);
Thecryptostream.flushfinalblock ();
Thecryptostream.close ();
Return Encoding.ASCII.GetString (Plaintextstream.toarray ());
}//end Function
Static private ICryptoTransform Getencryptor ()
{
Rc2cryptoserviceprovider CryptoProvider = new Rc2cryptoserviceprovider ();
Cryptoprovider.mode = CIPHERMODE.CBC;
Return Cryptoprovider.createencryptor (Bytekey, BYTEIV);
}//end Function
Static private ICryptoTransform Getdecryptor ()
{
Rc2cryptoserviceprovider CryptoProvider = new Rc2cryptoserviceprovider ();
Cryptoprovider.mode = CIPHERMODE.CBC;
Return Cryptoprovider.createdecryptor (Bytekey, BYTEIV);
}//end Function
}
}