This blog post lists the. NET under the common symmetric encryption algorithm, and they are made into small demo, hope to be helpful to everyone.
Common code
Static byte[] CreateKey (int num)
{
byte[] result = new Byte[num];
Random rand = new Random ();
for (int i = 0; i < num i++)
{
Result[i] = (Byte) rand. Next (1, 256);
}
return result;
}
Des
<summary>///des encryption algorithm must use Base64 byte object///</summary>///<param name= "Data" > character data to be encrypted </par am>///<param name= "key" > key, length must be 64 bits (byte[8)) </param>///<param name= "IV" >iv vector, must be 64 bits in length (byte
[8]) </param>///<returns> encrypted character </returns> static string endes (string data, byte[] key, byte[] IV) {des des = des.
Create ();
This line of code is important and requires different conversion formats to be selected according to different strings byte[] tmp = Encoding.Unicode.GetBytes (data);
Byte[] Encryptodata; ICryptoTransform encryptor = des.
CreateEncryptor (key, iv); using (MemoryStream MemoryStream = new MemoryStream ()) {using (var cs = new CryptoStream (MemoryStream, en
Cryptor, CryptoStreamMode.Write)) {using (StreamWriter writer = new StreamWriter (CS)) {writer.
Write (data); Writer.
Flush ();
} Encryptodata = Memorystream.toarray ();
} Des.
Clear ();
Return convert.tobase64string (Encryptodata);
///<summary>///des decryption algorithm///</summary>///<param name= "Data" > Encrypted character data </param> <param name= "key" > key, length must be 64 bits (byte[8)) </param>///<param name= "IV" >IV vector, length must be 64 bits (BYTE[8))
</param>///<returns> Encrypted character </returns> static string Dedes (string data, byte[] key, byte[] IV { String resultdata = String.
Empty; This line of code is important byte[] Tmpdata = convert.frombase64string (data);//The format of the conversion is very important des des = des.
Create (); ICryptoTransform decryptor = des.
CreateDecryptor (key, iv); using (var MemoryStream = new MemoryStream (tmpdata)) {using (var cs = new CryptoStream (MemoryStream, DECR
Yptor, CryptoStreamMode.Read)) {StreamReader reader = new StreamReader (CS); Resultdata = reader.
ReadLine (); } return resultData; }
Calling code
Des symmetric encryption
Console.WriteLine ("Des symmetric Encryption");
Byte[] IV = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
byte[] key = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
String inputdes_1 = InputString ();
String endata = Endes (Inputdes_1, Key, iv);<p>
Console.WriteLine ("Encrypted data: {0}", endata);
Console.WriteLine ("Decrypted data: {0}", Dedes (Endata, key, iv));</p>
TripleDES (3DES)
///<summary>///tripledes encryption///</summary>///<param name= "Data" > character data to be encrypted </param> <param name= "key" > key, Length can be: 128-bit (byte[16]), 192-bit (byte[24)) </param>///<param name= "IV" >iv vector, Length must be 64-bit (byte[8]) </param>///<returns> Encrypted characters </returns> static string Entripledes (string data, byte
[] key, byte[] IV {byte[] tmp = NULL;
byte[] Tmpdata = Encoding.Unicode.GetBytes (data);
TripleDES TripleDES = Tripledes.create ();
ICryptoTransform encryptor = Tripledes.createencryptor (key, iv); using (MemoryStream ms = new MemoryStream ()) {using (CryptoStream cs = new CryptoStream (MS, Encryptor, Cr
Yptostreammode.write)) {StreamWriter writer = new StreamWriter (CS); Writer.
Write (data); Writer. Flush ()//This sentence is very important, after the convection operation must use this sentence to force all data in the buffer to be written to the target object} tmp = Ms.
ToArray (); Return convert.toBase64string (TMP); ///<summary>///tripledes decryption///</summary>///<param name= "Data" > character data to be encrypted </PARAM&G
T <param name= "key" > key, Length can be: 128-bit (byte[16]), 192-bit (byte[24)) </param>///<param name= "IV" >iv vector, Length must be 64-bit (byte[8]) </param>///<returns> Encrypted characters </returns> static string Detripledes (string data, byte
[] key, byte[] IV {byte[] tmp = convert.frombase64string (data); string result = String.
Empty;
TripleDES TripleDES = Tripledes.create ();
ICryptoTransform decryptor = Tripledes.createdecryptor (key, iv); using (MemoryStream ms = new MemoryStream (tmp)) {using (CryptoStream cs = new CryptoStream (MS, Decryptor,
CryptoStreamMode.Read)) {StreamReader reader = new StreamReader (CS); result = reader.
ReadLine ();
} tripledes.clear ();
return result; }