This commit is contained in:
ecpvint 2024-08-15 18:43:04 +08:00
commit 0a1addb2db
28 changed files with 1553 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/build
/release

119
app.iml Normal file
View File

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":app" />
</configuration>
</facet>
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<afterSyncTasks>
<task>generateDebugSources</task>
</afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/debug" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/test/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTestDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/build-info" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-resources" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/prebuild" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/split-apk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/splits-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/reports" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 15 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

21
build.gradle Normal file
View File

@ -0,0 +1,21 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
defaultConfig {
applicationId "net.droidtech.ncmdecoder"
minSdkVersion 15
versionCode 3
versionName "1.0.6-beta"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

21
proguard-rules.pro vendored Normal file
View File

@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

7
readme.md Normal file
View File

@ -0,0 +1,7 @@
## 影沐纪念项目
#### NCM解密
<p>解密网易音乐下载后的NCM加密</p>
<p>安卓项目</p>

View File

@ -0,0 +1,26 @@
package net.droidtech.ncmdecoder;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("net.droidtech.ncmdecoder", appContext.getPackageName());
}
}

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.droidtech.ncmdecoder">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".NCMDecoderUI"
android:label="@string/app_name"
android:theme="@android:style/Theme.Holo.Light.Dialog">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file"/>
<data android:scheme="content"/>
<data android:host="*" android:mimeType="*/*" android:pathPattern=".*\\.ncm"/>
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -0,0 +1,119 @@
package net.droidtech.io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
public class DroidFile extends File{
private static final long serialVersionUID = 7311643678032690177L;
private File file=null;
public DroidFile(String path){
super(path);
file=new File(path);
}
public DataInputStream getInputStream(){
try {
if(file.exists()&&!file.isDirectory()){
DataInputStream in=new DataInputStream(new FileInputStream(file));
return in;
}else{
return null;
}
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
public DataOutputStream getOutputStream(){
try {
if(file.exists()&&!file.isDirectory()){
DataOutputStream out=new DataOutputStream(new FileOutputStream(file));
return out;
}else{
return null;
}
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
public void copyTo(File target){
DroidFile file=new DroidFile(target.getAbsolutePath());
if(file.createNewFile()){
StreamWriter writer=new StreamWriter();
writer.writeStream(this.getInputStream(),file);
}
}
@Override
public boolean createNewFile(){
try {
File parentDir=new File(file.getParent());
if(!parentDir.exists()){
parentDir.mkdirs();
}
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return file.exists();
}
@Override
public boolean mkdir(){
file.mkdirs();
return file.exists();
}
public String getMD5String(){
byte[] md5data=getMD5Bytes();
if(md5data!=null){
return new BigInteger(1,md5data).toString(16);
}else{
return null;
}
}
public byte[] getMD5Bytes(){
DataInputStream in=this.getInputStream();
if(in==null){
return null;
}
try {
byte[] buffer=new byte[4096];
MessageDigest md5=MessageDigest.getInstance("MD5");
while(true){
int count=in.read(buffer);
if(count!=-1){
md5.update(buffer,0,count);
}else{
in.close();
break;
}
}
return md5.digest();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public DroidFile getChildFile(String name){
if(!this.isDirectory()){
return null;
}
return new File(this.getAbsolutePath()+File.separator+name).exists()?new DroidFile(this.getAbsolutePath()+File.separator+name):null;
}
}

View File

@ -0,0 +1,44 @@
package net.droidtech.io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class StreamReader {
public String readStream(InputStream in,String encoding){
try {
return new String(readStream(in),encoding);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Undefined encoding:"+encoding);
}
}
public byte[] readStream(InputStream in){
if(in==null){
return null;
}
ByteArrayOutputStream data=new ByteArrayOutputStream();
byte[] buffer=new byte[4096];
try{
while(true){
int count=in.read(buffer);
if(count!=-1){
data.write(buffer,0,count);
}else{
in.close();
data.close();
break;
}
}
return data.toByteArray();
}catch(IOException e){
return null;
}
}
}

View File

@ -0,0 +1,48 @@
package net.droidtech.io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamWriter {
public void writeStream(InputStream in,File outFile){
writeStream(in,outFile,false);
}
public void writeStream(InputStream in,File outFile,boolean append){
byte[] buffer=new byte[4096];
if(in==null||outFile==null){
return;
}
try{
DataOutputStream out=new DataOutputStream(new FileOutputStream(outFile,append));
DataInputStream dataInput=new DataInputStream(in);
while(true){
int count=dataInput.read(buffer);
if(count!=-1){
out.write(buffer,0,count);
}else{
dataInput.close();
out.close();
break;
}
}
}catch(IOException e){
throw new IllegalStateException(e.getMessage());
}
}
}

View File

@ -0,0 +1,189 @@
package net.droidtech.ncmdecoder;
import android.os.Handler;
import android.os.Message;
import net.droidtech.io.DroidFile;
import net.droidtech.ncmtypes.NCMApicBlock;
import net.droidtech.ncmtypes.NCMCrc32Block;
import net.droidtech.ncmtypes.NCMKeyBlock;
import net.droidtech.ncmtypes.NCMMetaInfoBlock;
import net.droidtech.ncmtypes.NCMTypeHeader;
import net.droidtech.utils.Formatter;
import net.droidtech.utils.RC4;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.DataInputStream;
import java.io.DataOutputStream;
/**
* Created by root on 2020/4/9.
*/
public class NCMDecoderThread extends Thread {
private Handler handler;
private String filePath;
private NCMDecoderUI ui;
public void setHandler(Handler handler){
this.handler=handler;
}
public void attachUI(NCMDecoderUI ui){
this.ui=ui;
}
public void setSourceNCMFile(String filePath){
this.filePath=filePath;
}
private void writeLog(String log){
this.writeLog(log,0);
}
private void writeLog(String log, int level){
Message msg=handler.obtainMessage();
msg.obj=log;
if(level!=0){
msg.what=level;
}
msg.sendToTarget();
}
@Override
public void run(){
DroidFile ncmfile=new DroidFile(filePath);
this.writeLog(ui.getStringById(R.string.target_file)+filePath);
if(!ncmfile.exists()||!ncmfile.canRead()||ncmfile.isDirectory()){
this.writeLog(ui.getStringById(R.string.access_denied), NCMDecoderUI.LogHandler.RESULT_FAILED);
return;
}
DataInputStream ncmstream=ncmfile.getInputStream();
NCMTypeHeader hdr=new NCMTypeHeader(ncmstream);
this.writeLog(ui.getStringById(R.string.is_ncm_file)+(hdr.isNcmHeader()?ui.getStringById(R.string.yes):ui.getStringById(R.string.no)));
if(!hdr.isNcmHeader()){
this.writeLog(ui.getStringById(R.string.not_an_ncm_file), NCMDecoderUI.LogHandler.RESULT_FAILED);
return;
}
NCMKeyBlock keyblk=new NCMKeyBlock(ncmstream);
this.writeLog(ui.getStringById(R.string.can_decrypt)+(keyblk.isSupportedKeyBlock()?ui.getStringById(R.string.yes):ui.getStringById(R.string.no)));
if(!keyblk.isSupportedKeyBlock()){
this.writeLog(ui.getStringById(R.string.unsupported_ncm_file), NCMDecoderUI.LogHandler.RESULT_FAILED);
return;
}
byte[] sbox= RC4.getSbox(keyblk.getRC4Key());
NCMMetaInfoBlock metainfo=new NCMMetaInfoBlock(ncmstream);
//System.out.println("歌曲元数据: "+metainfo.getJsonMetaInfo());
NCMCrc32Block crc32=new NCMCrc32Block(ncmstream);
this.writeLog(ui.getStringById(R.string.crc32_check_result)+ui.getStringById(R.string.unimplemented));
NCMApicBlock pic=new NCMApicBlock(ncmstream);
try{
byte[] buffer=new byte[4096];
JSONObject json=new JSONObject(metainfo.getJsonMetaInfo());
String songName=Formatter.removeIllegalCharacters(json.getString("musicName"));
String artist=Formatter.removeIllegalCharacters(new JSONArray(new JSONArray(json.getString("artist")).getString(0)).getString(0));
String format=json.getString("format");
if(pic.getPictureData()!=null){
this.writeLog(ui.getStringById(R.string.image_size)+pic.getPictureData().length+ui.getStringById(R.string.size_byte));
DroidFile outImg=new DroidFile(ncmfile.getParentFile().getAbsolutePath()+DroidFile.separator+songName+"_"+artist+"_pic.jpg");
outImg.createNewFile();
this.writeLog(ui.getStringById(R.string.writing_file)+outImg.getAbsolutePath());
DataOutputStream imgOutStream=outImg.getOutputStream();
imgOutStream.write(pic.getPictureData());
imgOutStream.close();
}else{
this.writeLog(ui.getStringById(R.string.apic_not_found));
}
DroidFile outFile=new DroidFile(ncmfile.getParentFile().getAbsolutePath()+DroidFile.separator+songName+"_"+artist+"."+format);
outFile.createNewFile();
DataOutputStream out=outFile.getOutputStream();
this.writeLog(ui.getStringById(R.string.writing_file)+outFile.getAbsolutePath());
while(true){
int length=ncmstream.read(buffer);
if(length==-1){
break;
}
RC4.decrypt_rc4_prga(buffer,length,sbox);
out.write(buffer,0, length);
}
ncmstream.close();
out.close();
this.writeLog(ui.getStringById(R.string.decryption_success), NCMDecoderUI.LogHandler.RESULT_SUCCESS);
}catch(Exception e){
this.writeLog(e.getMessage(), NCMDecoderUI.LogHandler.RESULT_FAILED);
}
}
}

View File

@ -0,0 +1,97 @@
package net.droidtech.ncmdecoder;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NCMDecoderUI extends Activity {
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ncmdecoder_ui);
Intent intent = getIntent();
Uri uri = intent.getData();
if(uri==null){
this.finish();
return;
}
String uriPath=uri.getPath();
if(uriPath.startsWith("/root")){
uriPath=uriPath.replace("/root","");
}
this.setTitle(R.string.window_title);
this.setFinishOnTouchOutside(false);
this.layout=(LinearLayout)this.findViewById(R.id.logger);
NCMDecoderThread thread=new NCMDecoderThread();
thread.setHandler(new LogHandler());
thread.setSourceNCMFile(uriPath);
thread.attachUI(this);
thread.start();
}
public String getStringById(int id){
return this.getResources().getString(id);
}
public class LogHandler extends Handler{
public static final int RESULT_FAILED=0xFF;
public static final int RESULT_SUCCESS=0xFC;
private ViewGroup.LayoutParams textViewLayout=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT);
@Override
public void handleMessage(Message msg){
TextView message=new TextView(NCMDecoderUI.this);
message.setText((CharSequence)msg.obj);
message.setLayoutParams(textViewLayout);
if(msg.what==LogHandler.RESULT_FAILED){
message.setTextColor(NCMDecoderUI.this.getResources().getColor(android.R.color.holo_red_light));
}
if(msg.what==LogHandler.RESULT_SUCCESS){
message.setTextColor(NCMDecoderUI.this.getResources().getColor(android.R.color.holo_green_light));
}
NCMDecoderUI.this.layout.addView(message);
}
}
}

View File

@ -0,0 +1,58 @@
package net.droidtech.ncmtypes;
import java.io.DataInputStream;
import net.droidtech.utils.ByteMatcher;
import net.droidtech.utils.IntegerUtils;
public class NCMApicBlock {
public static final int APIC_BLOCK_START=1;
private byte[] mPic;
public NCMApicBlock(DataInputStream in){
try{
if(in.read()!=NCMApicBlock.APIC_BLOCK_START){
return;
}
byte[] magic_len=new byte[4];
in.readFully(magic_len);
byte[] pic_len_data=new byte[4];
in.readFully(pic_len_data);
if(!ByteMatcher.isMatch(magic_len, pic_len_data)){
return;
}
int pic_len=IntegerUtils.changeEndian(IntegerUtils.byteToInteger(pic_len_data));
this.mPic=new byte[pic_len];
in.readFully(this.mPic);
}catch(Exception e){
e.printStackTrace();
}
}
public byte[] getPictureData(){
return this.mPic;
}
}

View File

@ -0,0 +1,51 @@
package net.droidtech.ncmtypes;
import java.io.DataInputStream;
import net.droidtech.utils.IntegerUtils;
public class NCMCrc32Block {
//unimplemented
private boolean mIsCrc32Matched;
private byte[] mCrc32;
public NCMCrc32Block(DataInputStream in){
try{
byte[] crc32=new byte[4];
in.readFully(crc32);
this.mCrc32=crc32;
this.mIsCrc32Matched=true;
}catch(Exception e){
e.printStackTrace();
}
}
public boolean isCrc32Matched(){
return this.mIsCrc32Matched;
}
public byte[] getCrc32Byte(){
return this.mCrc32;
}
public int getCrc32Int(){
return IntegerUtils.byteToInteger(this.mCrc32);
}
}

View File

@ -0,0 +1,88 @@
package net.droidtech.ncmtypes;
import java.io.DataInputStream;
import net.droidtech.utils.AESUtils;
import net.droidtech.utils.ByteMatcher;
import net.droidtech.utils.HexStringUtils;
import net.droidtech.utils.IntegerUtils;
public class NCMKeyBlock{
public static final byte[] KEY_BLOCK_MAGIC={0x01,0x70};
public static final int XOR_MAIN_KEY=0x64;
public static final String AES_MAIN_KEY="687A4852416D736F356B496E62617857";
public static final String KEY_PADDING="neteasecloudmusic";
private byte[] mKeyData=null;
private boolean mIsSupportedKeyBlock=false;
public NCMKeyBlock(DataInputStream in){
try{
byte[] magic=new byte[NCMKeyBlock.KEY_BLOCK_MAGIC.length];
in.readFully(magic);
/*if(!ByteMatcher.isMatch(NCMKeyBlock.KEY_BLOCK_MAGIC,magic)){
return;
}*/
byte[] key_length_data=new byte[4];
in.readFully(key_length_data);
int key_length=IntegerUtils.changeEndian(IntegerUtils.byteToInteger(key_length_data));
byte[] key=new byte[key_length];
in.readFully(key);
for(int i=0;i<key.length;i++){
key[i]=(byte)(key[i]^NCMKeyBlock.XOR_MAIN_KEY);
}
AESUtils aes=new AESUtils();
aes.setKey(HexStringUtils.parseHexString(NCMKeyBlock.AES_MAIN_KEY));
aes.setMode(AESUtils.MODE_DECRYPT);
this.mKeyData=aes.decrypt(key);
String key_str=new String(mKeyData);
key_str=key_str.replace(NCMKeyBlock.KEY_PADDING, "").trim();
key_length=key_length-(NCMKeyBlock.KEY_PADDING.length());
this.mKeyData=key_str.getBytes();
this.mIsSupportedKeyBlock=true;
}catch(Exception e){
e.printStackTrace();
return;
}
}
public boolean isSupportedKeyBlock(){
return this.mIsSupportedKeyBlock;
}
public byte[] getRC4Key(){
return this.mKeyData;
}
}

View File

@ -0,0 +1,67 @@
package net.droidtech.ncmtypes;
import java.io.DataInputStream;
import net.droidtech.utils.AESUtils;
import net.droidtech.utils.Base64;
import net.droidtech.utils.HexStringUtils;
import net.droidtech.utils.IntegerUtils;
public class NCMMetaInfoBlock {
public static final int XOR_METAINFO_KEY=0x63;
public static final String AES_METAINFO_KEY="2331346C6A6B5F215C5D2630553C2728";
public static final String JSON_START_MAGIC="music:";
private String mJsonMetaInfo=null;
public NCMMetaInfoBlock(DataInputStream in){
try{
byte[] length_data=new byte[4];
in.readFully(length_data);
int blk_length=IntegerUtils.changeEndian(IntegerUtils.byteToInteger(length_data));
byte[] blk=new byte[blk_length];
in.readFully(blk);
for(int i=0;i<blk.length;i++){
blk[i]=(byte)(blk[i]^NCMMetaInfoBlock.XOR_METAINFO_KEY);
}
String metaInfo=new String(blk);
metaInfo=metaInfo.substring(metaInfo.indexOf(":")+1,metaInfo.length()).trim();
byte[] rawMetaInfo=Base64.decode(metaInfo);
AESUtils aes=new AESUtils();
aes.setKey(HexStringUtils.parseHexString(NCMMetaInfoBlock.AES_METAINFO_KEY));
aes.setMode(AESUtils.MODE_DECRYPT);
this.mJsonMetaInfo=new String(aes.decrypt(rawMetaInfo),"UTF-8").replace(NCMMetaInfoBlock.JSON_START_MAGIC,"").trim();
}catch(Exception e){
e.printStackTrace();
}
}
public String getJsonMetaInfo(){
return this.mJsonMetaInfo;
}
}

View File

@ -0,0 +1,48 @@
package net.droidtech.ncmtypes;
import java.io.DataInputStream;
public class NCMTypeHeader {
public static final String NCM_HEADER="CTENFDAM";
public static final int NCM_HEADER_LENGTH=8;
private boolean mIsNcmFmt=false;
public NCMTypeHeader(DataInputStream in){
try{
byte[] ncm_hdr=new byte[NCMTypeHeader.NCM_HEADER.length()];
in.readFully(ncm_hdr);
if(ncm_hdr.length>NCMTypeHeader.NCM_HEADER_LENGTH){
return;
}
if(new String(ncm_hdr).equals(NCMTypeHeader.NCM_HEADER)){
this.mIsNcmFmt=true;
}
}catch(Exception e){
e.printStackTrace();
return;
}
}
public boolean isNcmHeader(){
return this.mIsNcmFmt;
}
}

View File

@ -0,0 +1,83 @@
package net.droidtech.utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class AESUtils {
public static final int MODE_ENCRYPT=Cipher.ENCRYPT_MODE;
public static final int MODE_DECRYPT=Cipher.DECRYPT_MODE;
public static final int DEFAULT_KEY_LENGTH=128;
private SecretKeySpec keySpec;
private Cipher cipher;
public void setKey(byte[] key){
if(key==null){
return;
}
if(key.length<16){
return;
}
this.keySpec = new SecretKeySpec(key, "AES");
}
public static byte[] generateRandomKey(){
return AESUtils.generateRandomKey(256);
}
public static byte[] generateRandomKey(int length){
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(length);
return keyGenerator.generateKey().getEncoded();
} catch (Exception e) {
return null;
}
}
public void setMode(int mode){
try {
this.cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(mode, this.keySpec);
} catch (Exception e) {
throw new java.lang.RuntimeException(e.getMessage());
}
}
public byte[] encrypt(byte[] data){
try {
byte[] result=this.cipher.doFinal(data);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public byte[] decrypt(byte[] data){
try {
byte[] result=this.cipher.doFinal(data);
return result;
} catch (Exception e) {
return null;
}
}
}

View File

@ -0,0 +1,116 @@
package net.droidtech.utils;
import java.io.ByteArrayOutputStream;
public class Base64 {
private Base64(){
}
public static String encode(byte[] data){
char[] tbl = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};
StringBuilder buffer = new StringBuilder();
int pad = 0;
for (int i = 0; i < data.length; i += 3) {
int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF;
if (i + 1 < data.length) {
b |= (data[i+1] & 0xFF) << 8;
} else {
pad++;
}
if (i + 2 < data.length) {
b |= (data[i+2] & 0xFF);
} else {
pad++;
}
for (int j = 0; j < 4 - pad; j++) {
int c = (b & 0xFC0000) >> 18;
buffer.append(tbl[c]);
b <<= 6;
}
}
for (int j = 0; j < pad; j++) {
buffer.append("=");
}
return buffer.toString();
}
public static byte[] decode(String data){
int[] tbl = {
-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, -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, -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, -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
};
byte[] bytes = data.getBytes();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; ) {
int b = 0;
if (tbl[bytes[i]] != -1) {
b = (tbl[bytes[i]] & 0xFF) << 18;
}
// skip unknown characters
else {
i++;
continue;
}
int num = 0;
if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) {
b = b | ((tbl[bytes[i+1]] & 0xFF) << 12);
num++;
}
if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) {
b = b | ((tbl[bytes[i+2]] & 0xFF) << 6);
num++;
}
if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) {
b = b | (tbl[bytes[i+3]] & 0xFF);
num++;
}
while (num > 0) {
int c = (b & 0xFF0000) >> 16;
buffer.write((char)c);
b <<= 8;
num--;
}
i += 4;
}
return buffer.toByteArray();
}
}

View File

@ -0,0 +1,33 @@
package net.droidtech.utils;
public class ByteMatcher {
private ByteMatcher(){
}
public static boolean isMatch(byte[] src, byte[] dst){
if(src.length!=dst.length){
return false;
}
for(int i=0;i<src.length;i++){
if(src[i]!=dst[i]){
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,32 @@
package net.droidtech.utils;
public class Formatter {
private Formatter(){
}
public static final String[] mIllegalChars={"\"","/","\\","*","?","<",">",":","|"};
public static final String[] mReplaceTable={"\'","-","-","×","","","","","#"};
public static String removeIllegalCharacters(String arg){
String result=arg;
for(int i=0;i<Formatter.mIllegalChars.length;i++){
if(result.contains(Formatter.mIllegalChars[i])){
result=result.replace(Formatter.mIllegalChars[i], Formatter.mReplaceTable[i]);
}
}
return result;
}
}

View File

@ -0,0 +1,87 @@
package net.droidtech.utils;
import java.io.ByteArrayOutputStream;
public class HexStringUtils {
private HexStringUtils(){
}
public static byte[] parseHexString(String arg){
return parseHexString(arg,null);
}
public static byte[] parseHexString(String arg, String separator){
ByteArrayOutputStream result=new ByteArrayOutputStream();
if(separator==null){
if(arg.length()%2!=0){
throw new IllegalArgumentException("Unsupported string format or incompletely");
}
char[] tempStack=new char[2];
int stackSize=0;
for(int i=0;i<arg.length();i++){
if(stackSize<2){
tempStack[stackSize]=arg.charAt(i);
stackSize++;
}
if(stackSize==2){
result.write(Integer.parseInt(new String(tempStack), 16));
stackSize=0;
}
}
}
if(separator!=null){
String[] items=arg.split(separator);
for(int i=0;i<items.length;i++){
if(items[i].length()>2||items[i].length()<=0){
throw new IllegalArgumentException("Unsupported string format");
}
result.write(Integer.parseInt(items[i],16));
}
}
return result.toByteArray();
}
public static String byteToHexString(byte[] value){
return byteToHexString(value,null);
}
public static String byteToHexString(byte[] value, String separator){
StringBuffer result=new StringBuffer();
for(int i=0;i<value.length;i++){
if((value[i]&0xFF)<=0xF){
result.append("0");
}
result.append(Integer.toHexString((value[i])&0xFF));
if(separator!=null&&i<(value.length-1)){
result.append(separator);
}
}
return result.toString();
}
}

View File

@ -0,0 +1,51 @@
package net.droidtech.utils;
public class IntegerUtils{
private IntegerUtils(){
}
public static int changeEndian(int var){
int result=0;
for(int i=3;i>=0;i--){
result=result+(IntegerUtils.getUnsignedByte((byte)(var>>(i*8)))<<(24-i*8));
}
return result;
}
public static int byteToInteger(byte[] data){
int result=0;
if(data.length>4){
return 0;
}
for(int i=0;i<4;i++){
result=result+(IntegerUtils.getUnsignedByte(data[i])<<(24-i*8));
}
return result;
}
public static int getUnsignedByte(byte var){
return var&0xFF;
}
}

View File

@ -0,0 +1,59 @@
package net.droidtech.utils;
public class RC4 {
private RC4(){
}
public static byte[] getSbox(byte[] key){
byte[] box=new byte[256];
for(int i=0;i<box.length;i++){
box[i]=(byte)i;
}
int c=0;
int last_byte=0;
int key_offset=0;
for(int i=0;i<box.length;i++){
int swap=box[i];
c=(swap+last_byte+key[key_offset])&0xff;
key_offset++;
if(key_offset>=key.length){
key_offset=0;
}
box[i]=box[c];
box[c]=(byte)swap;
last_byte=c;
}
return box;
}
public static void decrypt_rc4_prga(byte[] data, int data_length, byte[] sbox){
for(int i=1;i<(data_length==0?(data.length+1):(data_length+1));i++){
int j = i & 0xff;
data[i-1] ^= sbox[(sbox[j] + sbox[(sbox[j]+j)&0xff])&0xff];
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/logger">
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,21 @@
<resources>
<string name="app_name">NCM 解码插件</string>
<string name="window_title">日志</string>
<string name="target_file">要解密的文件: </string>
<string name="access_denied">解密失败: 目标文件有误,请确保插件有权访问该存储空间!</string>
<string name="is_ncm_file">是否为NCM格式: </string>
<string name="not_an_ncm_file">解密失败: 不是NCM格式</string>
<string name="can_decrypt">能否解密: </string>
<string name="unsupported_ncm_file">解密失败: 不支持的加密方式!</string>
<string name="crc32_check_result">CRC32检查结果: </string>
<string name="unimplemented">(此功能未实现!)</string>
<string name="image_size">封面图像大小: </string>
<string name="size_byte">字节</string>
<string name="writing_file">正在写入: </string>
<string name="apic_not_found">未找到封面图片!</string>
<string name="decryption_success">解密完成!</string>
<string name="yes"></string>
<string name="no"></string>
</resources>

View File

@ -0,0 +1,17 @@
package net.droidtech.ncmdecoder;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}