|
@ -2,10 +2,9 @@ const CryptoJS = require("crypto-js"); |
|
|
const ID3Writer = require("browser-id3-writer"); |
|
|
const ID3Writer = require("browser-id3-writer"); |
|
|
const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); |
|
|
const CORE_KEY = CryptoJS.enc.Hex.parse("687a4852416d736f356b496e62617857"); |
|
|
const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); |
|
|
const META_KEY = CryptoJS.enc.Hex.parse("2331346C6A6B5F215C5D2630553C2728"); |
|
|
import {AudioMimeType, DetectAudioExt, GetArrayBuffer} from "./util" |
|
|
import {AudioMimeType, DetectAudioExt, GetArrayBuffer, GetFileInfo} from "./util" |
|
|
|
|
|
|
|
|
export async function Decrypt(file) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function Decrypt(file, raw_filename, raw_ext) { |
|
|
const fileBuffer = await GetArrayBuffer(file); |
|
|
const fileBuffer = await GetArrayBuffer(file); |
|
|
const dataView = new DataView(fileBuffer); |
|
|
const dataView = new DataView(fileBuffer); |
|
|
|
|
|
|
|
@ -13,32 +12,34 @@ export async function Decrypt(file) { |
|
|
dataView.getUint32(4, true) !== 0x4d414446) |
|
|
dataView.getUint32(4, true) !== 0x4d414446) |
|
|
return {status: false, message: "此ncm文件已损坏"}; |
|
|
return {status: false, message: "此ncm文件已损坏"}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const keyDataObj = getKeyData(dataView, fileBuffer, 10); |
|
|
const keyDataObj = getKeyData(dataView, fileBuffer, 10); |
|
|
const keyBox = getKeyBox(keyDataObj.data); |
|
|
const keyBox = getKeyBox(keyDataObj.data); |
|
|
|
|
|
|
|
|
const musicMetaObj = getMetaData(dataView, fileBuffer, keyDataObj.offset); |
|
|
const musicMetaObj = getMetaData(dataView, fileBuffer, keyDataObj.offset); |
|
|
const musicMeta = musicMetaObj.data; |
|
|
const musicMeta = musicMetaObj.data; |
|
|
|
|
|
|
|
|
let audioOffset = musicMetaObj.offset + dataView.getUint32(musicMetaObj.offset + 5, true) + 13; |
|
|
let audioOffset = musicMetaObj.offset + dataView.getUint32(musicMetaObj.offset + 5, true) + 13; |
|
|
let audioData = new Uint8Array(fileBuffer, audioOffset); |
|
|
let audioData = new Uint8Array(fileBuffer, audioOffset); |
|
|
|
|
|
|
|
|
for (let cur = 0; cur < audioData.length; ++cur) audioData[cur] ^= keyBox[cur & 0xff]; |
|
|
for (let cur = 0; cur < audioData.length; ++cur) audioData[cur] ^= keyBox[cur & 0xff]; |
|
|
|
|
|
|
|
|
if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); |
|
|
|
|
|
|
|
|
|
|
|
const mime = AudioMimeType[musicMeta.format]; |
|
|
if (musicMeta.album === undefined) musicMeta.album = ""; |
|
|
|
|
|
|
|
|
const artists = []; |
|
|
const artists = []; |
|
|
musicMeta.artist.forEach(arr => artists.push(arr[0])); |
|
|
if (!!musicMeta.artist) musicMeta.artist.forEach(arr => artists.push(arr[0])); |
|
|
|
|
|
const info = GetFileInfo(artists.join(" & "), musicMeta.musicName, raw_filename); |
|
|
|
|
|
if (artists.length === 0) artists.push(info.artist); |
|
|
|
|
|
|
|
|
|
|
|
if (musicMeta.format === undefined) musicMeta.format = DetectAudioExt(audioData, "mp3"); |
|
|
if (musicMeta.format === "mp3") |
|
|
if (musicMeta.format === "mp3") |
|
|
audioData = await writeID3(audioData, artists, musicMeta.musicName, musicMeta.album, musicMeta.albumPic); |
|
|
audioData = await writeID3(audioData, artists, info.title, musicMeta.album, musicMeta.albumPic); |
|
|
|
|
|
|
|
|
|
|
|
const mime = AudioMimeType[musicMeta.format]; |
|
|
const musicData = new Blob([audioData], {type: mime}); |
|
|
const musicData = new Blob([audioData], {type: mime}); |
|
|
return { |
|
|
return { |
|
|
status: true, |
|
|
status: true, |
|
|
title: musicMeta.musicName, |
|
|
title: info.title, |
|
|
artist: artists.join(" & "), |
|
|
artist: info.artist, |
|
|
ext: musicMeta.format, |
|
|
ext: musicMeta.format, |
|
|
album: musicMeta.album, |
|
|
album: musicMeta.album, |
|
|
picture: musicMeta.albumPic, |
|
|
picture: musicMeta.albumPic, |
|
@ -129,9 +130,7 @@ function getKeyBox(keyData) { |
|
|
function getMetaData(dataView, fileBuffer, offset) { |
|
|
function getMetaData(dataView, fileBuffer, offset) { |
|
|
const metaDataLen = dataView.getUint32(offset, true); |
|
|
const metaDataLen = dataView.getUint32(offset, true); |
|
|
offset += 4; |
|
|
offset += 4; |
|
|
if (metaDataLen === 0) { |
|
|
if (metaDataLen === 0) return {data: {}, offset: offset}; |
|
|
return {}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const cipherText = new Uint8Array(fileBuffer, offset, metaDataLen).map( |
|
|
const cipherText = new Uint8Array(fileBuffer, offset, metaDataLen).map( |
|
|
data => data ^ 0x63 |
|
|
data => data ^ 0x63 |
|
|