Data encryption with C # (i)--Symmetric encryption algorithm

Source: Internet
Author: User
Encryption | data | algorithm using C # Data encryption (i)--Symmetric encryption algorithm



The following is about the symmetric encryption algorithm of C # implementation code, you can change the different algorithm according to the need, the article takes Rijndael algorithm for example:

Using System;
Using System.IO;
Using System.Security.Cryptography;
Using System.Text;

Namespace Datacrypto
{
<summary>
Symmetric encryption Algorithm Class
</summary>
public class Symmetricmethod
{

Private SymmetricAlgorithm Mobjcryptoservice;
private string Key;
<summary>
Constructors for symmetric cryptographic classes
</summary>
Public Symmetricmethod ()
{
Mobjcryptoservice = new RijndaelManaged ();
Key = "Guz" (%&hj7x89h$yubi0456ftmat5&fvhufcy76*h% (hilj$lhj!y6&) (*JKP87JH7;
}
<summary>
Get key
</summary>
<returns> Key </returns>
Private byte[] Getlegalkey ()
{
String stemp = Key;
Mobjcryptoservice.generatekey ();
byte[] Byttemp = Mobjcryptoservice.key;
int keylength = Byttemp.length;
if (Stemp.length > Keylength)
stemp = stemp.substring (0, keylength);
else if (Stemp.length < keylength)
Stemp = Stemp.padright (Keylength, ");
Return ASCIIEncoding.ASCII.GetBytes (stemp);
}
<summary>
Get the initial vector IV
</summary>
<returns> Preliminary Vector iv</returns>
Private byte[] Getlegaliv ()
{
String stemp = "E4ghj*ghg7!rnifb&95guy86gfghub#er57hbh (u%g6hj ($jhWk 7&!hg4ui% $HJK";
Mobjcryptoservice.generateiv ();
byte[] Byttemp = MOBJCRYPTOSERVICE.IV;
int ivlength = Byttemp.length;
if (Stemp.length > Ivlength)
stemp = stemp.substring (0, ivlength);
else if (Stemp.length < ivlength)
Stemp = Stemp.padright (Ivlength, ");
Return ASCIIEncoding.ASCII.GetBytes (stemp);
}
<summary>
Encryption method
</summary>
<param name= "Source" > Strings to be encrypted </param>
<returns> Encrypted String </returns>
public string Encrypto (string Source)
{
byte[] Bytin = UTF8Encoding.UTF8.GetBytes (Source);
MemoryStream ms = new MemoryStream ();
Mobjcryptoservice.key = Getlegalkey ();
MOBJCRYPTOSERVICE.IV = Getlegaliv ();
ICryptoTransform encrypto = Mobjcryptoservice.createencryptor ();
CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Write);
Cs. Write (bytin, 0, bytin.length);
Cs. FlushFinalBlock ();
Ms. Close ();
Byte[] Bytout = Ms. ToArray ();
Return convert.tobase64string (bytout);
}
<summary>
Decryption method
</summary>
<param name= "Source" > String to be decrypted </param>
<returns> decrypted String </returns>
public string Decrypto (string Source)
{
byte[] Bytin = convert.frombase64string (Source);
MemoryStream ms = new MemoryStream (bytin, 0, bytin.length);
Mobjcryptoservice.key = Getlegalkey ();
MOBJCRYPTOSERVICE.IV = Getlegaliv ();
ICryptoTransform encrypto = Mobjcryptoservice.createdecryptor ();
CryptoStream cs = new CryptoStream (MS, Encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader (CS);
return Sr. ReadToEnd ();
}
}
}



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.