Skip to content

Instantly share code, notes, and snippets.

@Tithen-Firion
Last active April 6, 2024 04:26
Show Gist options
  • Save Tithen-Firion/244bdc4e2ab29ee7aaff4e34ed40b93d to your computer and use it in GitHub Desktop.
Save Tithen-Firion/244bdc4e2ab29ee7aaff4e34ed40b93d to your computer and use it in GitHub Desktop.
WeTV - cKey function

Here's the code (in JavaScript and Python) to make the cKey for downloading stuff from WeTV. This works for encryption version 8.1. Both versions accept the same arguments, first 5 (vid, tm, appVer, guid and platform) are the same as in the call to https://play.wetv.vip/getvinfo. url is optional for JavaScript but required for Python - that's the URL of the page you're on. userAgent, referer, navCodeName, navName and navPlatform are optional, check the code for default values.

Usage

JavaScript

makeCKey(/* arguments here */);

Python

ckey = CKey()
ckey.make(
  # arguments here
)

If you use this code in your project - let me know and credit me. ;)

var makeCKey = function() {
function encodeText(text) {
const len = text.length, arr = [];
for(let i=0; i<len; i++) arr[i >>> 2] |= (255 & text.charCodeAt(i)) << 24 - i % 4 * 8;
return [arr, len];
}
function decodeText(arr, len) {
const textArray = [];
for (let i=0; i<len; i++) {
textArray.push((arr[i >>> 2] >>> 24 - i % 4 * 8 & 255).toString(16).padStart(2, '0'))
}
return textArray.join('')
}
function shorten(text) {
return (text || '').substr(0, 48);
}
function calculateHash(text) {
let result = 0;
const len = text.length;
for(let i=0; i<len; i++) result = (result << 5) - result + text.charCodeAt(i);
return result;
}
function padText(text) {
const padLength = 16 - text.length % 16;
for(let i = 0; i < padLength; ++i) text += String.fromCharCode(padLength);
return text;
}
function encrypt(arr) {
const len = arr.length;
for(let i=0; i<len; i+= 4) mainAlgorithm(arr, i);
}
// init start
const encryptionArrays = [[1332468387, -1641050960, 2136896045, -1629555948, 1399201960, -850809832, -1307058635, 751381793, -1933648423, 1106735553, -203378700, -550927659, 766369351, 1817882502, -1615200142, 1083409063, -104955314, -1780208184, 173944250, 1254993693, 1422337688, -1054667952, -880990486, -2119136777, -1822404972, 1380140484, -1723964626, 412019417, -890799303, -1734066435, 26893779, 420787978, -1337058067, 686432784, 695238595, 811911369, -391724567, -1068702727, -381903814, -648522509, -1266234148, 1959407397, -1644776673, 1152313324]];
(function() {
var d = [], f = [], g = [], h = [], j = [], o = [];
for(let i=0; i<256; i++) o[i] = i < 128 ? i << 1 : i << 1 ^ 283;
var t = 0,
u = 0;
for(let i=0; i<256; i++) {
var v = u ^ u << 1 ^ u << 2 ^ u << 3 ^ u << 4;
v = v >>> 8 ^ 255 & v ^ 99;
d[t] = v;
var x = o[t],
z = o[o[x]],
A = 257 * o[v] ^ 16843008 * v;
f[t] = A << 24 | A >>> 8
g[t] = A << 16 | A >>> 16
h[t] = A << 8 | A >>> 24
j[t] = A
t ? (t = x ^ o[o[o[z ^ x]]], u ^= o[o[u]]) : t = u = 1
}
encryptionArrays.push(f);
encryptionArrays.push(g);
encryptionArrays.push(h);
encryptionArrays.push(j);
encryptionArrays.push(d);
})();
// init end
function mainAlgorithm(a, b) {
const [c, d, e, f, g, h] = encryptionArrays;
let xorArr;
if(b === 0) {
xorArr = [22039283, 1457920463, 776125350, -1941999367];
}
else {
xorArr = a.slice(b - 4, b);
}
for (let i = 0; i < 4; i++) a[b + i] ^= xorArr[i];
for (var i = 10, j = a[b] ^ c[0], k = a[b + 1] ^ c[1], l = a[b + 2] ^ c[2], m = a[b + 3] ^ c[3], n = 4, o = 1; o < i; o++) {
var q = d[j >>> 24] ^ e[k >>> 16 & 255] ^ f[l >>> 8 & 255] ^ g[255 & m] ^ c[n++],
s = d[k >>> 24] ^ e[l >>> 16 & 255] ^ f[m >>> 8 & 255] ^ g[255 & j] ^ c[n++],
t = d[l >>> 24] ^ e[m >>> 16 & 255] ^ f[j >>> 8 & 255] ^ g[255 & k] ^ c[n++];
m = d[m >>> 24] ^ e[j >>> 16 & 255] ^ f[k >>> 8 & 255] ^ g[255 & l] ^ c[n++];
j = q;
k = s;
l = t;
}
q = (h[j >>> 24] << 24 | h[k >>> 16 & 255] << 16 | h[l >>> 8 & 255] << 8 | h[255 & m]) ^ c[n++];
s = (h[k >>> 24] << 24 | h[l >>> 16 & 255] << 16 | h[m >>> 8 & 255] << 8 | h[255 & j]) ^ c[n++];
t = (h[l >>> 24] << 24 | h[m >>> 16 & 255] << 16 | h[j >>> 8 & 255] << 8 | h[255 & k]) ^ c[n++];
m = (h[m >>> 24] << 24 | h[j >>> 16 & 255] << 16 | h[k >>> 8 & 255] << 8 | h[255 & l]) ^ c[n++];
a[b] = q;
a[b + 1] = s;
a[b + 2] = t;
a[b + 3] = m;
}
return function(vid, tm, appVer, guid, platform, url, userAgent, referer, navCodeName, navName, navPlatform) {
if(typeof url === 'undefined') url = window.document.URL;
if(typeof userAgent === 'undefined') userAgent = window.navigator.userAgent;
if(typeof referer === 'undefined') referer = window.document.referrer;
if(typeof navCodeName === 'undefined') navCodeName = window.navigator.appCodeName;
if(typeof navName === 'undefined') navName = window.navigator.appName;
if(typeof navPlatform === 'undefined') navPlatform = window.navigator.platform;
const textParts = [
'', vid, tm, 'mg3c3b04ba', appVer, guid, platform, shorten(url),
shorten(userAgent.toLowerCase()), shorten(referer),
navCodeName, navName, navPlatform, '00', ''
];
textParts.splice(1, 0, calculateHash(textParts.join('|')));
const text = padText(textParts.join('|'));
const [arr, len] = encodeText(text);
encrypt(arr);
return decodeText(arr, len).toUpperCase();
}
}();
from ctypes import c_int32
class CKey:
def __init__(self):
self.encryption_arrays = [[
1332468387, -1641050960, 2136896045, -1629555948,
1399201960, -850809832, -1307058635, 751381793,
-1933648423, 1106735553, -203378700, -550927659,
766369351, 1817882502, -1615200142, 1083409063,
-104955314, -1780208184, 173944250, 1254993693,
1422337688, -1054667952, -880990486, -2119136777,
-1822404972, 1380140484, -1723964626, 412019417,
-890799303, -1734066435, 26893779, 420787978,
-1337058067, 686432784, 695238595, 811911369,
-391724567, -1068702727, -381903814, -648522509,
-1266234148, 1959407397, -1644776673, 1152313324]]
d = [None] * 256
f = d.copy()
g = d.copy()
h = d.copy()
j = d.copy()
o = d.copy()
for i in range(256):
o[i] = i << 1 if i < 128 else i << 1 ^ 283
t = 0
u = 0
for i in range(256):
v = u ^ u << 1 ^ u << 2 ^ u << 3 ^ u << 4
v = CKey.rshift(v, 8) ^ 255 & v ^ 99
d[t] = v
x = o[t]
z = o[o[x]]
A = CKey.int32(257 * o[v] ^ 16843008 * v)
f[t] = CKey.int32(A << 24 | CKey.rshift(A, 8))
g[t] = CKey.int32(A << 16 | CKey.rshift(A, 16))
h[t] = CKey.int32(A << 8 | CKey.rshift(A, 24))
j[t] = A
if t == 0:
t = 1
u = 1
else:
t = x ^ o[o[o[z ^ x]]]
u ^= o[o[u]]
self.encryption_arrays.append(f)
self.encryption_arrays.append(g)
self.encryption_arrays.append(h)
self.encryption_arrays.append(j)
self.encryption_arrays.append(d)
@staticmethod
def rshift(val, n):
return (val & 0xFFFFFFFF) >> n
@staticmethod
def int32(val):
return c_int32(val).value
@staticmethod
def encode_text(text):
length = len(text)
arr = [0] * (length // 4)
for i in range(length):
arr[i // 4] |= (255 & ord(text[i])) << 24 - i % 4 * 8
return arr, length
@staticmethod
def decode_text(arr, length):
text_array = []
for i in range(length):
text_array.append('{:02x}'.format(
CKey.rshift(arr[i // 4], 24 - i % 4 * 8) & 255))
return ''.join(text_array)
@staticmethod
def calculate_hash(text):
result = 0
for char in text:
result = CKey.int32(result << 5) - result + ord(char)
return str(result)
@staticmethod
def pad_text(text):
pad_length = 16 - len(text) % 16
return text + chr(pad_length) * pad_length
def encrypt(self, arr):
for i in range(0, len(arr), 4):
self.main_algorithm(arr, i)
def main_algorithm(self, a, b):
c, d, e, f, g, h = self.encryption_arrays
if b == 0:
xor_arr = [22039283, 1457920463, 776125350, -1941999367]
else:
xor_arr = a[b - 4: b]
for i, val in enumerate(xor_arr):
a[b + i] ^= val
j = a[b] ^ c[0]
k = a[b + 1] ^ c[1]
l = a[b + 2] ^ c[2]
m = a[b + 3] ^ c[3]
n = 4
for _ in range(9):
q = (d[CKey.rshift(j, 24)] ^ e[CKey.rshift(k, 16) & 255]
^ f[CKey.rshift(l, 8) & 255] ^ g[255 & m] ^ c[n])
s = (d[CKey.rshift(k, 24)] ^ e[CKey.rshift(l, 16) & 255]
^ f[CKey.rshift(m, 8) & 255] ^ g[255 & j] ^ c[n + 1])
t = (d[CKey.rshift(l, 24)] ^ e[CKey.rshift(m, 16) & 255]
^ f[CKey.rshift(j, 8) & 255] ^ g[255 & k] ^ c[n + 2])
m = (d[CKey.rshift(m, 24)] ^ e[CKey.rshift(j, 16) & 255]
^ f[CKey.rshift(k, 8) & 255] ^ g[255 & l] ^ c[n + 3])
j = q
k = s
l = t
n += 4
q = CKey.int32(h[CKey.rshift(j, 24)] << 24
| h[CKey.rshift(k, 16) & 255] << 16
| h[CKey.rshift(l, 8) & 255] << 8
| h[255 & m]) ^ c[n]
s = CKey.int32(h[CKey.rshift(k, 24)] << 24
| h[CKey.rshift(l, 16) & 255] << 16
| h[CKey.rshift(m, 8) & 255] << 8
| h[255 & j]) ^ c[n + 1]
t = CKey.int32(h[CKey.rshift(l, 24)] << 24
| h[CKey.rshift(m, 16) & 255] << 16
| h[CKey.rshift(j, 8) & 255] << 8
| h[255 & k]) ^ c[n + 2]
m = CKey.int32(h[CKey.rshift(m, 24)] << 24
| h[CKey.rshift(j, 16) & 255] << 16
| h[CKey.rshift(k, 8) & 255] << 8
| h[255 & l]) ^ c[n + 3]
a[b] = q
a[b + 1] = s
a[b + 2] = t
a[b + 3] = m
def make(self, vid, tm, app_ver, guid, platform, url,
# user_agent is shortened anyway
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.',
referer='', nav_code_name='Mozilla',
nav_name='Netscape', nav_platform='Win32'):
text_parts = [
'', vid, tm, 'mg3c3b04ba', app_ver, guid, platform,
url[:48], user_agent[:48].lower(), referer[:48],
nav_code_name, nav_name, nav_platform, '00', ''
]
text_parts.insert(1, CKey.calculate_hash('|'.join(text_parts)))
text = CKey.pad_text('|'.join(text_parts))
[arr, length] = CKey.encode_text(text)
self.encrypt(arr)
return CKey.decode_text(arr, length).upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment