日期:2014-05-20 浏览次数:20784 次
public string Encrypt(string strTobeEnCrypted, string strKEY, string strIV) { if (strTobeEnCrypted == "") return ""; try { byte[] pKEY = HexStringToByteArray(strKEY); byte[] pIV = HexStringToByteArray(strIV); byte[] Encrypted; if (Encrypt(pKEY, pIV, ConvertStringToByteArray(strTobeEnCrypted), out Encrypted)) { return ToBase64String(Encrypted); } else { return ""; } } catch { } return ""; } public byte[] HexStringToByteArray(string s) { Byte[] buf = new byte[s.Length / 2]; for (int i = 0; i < buf.Length; i++) { buf[i] = (byte)(chr2hex(s.Substring(i * 2, 1)) * 0x10 + chr2hex(s.Substring(i * 2 + 1, 1))); } return buf; } private bool Encrypt(byte[] KEY, byte[] IV, byte[] TobeEncrypted, out byte[] Encrypted) { Encrypted = null; try { byte[] tmpiv ={ 0, 1, 2, 3, 4, 5, 6, 7 }; for (int ii = 0; ii < 8; ii++) { tmpiv[ii] = IV[ii]; } byte[] tmpkey ={ 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 }; for (int ii = 0; ii < 24; ii++) { tmpkey[ii] = KEY[ii]; } private System.Security.Cryptography.TripleDESCryptoServiceProvider des; ICryptoTransform tridesencrypt = des.CreateEncryptor(tmpkey, tmpiv); Encrypted = tridesencrypt.TransformFinalBlock(TobeEncrypted, 0, TobeEncrypted.Length); des.Clear(); } catch (Exception e) { return false; } return true; }