/// <summary> /// AuthCode /// </summary> /// <param name="sourceStr">源字符串</param> /// <param name="operation">操作</param> /// <param name="keyStr">KEY</param> /// <param name="expiry">过期时间</param> /// <returns></returns> private static string authCode(string sourceStr, AuthCodeMethod operation, string keyStr, int expiry = 0) { if (string.IsNullOrEmpty(sourceStr)) return ""; const int ckeyLength = 4; var source = Encode.GetBytes(sourceStr); var key = Encode.GetBytes(keyStr); key = Md5(key); var keya = Md5(SubBytes(key, 0, 0x10)); var keyb = Md5(SubBytes(key, 0x10, 0x10)); var keyc = getKeyc(ckeyLength, operation, source); var cryptkey = AddBytes(keya, Md5(AddBytes(keya, keyc))); var keyLength = cryptkey.Length; source = getCKey(expiry, ckeyLength, operation, source, keyb); var sourceLength = source.Length; var box = getBox(); var rndkey = getRndkey(cryptkey, keyLength); rndBox(box, rndkey); var result = getResult(source, box, sourceLength); return operation == AuthCodeMethod.Decode ? check(keyb, result) : BytesToString(keyc) + Convert.ToBase64String(result).Replace("=", ""); }
/// <summary> /// AuthCode /// </summary> /// <param name="sourceStr">源字符串</param> /// <param name="operation">操作</param> /// <param name="keyStr">KEY</param> /// <param name="expiry">过期时间</param> /// <returns></returns> private static string authCode(string sourceStr, AuthCodeMethod operation, string keyStr, int expiry = 0) { const int ckeyLength = 4; byte[] source = Encode.GetBytes(sourceStr); byte[] key = Encode.GetBytes(keyStr); key = Md5(key); byte[] keya = Md5(SubBytes(key, 0, 0x10)); byte[] keyb = Md5(SubBytes(key, 0x10, 0x10)); byte[] keyc = getKeyc(ckeyLength, operation, source); byte[] cryptkey = AddBytes(keya, Md5(AddBytes(keya, keyc))); int keyLength = cryptkey.Length; source = getCKey(expiry, ckeyLength, operation, source, keyb); int sourceLength = source.Length; int[] box = getBox(); int[] rndkey = getRndkey(cryptkey, keyLength); rndBox(box, rndkey); byte[] result = getResult(source, box, sourceLength); return(operation == AuthCodeMethod.Decode ? check(keyb, result) : BytesToString(keyc) + Convert.ToBase64String(result).Replace("=", "")); }
/// <summary> /// 得到keyc /// </summary> /// <param name="ckeyLength">ckey长度</param> /// <param name="operation">操作</param> /// <param name="source">数据源</param> /// <returns></returns> private static byte[] getKeyc(int ckeyLength, AuthCodeMethod operation, byte[] source) { return((ckeyLength > 0) ? ((operation == AuthCodeMethod.Decode) ? SubBytes(source, 0, ckeyLength) : RandomBytes(ckeyLength)) : new byte[0]); }
public string SendLogingRequest(BCClientModel clientModel, Action <BCClientModel> callback) { string HtmlResult = ""; AuthCodeMethod.GetAccessTokenAsync(clientModel, model => { Url = new Uri(clientModel.url); parameters = model.url + "?response_type=" + model.response_type + "&client_id=" + model.client_id + "&redirect_uri=" + model.redirect_uri + "&scope=openid" + model.scope + "offline_access&state=" + clientModel.state + "&code_challenge=" + model.code_challenge + "&code_challenge_method=" + model.code_challenge_method + "&nonce=" + model.nonce + "&response_mode=form_post"; using (WebClient wc = new WebClient()) { HtmlResult = wc.DownloadString(parameters); } callback(model); }); return(parameters); }
/// <summary> /// 得到CKey /// </summary> /// <param name="expiry">过期时间</param> /// <param name="ckeyLength">CKey长度</param> /// <param name="operation">操作</param> /// <param name="source">数据源</param> /// <param name="keyb">Keyb</param> /// <returns></returns> private byte[] getCKey(int expiry, int ckeyLength, AuthCodeMethod operation, byte[] source, byte[] keyb) { if (operation == AuthCodeMethod.Decode) { while (source.Length % 4 != 0) { source = AddBytes(source, Encode.GetBytes("=")); } source = Convert.FromBase64String(BytesToString(SubBytes(source, ckeyLength))); } else { source = AddBytes( (expiry != 0 ? Encode.GetBytes((expiry + PhpTimeNow()).ToString()) : Encode.GetBytes("0000000000")), SubBytes(Md5(AddBytes(source, keyb)), 0, 0x10), source); } return(source); }
/// <summary> /// 得到keyc /// </summary> /// <param name="ckeyLength">ckey长度</param> /// <param name="operation">操作</param> /// <param name="source">数据源</param> /// <returns></returns> private static byte[] getKeyc(int ckeyLength, AuthCodeMethod operation, byte[] source) { return (ckeyLength > 0) ? ((operation == AuthCodeMethod.Decode) ? SubBytes(source, 0, ckeyLength) : RandomBytes(ckeyLength)) : new byte[0]; }
/// <summary> /// 得到CKey /// </summary> /// <param name="expiry">过期时间</param> /// <param name="ckeyLength">CKey长度</param> /// <param name="operation">操作</param> /// <param name="source">数据源</param> /// <param name="keyb">Keyb</param> /// <returns></returns> private static byte[] getCKey(int expiry, int ckeyLength, AuthCodeMethod operation, byte[] source, byte[] keyb) { if (operation == AuthCodeMethod.Decode) { while (source.Length % 4 != 0) { source = AddBytes(source, Encode.GetBytes("=")); } source = Convert.FromBase64String(BytesToString(SubBytes(source, ckeyLength))); } else { source = AddBytes( (expiry != 0 ? Encode.GetBytes((expiry + PhpTimeNow()).ToString()) : Encode.GetBytes("0000000000")), SubBytes(Md5(AddBytes(source, keyb)), 0, 0x10), source); } return source; }
private static string AuthCode(string sourceStr, AuthCodeMethod operation, string keyStr, int expiry = 0) { const int ckeyLength = 4; var source = Charset.GetBytes(sourceStr); var key = Charset.GetBytes(keyStr); key = Md5(key); var keya = Md5(SubBytes(key, 0, 0x10)); var keyb = Md5(SubBytes(key, 0x10, 0x10)); var keyc = (operation == AuthCodeMethod.Decode) ? SubBytes(source, 0, ckeyLength) : RandomBytes(ckeyLength); var cryptkey = AddBytes(keya, Md5(AddBytes(keya, keyc))); var keyLength = cryptkey.Length; if (operation == AuthCodeMethod.Decode) { while (source.Length % 4 != 0) { source = AddBytes(source, Charset.GetBytes("=")); } source = Convert.FromBase64String(BytesToString(SubBytes(source, ckeyLength))); } else { source = AddBytes(expiry != 0 ? Charset.GetBytes((expiry + DateTime.Now.ToTimestamp()).ToString()) : Charset.GetBytes("0000000000"), SubBytes(Md5(AddBytes(source, keyb)), 0, 0x10), source); } var sourceLength = source.Length; var box = new int[256]; for (var k = 0; k < 256; k++) { box[k] = k; } var rndkey = new int[256]; for (var i = 0; i < 256; i++) { rndkey[i] = cryptkey[i % keyLength]; } for (int j = 0, i = 0; i < 256; i++) { j = (j + box[i] + rndkey[i]) % 256; var tmp = box[i]; box[i] = box[j]; box[j] = tmp; } var result = new byte[sourceLength]; for (int a = 0, j = 0, i = 0; i < sourceLength; i++) { a = (a + 1) % 256; j = (j + box[a]) % 256; var tmp = box[a]; box[a] = box[j]; box[j] = tmp; result[i] = (byte)(source[i] ^ (box[(box[a] + box[j]) % 256])); } if (operation == AuthCodeMethod.Decode) { var time = long.Parse(BytesToString(SubBytes(result, 0, 10))); if ((time == 0 || time - DateTime.Now.ToTimestamp() > 0) && BytesToString(SubBytes(result, 10, 16)) == BytesToString(SubBytes(Md5(AddBytes(SubBytes(result, 26), keyb)), 0, 16))) { return(BytesToString(SubBytes(result, 26))); } return(string.Empty); } return($"{BytesToString(keyc)}{Convert.ToBase64String(result).Replace("=", string.Empty)}"); }