A class that can be used to encrypt/decrypt

Source: Internet
Author: User
Tags decrypt
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
}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.