分析优酷2016.04最新视频加密算法


算法整理
 
// 使用到的加密算法//
function rc4(a, b) {
	for (var c, d = [], e = 0, f = "", g = 0; 256 > g; g++)
		d[g] = g;
	for (g = 0; 256 > g; g++)
		e = (e + d[g] + a.charCodeAt(g % a.length)) % 256, c = d[g], d[g] = d[e], d[e] = c;
	g = 0,
	e = 0;
	for (var h = 0; h < b.length; h++)
		g = (g + 1) % 256, e = (e + d[g]) % 256, c = d[g], d[g] = d[e], d[e] = c, f += String.fromCharCode(b.charCodeAt(h)^d[(d[g] + d[e]) % 256]);
	return f
}
function translate(a, b) {
	for (var c = [], d = 0; d < a.length; d++) {
		var e = 0;
		e = a[d] >= "a" && a[d] <= "z" ? a[d].charCodeAt(0) - "a".charCodeAt(0) : a[d] - "0" + 26;
		for (var f = 0; 36 > f; f++)
			if (b[f] == e) {
				e = f;
				break
			}
		e > 25 ? c[d] = e - 26 : c[d] = String.fromCharCode(e + 97)
	}
	return c.join("")
}
function decode64(a) {
	if (!a)
		return "";
	a = a.toString();
	var b,
	c,
	d,
	e,
	f,
	g,
	h,
	i = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
	for (g = a.length, f = 0, h = ""; g > f; ) {
		do
			b = i[255 & a.charCodeAt(f++)];
		while (g > f && -1 == b);
		if (-1 == b)
			break;
		do
			c = i[255 & a.charCodeAt(f++)];
		while (g > f && -1 == c);
		if (-1 == c)
			break;
		h += String.fromCharCode(b << 2 | (48 & c) >> 4);
		do {
			if (d = 255 & a.charCodeAt(f++), 61 == d)
				return h;
			d = i[d]
		} while (g > f && -1 == d);
		if (-1 == d)
			break;
		h += String.fromCharCode((15 & c) << 4 | (60 & d) >> 2);
		do {
			if (e = 255 & a.charCodeAt(f++), 61 == e)
				return h;
			e = i[e]
		} while (g > f && -1 == e);
		if (-1 == e)
			break;
		h += String.fromCharCode((3 & d) << 6 | e)
	}
	return h
}
function encode64 (a) {
	if (!a)
		return "";
	a = a.toString();
	var b,
	c,
	d,
	e,
	f,
	g,
	h = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	for (d = a.length, c = 0, b = ""; d > c; ) {
		if (e = 255 & a.charCodeAt(c++), c == d) {
			b += h.charAt(e >> 2),
			b += h.charAt((3 & e) << 4),
			b += "==";
			break
		}
		if (f = a.charCodeAt(c++), c == d) {
			b += h.charAt(e >> 2),
			b += h.charAt((3 & e) << 4 | (240 & f) >> 4),
			b += h.charAt((15 & f) << 2),
			b += "=";
			break
		}
		g = a.charCodeAt(c++),
		b += h.charAt(e >> 2),
		b += h.charAt((3 & e) << 4 | (240 & f) >> 4),
		b += h.charAt((15 & f) << 2 | (192 & g) >> 6),
		b += h.charAt(63 & g)
	}
	return b
}
//YKP.userCache.sid & YKP.userCache.token 算法//
YK.mk.a3 = "b4et"
YKP.userCache.a1 = 4
d = [19, 1, 4, 7, 30, 14, 28, 8, 24, 17, 6, 35, 34, 16, 9, 10, 13, 22, 32, 29, 31, 21, 18, 3, 2, 23, 25, 27, 11, 20, 5, 15, 12, 0, 33, 26]
b.security.encrypt_string = json数据中的security.encrypt_string值
e = rc4(translate(YK.mk.a3 + "o0b" + YKP.userCache.a1, d).toString(), decode64(b.security.encrypt_string))
YKP.userCache.sid = e.split("_")[0]
YKP.userCache.token = e.split("_")[1]


//n为视频分段数量//
n获取方式
n = v.toString(16)
v = json数据表 stream 数组通过下面的m值确定格式再在对应格式内部获取segs数组的成员数

//m为视频格式//
m获取方式
先从json数据表 stream 数组中获取 stream_type的值
通过stream_type的值查询字典l
l = {
	flv : "flv",
	mp4 : "mp4",
	hd2 : "flv",
	mp4hd : "mp4",
	mp4hd2 : "mp4",
	"3gphd" : "mp4",
	"3gp" : "flv",
	flvhd : "flv"
}
得到m值

//e为视频段主要数据//
o.stream_fileid 获取方式
json数据表 stream 数组通过下面的m值确定格式再在对应格式内部获取stream_fileid值
e  = getFileId(o.stream_fileid, v)
getFileId : function (a, b) {
	if (null == a || "" == a)
		return "";
	var c = "",
	d = a.slice(0, 8),
	e = b.toString(16);
	1 == e.length && (e = "0" + e),
	e = e.toUpperCase();
	var f = a.slice(10, a.length);
	return c = d + e + f
}
//p值//
1.新算法
p = h.segs[v].key;
h = c.stream[a]

2.旧算法
p = h.key2 + h.key1

//k值//
先从json数据表 stream 数组中获取 stream_type的值d
通过stream_type的值查询字典j
k = j[d]
j = {
	flv : 0,
	flvhd : 0,
	mp4 : 1,
	hd2 : 2,
	"3gphd" : 1,
	"3gp" : 0
}

//o值//
o = h.segs[b].total_milliseconds_video / 1e3

//YK.v.data.security.ip//
json数据表 security中获取ip

//t值//
YK.mk.a4 = "boa4"
YKP.userCache.a2 = 1
s = d
t = encodeURIComponent(encode64(rc4(translate(YK.mk.a4 + "poz" + YKP.userCache.a2, s).toString(), YKP.userCache.sid + "_" + e + "_" + YKP.userCache.token)));

不写完全,懂的人自然懂 ,我也避免一些不必要的麻烦微笑