The article will continue to be revenue. NET encryption algorithms, these algorithms will adopt a policy model for unified management.
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Encryptanddecrypt
{public
class Encryptanddecrythandler
{
Iencrytanddecrypt _ Encryptanddecrypt;
Public Encryptanddecrythandler (Iencrytanddecrypt iencrytanddecrypt)
{
_encryptanddecrypt = Iencrytanddecrypt;
}
public string Encrypt (string data)
{return
_encryptanddecrypt.encrypt (data);
}
public string Decrypt (string data)
{return
_encryptanddecrypt.decrypt (data);
}
}
}
Cryptographic decryption of the caller
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Encryptanddecrypt
{public
interface Iencrytanddecrypt
{
string Encrypt (string data);
String Decrypt (string data);
}
}
Policy interface
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Cryptography;
Using System.IO; Namespace Encryptanddecrypt {public class Descrypt:iencrytanddecrypt {private byte[] Rgbkey = NE
W byte[] {1, 9, 8, 7, 0, 1, 0, 3};
Private byte[] Rgbiv = new byte[] {1, 9, 8, 7, 0, 1, 0, 3}; #region Iencrytanddecrypt member public string Encrypt (string data) {MemoryStream ms
Tream = new MemoryStream ();
DESCryptoServiceProvider Deskey = new DESCryptoServiceProvider (); CryptoStream cstream = new CryptoStream (Mstream, Deskey.
CreateEncryptor (RGBKEY,RGBIV), cryptostreammode.write);
StreamWriter sw = new StreamWriter (Cstream); Sw.
WriteLine (data); Sw.
Close (); Cstream.
Close (); byte[] bytes = Mstream.
ToArray (); Mstream.
Close (); Return convert.toBase64string (bytes);
public string Decrypt (string data) {string tmp;
MemoryStream mstream = new MemoryStream (convert.frombase64string (data));
DESCryptoServiceProvider Deskey = new DESCryptoServiceProvider (); CryptoStream cstream = new CryptoStream (Mstream, Deskey.
CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.read);
StreamReader sr = new StreamReader (cstream); TMP = Sr.
ReadLine (); Sr.
Close (); Cstream.
Close (); Mstream.
Close ();
return TMP; } #endregion}}
DES encryption algorithm
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Security.Cryptography;
Using System.IO;
Namespace Encryptanddecrypt {public class Tripledescrypt:iencrytanddecrypt {private byte[] rgbkey;
Private byte[] RGBIV;
Public Tripledescrypt () {Rgbkey = new byte[130];
RGBIV = new byte[130]; for (int i = 0; i < 130 i++) {Rgbkey[i] = rgbiv[i] = (byte) (New Random ().
Next (1, 255));
#region Iencrytanddecrypt member public string Encrypt (string data)
{TripleDESCryptoServiceProvider tripledes=new tripledescryptoserviceprovider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, Tripledes.createencryptor (Rgbkey, Rgbiv), cryptostreammode.write); Streamwriter SW = new StreamWriter (Cstream); Sw.
WriteLine (data); Cstream.
Close (); Byte[] result = Mstream.
ToArray (); Mstream.
Close ();
return convert.tobase64string (Result); public string Decrypt (string data) {TripleDESCryptoServiceProvider TripleDes =
New TripleDESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream (convert.frombase64string (data));
CryptoStream cstream = new CryptoStream (Mstream, Tripledes.createdecryptor (Rgbkey, Rgbiv), cryptostreammode.read);
StreamReader sr = new StreamReader (cstream); string result = Sr.
ReadLine (); Sr.
Close (); Cstream.
Close (); Mstream.
Close ();
return result; } #endregion}}
TripleDES encryption algorithm