Last active
May 15, 2019 02:09
-
-
Save cmd64/5521296d6db1e64c21e7e87eb9eb865c to your computer and use it in GitHub Desktop.
Youtube Decrypt Chipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace CM.Youtube.Library | |
{ | |
public class YTDecrypt | |
{ | |
private static string GetPlayerJS(string videoUrl) | |
{ | |
var result = ""; | |
using (var client = new WebClient()) | |
{ | |
var baseJs = client.DownloadString(videoUrl); | |
result = Regex.Match(baseJs, "<script.+?src=[\"'](.+?)base.js[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value; | |
result = string.Format("https://www.youtube.com{0}base.js", result); | |
} | |
return result; | |
} | |
public static string GetDecrytFunction(string videoUrl, string signature) | |
{ | |
var playerUrl = GetPlayerJS(videoUrl); | |
using (var client = new WebClient()) | |
{ | |
var baseJs = client.DownloadString(playerUrl); | |
// Find the name of the function that handles deciphering | |
var fnname = Regex.Match(baseJs, @"yt\.akamaized\.net.*encodeURIComponent\((\w+)").Groups[1].Value; | |
var _argnamefnbodyresult = Regex.Match(baseJs, fnname + @"=function\((.+?)\){(.+?)}"); | |
var helpername = Regex.Match(_argnamefnbodyresult.Groups[2].Value, @";(.+?)\..+?\(").Groups[1].Value; | |
var helperresult = Regex.Match(baseJs, "var " + helpername + "={[\\s\\S]+?};"); | |
var result = helperresult.Groups[0].Value; | |
MatchCollection matches = Regex.Matches(result, @"[A-Za-z0-9]+:function\s*([A-z0-9]+)?\s*\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)\s*\{(?:[^}{]+|\{(?:[^}{]+|\{[^}{]*\})*\})*\}"); | |
var funcs = _argnamefnbodyresult.Groups[2].Value.Split(";"); | |
var sign = signature.ToCharArray(); | |
foreach (var func in funcs) | |
{ | |
foreach (Match group in matches) | |
{ | |
if (group.Value.Contains("reverse")) | |
{ | |
var test = Regex.Match(group.Value, "^(.*?):").Groups[1].Value; | |
if (func.Contains(test)) | |
{ | |
var pos = Regex.Match(func, @"(\d)").Groups[1].Value; | |
sign = ReverseFunction(sign); | |
} | |
} | |
else if (group.Value.Contains("splice")) | |
{ | |
var test = Regex.Match(group.Value, "^(.*?):").Groups[1].Value; | |
if (func.Contains(test)) | |
{ | |
var pos = Regex.Match(func, @"(\d)").Groups[1].Value; | |
sign = SliceFunction(sign, Convert.ToInt32(pos)); | |
} | |
} | |
else | |
{ | |
var test = Regex.Match(group.Value, "^(.*?):").Groups[1].Value; | |
if (func.Contains(test)) | |
{ | |
var pos = Regex.Match(func, @"(\d+)").Groups[1].Value; | |
sign = SwapFunction(sign, Convert.ToInt32(pos)); | |
} | |
} | |
} | |
} | |
return string.Join("", sign); | |
} | |
} | |
private static char[] SliceFunction(char[] a, int b) | |
{ | |
var nArray = new ArraySegment<char>(a); | |
return nArray.Slice(b).ToArray(); | |
} | |
private static char[] SwapFunction(char[] a, int b) | |
{ | |
var c = a[0]; | |
a[0] = a[b % a.Length]; | |
a[b % a.Length] = c; | |
return a; | |
} | |
private static char[] ReverseFunction(char[] a) | |
{ | |
Array.Reverse(a); | |
return a; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment