Public Sealed Class CMYCRYPT {public static Byte [] md5encrypt (string s) {return (new md5cryptoserviceProvider ()). Computehash ((new utf8encoding ()). GetBytes (s));}
#Region encryption process /// /// encryption process /// summary> public string myencoding (string ss) {symmetricalgorithm symm = new rijndaelManaged (); symm.generateKey (); symm.generateiv (); BYTE [] key = symm.key; byte [] iv = symm.iv;
Byte [] BS = _GetBytesfromunicode (ss); byte [] be2 = _combinatebytes (BMD5, Key, IV, BE); return system.convert.tobase64string (be2);} #ENDREGION encryption process
#Region decryption process /// /// decryption process /// summary> /// encrypted string param> /// If decrypt and data test succeeds, the return source string, otherwise return null returns> public string MyDecoding (string ss) {byte [] bs = System.Convert.FromBase64String (ss); if (bs2 == null) return null; else return _getUnicodeFromBytes ( BS);} #ENDREGION decryption process
#Region Map a Unicode string to memory /// /// Map a Unicode string to memory /// summary> /// Unicode encoding Source string param> /// Memory array returns> private byte [] _getbytesfromunicode (string ss) {system.io.MemoryStream ms = new memorystream (System.Text.Encoding.Unicode. Gettes (ss)); binaryreader br2 = new binaryreader (); br2.close (); ms.close (); return buf;} #endregion map a Unicode string to memory
#Region Rijndael encrypted /// /// Rijndael encrypted /// summary> /// key param> /// Initialization vector array param> /// source data param> /// returns> private bote [] _doDesencoding (byte [] key, byte [] iv, byte [] bs) {System.IO.MemoryStream sin = new MemoryStream (bs); System.IO.MemoryStream sout = new MemoryStream (); SymmetricAlgorithm symm = new RijndaelManaged (); ICryptoTransform transform = symm.CreateEncryptor (key, iv) Cryptostream cstream = New CryptostReam (Sout, Transform, CryptostreamMode.write);
BinaryReader Br = New BinaryReader (sin); CStream.write (Br.Readbytes ((int) sin.length), 0, (int) sin.LEngth); cstream.flushfinalblock (); cstractAm.close ();
Sout.Close (); sin.close ();
Return Sout.toArray ();} #ENDREGION RIJNDAEL encryption
#REGION Rijndael decryption (reverse the order and encryption process) /// /// rijndael decryption (reverse order and encryption process) /// summary> /// (combination) Add Card param> /// Decryption data (if there is a data verification error, return null) returns> private bote [] _dodesdecoding (byte [] bs) {system.io.MemoryStream SIN = New MemoryStream (BS); BinaryReader Br = New BinaryReader (SIN);
INT NOFF = 0; Byte [] BMD5 = Br.Readbytes (16); Noff = 16; int nc = br.readint32 (); Noff = 4; Byte [] bkey = br.readbytes (NOFF = NC; Nc = br.readint32 (); Noff = 4; Byte [] BIV = Br.Readbytes (NC); NOFF = nc; Byte [] BSS = Br.Readbytes (BS.LENGTH-NOFF);
Br.close (); sin.close ();
SIN = New MemoryStream (BSS);
SymmetricAlgorithm symm = new RijndaelManaged (); ICryptoTransform transform = symm.CreateDecryptor (bKey, bIV); CryptoStream cstream = new CryptoStream (sin, transform, CryptoStreamMode.Read); br = new BinaryReader (cstream); byte [] buf = br. ReadBytes ((int) sin.length; br.close (); sin.close ();
// Data Verification BYTE [] BMD52 = (New MD5CryptoserviceProvider ()). Computehash (BUF); BOOL B = false; for (int i = 0, nlen = BMD5.LENGTH; I
Return buf;} #endregion rijndael decryption (reverse the order and encryption process)
#Region converts data into strings /// /// converts data into strings /// summary> /// Original data param> ////// Converted string returns> private string _getunicodefrombytes (byte [] bs) {return new string (system.text.encoding.unicode.getchars (bs));} #ENDREGON converts data into strings
#REGON data combination /// /// data combination /// summary> /// param> /// Param> /// param> /// param> /// returns> private bote [] _combinatebytes (byte " ] MD5, BYTE [] Key, Byte [] iv, byte [] be) {MemoryStream ms = new memorystream (); binarywriter bw = new binarywriter (ms); bw.write (key.length); bw.write BW.WRITE (iv.length); bw.write (iv); bw.write (); ms.close (); ms.close (); return ms.toarray () } #Endregion data combination
}