//解密 public static string Decrypt(string input) { if (string.IsNullOrEmpty(input)) { return(null); } byte[] b = Convert.FromBase64String(input); RC4Helper.RC4(b, _Key); return(new UTF8Encoding().GetString(b)); }
// 加密 public static string Encrypt(string input) { if (string.IsNullOrEmpty(input)) { return(null); } byte[] b = new UTF8Encoding().GetBytes(input); RC4Helper.RC4(b, _Key); return(Convert.ToBase64String(b)); }
public byte[] GetEncryptBytes(string keySHA1, string keyData) { string userToken = string.Format("U:{0}:{1}:{2}:T", this.UserID, this.RandomPwd, TimeUtil.NowRealTime() * 10000L); byte[] dataToken = new UTF8Encoding().GetBytes(userToken); byte[] macSHA = SHA1Helper.get_macsha1_bytes(dataToken, keySHA1); byte[] encryptToken = new byte[macSHA.Length + dataToken.Length]; DataHelper.CopyBytes(encryptToken, 0, macSHA, 0, macSHA.Length); DataHelper.CopyBytes(encryptToken, macSHA.Length, dataToken, 0, dataToken.Length); RC4Helper.RC4(encryptToken, keyData); return(encryptToken); }
/// <summary> /// 设置加密并做了完整性保护的字节流 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public int SetEncryptBytes(byte[] buffer, string keySHA1, string keyData, long maxTicks) { //先解密数据 RC4Helper.RC4(buffer, keyData); //拆分MACSHA1和DATATOKEN byte[] macSHA1 = new byte[20]; //固定的20个字节 DataHelper.CopyBytes(macSHA1, 0, buffer, 0, macSHA1.Length); byte[] dataToken = new byte[buffer.Length - 20]; DataHelper.CopyBytes(dataToken, 0, buffer, 20, dataToken.Length); //校验MACSHA1是否正确 byte[] verifyMacSHA1 = SHA1Helper.get_macsha1_bytes(dataToken, keySHA1); if (!DataHelper.CompBytes(verifyMacSHA1, macSHA1)) { return(-1); //不想同,数据被篡改过了 } if (dataToken[0] == NormalTokenHeader) { //如果相同则解析用户ID和随机密码 string strToken = new UTF8Encoding().GetString(dataToken); string[] parseTokens = strToken.Split(':'); if (parseTokens.Length != 5) //个数不同,被篡改过了 { return(-2); } //是否是自己的协议 if (parseTokens[0] != "U" || parseTokens[4] != "T") { return(-3); } //时间戳 long ticks = (long)Convert.ToUInt64(parseTokens[3]); if (TimeUtil.NowRealTime() * 10000 - ticks >= maxTicks && GameManager.GM_NoCheckTokenTimeRemainMS <= 0) //时间戳已经过期 { return(-4); } UserID = parseTokens[1]; RandomPwd = (int)Convert.ToUInt32(parseTokens[2]); } else { return(-3); } return(0); }
/// 获取加密并做了完整性保护的字节流 public byte[] GetEncryptBytes(string keySHA1, string keyData) { string userToken = string.Format("U:{0}:{1}:{2}:T", UserID, RandomPwd, DateTime.Now.Ticks); byte[] dataToken = new UTF8Encoding().GetBytes(userToken); byte[] macSHA1 = SHA1Helper.get_macsha1_bytes(dataToken, keySHA1); byte[] encryptToken = new byte[macSHA1.Length + dataToken.Length]; DataHelper.CopyBytes(encryptToken, 0, macSHA1, 0, macSHA1.Length); DataHelper.CopyBytes(encryptToken, macSHA1.Length, dataToken, 0, dataToken.Length); //加密数据 RC4Helper.RC4(encryptToken, keyData); return(encryptToken); }
public int SetEncryptBytes(byte[] buffer, string keySHA1, string keyData, long maxTicks) { RC4Helper.RC4(buffer, keyData); byte[] macSHA = new byte[20]; DataHelper.CopyBytes(macSHA, 0, buffer, 0, macSHA.Length); byte[] dataToken = new byte[buffer.Length - 20]; DataHelper.CopyBytes(dataToken, 0, buffer, 20, dataToken.Length); byte[] verifyMacSHA = SHA1Helper.get_macsha1_bytes(dataToken, keySHA1); int result; if (!DataHelper.CompBytes(verifyMacSHA, macSHA)) { result = -1; } else if (dataToken[0] == 85) { string strToken = new UTF8Encoding().GetString(dataToken); string[] parseTokens = strToken.Split(new char[] { ':' }); if (parseTokens.Length != 5) { result = -2; } else if (parseTokens[0] != "U" || parseTokens[4] != "T") { result = -3; } else { long ticks = (long)Convert.ToUInt64(parseTokens[3]); if (TimeUtil.NowRealTime() * 10000L - ticks >= maxTicks && GameManager.GM_NoCheckTokenTimeRemainMS <= 0L) { result = -4; } else { this.UserID = parseTokens[1]; this.RandomPwd = (int)Convert.ToUInt32(parseTokens[2]); result = 0; } } } else { result = -3; } return(result); }
public static string Encrypt(string input) { string result; if (string.IsNullOrEmpty(input)) { result = null; } else { byte[] b = new UTF8Encoding().GetBytes(input); RC4Helper.RC4(b, SecondPasswordRC4._Key); result = Convert.ToBase64String(b); } return(result); }