using the encryption method of single-key cryptosystem, the same key can be used as the encryption and decryption of information, which is called symmetric encryption, also known as single-key encryption.
encryption algorithms that use the same key for encryption and decryption are required. Because of its fast speed, symmetric encryption is typically used when the message sender needs to encrypt large amounts of data.
symmetry is the use of the same encryption method used by both parties to encrypt and decrypt the same key.
A key is an instruction that controls the process of encrypting and decrypting. The algorithm is a set of rules that specify how to encrypt and decrypt.
Therefore, the security of encryption not only depends on the encryption algorithm itself, but also the security of key management is more important.
because both encryption and decryption use the same key, how to safely pass the key to the decryption hand is a problem that must be solved.
Rijndael class
represents the base class from which all implementations of the Rijndael symmetric cryptographic algorithm must inherit.
namespaces: System.Security.Cryptography
Rijndael.create Method:
creates a cryptographic object to execute the Rijndael algorithm.
Example:
Rijndael crypt = Rijndael.create ();
Properties:
IV: Gets or sets the initialization vector of the symmetric algorithm.
Key : Gets or sets the key for the symmetric algorithm.
Example:
byte[] key = new Byte[32] {0xa6, 0x7d, 0xe1, 0X3F, 0X35, 0X0E, 0xe1, 0xa9, 0X83, 0xa5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0x98, 0xa7, 0X33, 0X49, 0XFF, 0xe6, 0XAE, 0XBF, 0x8d, 0x8d, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40};
byte[] IV = new BYTE[16] {0XF8, 0x8b, 0x01, 0XFB, 0X08, 0X85, 0x9a, 0xa4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0xf6, 0 X19};
Crypt. key = key;
crypt.iv = IV;
CreateEncryptor () method
creates a symmetric cryptographic object with the current key property and initialization phase (IV).
ICryptoTransform Interface
defines a basic cryptographic conversion operation.
Crytostream class
define a stream that links a data flow to an encryption transformation
constructor Syntax:
Public Crytostream (Stream stream, ICryptoTransform transform, Crytostreammode mode);
where stream represents the stream on which the cryptographic transformation is performed; Tranform represents the cryptographic transformation to be performed by the stream.
Cryptostream.write Method:
writes a sequence of bytes to the current Crytostream and increases the number of bytes written to the current position in the stream.
syntax: public override void Write (byte[] buffer, int offset, int count);
Cryptostream.flushfinalblock Method:
updates the data base source or repository with the current state of the buffer, and then clears the buffer.
MemoryStream class
Create a stream whose support store is memory.
constructor: Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.
MemoryStream ms = new MemoryStream ();
Menorystream.read Method
reads a block of bytes from the current stream and writes the data to buffer
syntax: public override int Read (byte[] buffer, int offset, int count);
UTF8Encoding class
represents the UTF-8 encoding of Unicode characters.
constructor: Initializes a new instance of the UTF8Encoding class.
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding ();
Encoding.GetBytes Method (String)
encodes all the characters in the specified string into a sequence of bytes.
byte[] RawData = Enc. GetBytes (text);
encoding.getstring Method (byte[])
decodes a sequence of bytes into a string.
Enc. GetString (EncryptedData);
**************************************************************
Cryptographic functions:
private String Encrypt (string text)
{Rijndael crypt = Rijndael.create ();
byte[] key = new Byte[32] {0xa6, 0x7d, 0xe1, 0X3F, 0X35, 0X0E, 0xe1, 0xa9, 0X83, 0xa5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0x98, 0xa7, 0X33, 0X49, 0XFF, 0xe6, 0XAE, 0XBF, 0x8d, 0x8d, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40};
byte[] IV = new BYTE[16] {0XF8, 0x8b, 0x01, 0XFB, 0X08, 0X85, 0x9a, 0xa4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0xf6, 0 X19};
Crypt. key = key;
crypt.iv = IV;
MemoryStream ms = new MemoryStream ();
ICryptoTransform transtormencode = new Tobase64transform ();
//BASE64 Encoding
CryptoStream csencode = new CryptoStream (MS, Transtormencode, cryptostreammode.write);
CryptoStream csencrypt = new CryptoStream (Csencode, Crypt. CreateEncryptor (), cryptostreammode.write);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding ();
byte[] RawData = Enc. GetBytes (text);
csencrypt.write (rawdata, 0, rawdata.length);
Csencrypt.flushfinalblock ();
byte[] EncryptedData = new Byte[ms. Length];
Ms. Position = 0;
Ms. Read (EncryptedData, 0, (int) Ms. Length);
return ENC. GetString (EncryptedData);
}
************************************************************************
decryption function:
private String Decrypt (string text)
{
Rijndael crypt = Rijndael.create ();
byte[] key = new Byte[32] {0xa6, 0x7d, 0xe1, 0X3F, 0X35, 0X0E, 0xe1, 0xa9, 0X83, 0xa5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0x98, 0xa7, 0X33, 0X49, 0XFF, 0xe6, 0XAE, 0XBF, 0x8d, 0x8d, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40};
byte[] IV = new BYTE[16] {0XF8, 0x8b, 0x01, 0XFB, 0X08, 0X85, 0x9a, 0xa4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0xf6, 0 X19};
Crypt. key = key;
crypt.iv = IV;
MemoryStream ms = new MemoryStream ();
CryptoStream csdecrypt = new CryptoStream (MS, Crypt. CreateDecryptor (), cryptostreammode.write);
ICryptoTransform transformdecode = new Frombase64transform ();
CryptoStream csdecode = new CryptoStream (Csdecrypt, Transformdecode, cryptostreammode.write);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding ();
byte[] RawData = Enc. GetBytes (text);
csdecode.write (rawdata, 0, rawdata.length);
Csdecode.flushfinalblock ();
byte[] Decrypteddata = new Byte[ms. Length];
Ms. Position = 0;
Ms. Read (decrypteddata, 0, (int) Ms. Length);
return (Enc. GetString (Decrypteddata));
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # symmetric encryption algorithm implementation and related class description