public static string GenerateOpTicket(OperatorTick op, string secret) { if ((secret == null) || (secret.Length == 0)) { throw new ArgumentNullException("Invalid Argument"); } try { string OpInfoStr = op.GenerateOpInfo(); byte[] Key = TicketTool.GetKey(secret); byte[] OpInfoByte = Encoding.Unicode.GetBytes(OpInfoStr); MemoryStream MSTicket = new MemoryStream(); MSTicket.Write(TicketTool.ConvertLength(OpInfoByte.Length), 0, 2); MSTicket.Write(OpInfoByte, 0, OpInfoByte.Length); MSTicket.Write(TicketTool.ConvertLength(Key.Length), 0, 2); MSTicket.Write(Key, 0, Key.Length); byte[] OpTicketCryptByte = TicketTool.Crypt(MSTicket.ToArray(), Key); string OpTicketCryptStr = Encoding.ASCII.GetString(TicketTool.Base64Encode(OpTicketCryptByte)); return(OpTicketCryptStr); } catch (Exception e) { throw e; } }
public static OperatorTick GetFromString(string opinfo, string secret) { try { byte[] Key = TicketTool.GetKey(secret); byte[] OpLoginTicketCry = TicketTool.Base64Decode(Encoding.ASCII.GetBytes(opinfo)); byte[] OpLoginTicketDec = TicketTool.Decrypt(OpLoginTicketCry, Key); byte[] OpLoginInfoByte; if (OpLoginTicketDec.Length < 2) { throw new System.Exception("Invalid ticket"); } int OpNextLen = TicketTool.GetPart(OpLoginTicketDec, 0, out OpLoginInfoByte); byte[] TickKey; if (OpLoginTicketDec.Length < OpNextLen + 2) { throw new System.Exception("Invalid ticket"); } OpNextLen = TicketTool.GetPart(OpLoginTicketDec, OpNextLen, out TickKey); if (!TicketTool.CompareByteArrays(Key, TickKey)) { throw new System.Exception("Invalid ticket"); } string OpLoginInfo = Encoding.Unicode.GetString(OpLoginInfoByte); string[] arra = OpLoginInfo.Split('\n'); if (arra.Length == 13) { OperatorTick op = new OperatorTick(Convert.ToInt32(arra[0]), arra[1], Convert.ToInt32(arra[2]), arra[3], arra[4]); op.Desp1 = arra[5]; op.Desp2 = arra[6]; op.Desp3 = arra[7]; op.Desp4 = arra[8]; op.Desp5 = arra[9]; op.Desp6 = arra[10]; op.Desp7 = arra[11]; op.Desp8 = arra[12]; return(op); } else { throw new Exception("转换OperatorTick参数个数不对!"); } } catch { return(null); } }