Unity3d 調用 C++ 函數 實現加密防破解 (Android向)

來源:互聯網
上載者:User

標籤:unity   android   組合語言   反編譯   破解   

因為Unity 採用C# 作為主要語言,代碼編譯之後作為DLL存在與執行檔案中,這就給我們帶來很大的一個問題,反編譯非常容易。


如何反編譯Unity遊戲的代碼:

Unity打包產生的安裝包,我們隨便下載一個遊戲,解壓APK,來到

assets\bin\Data\Managed

這個目錄。

Assembly-CSharp.dllAssembly-CSharp-firstpass.dll

你在遊戲中編寫的代碼就存放在這兩個dll中。

把dll拖放到MonoDevelop中,稍等片刻,就能看到dll中的代碼。


如果有一些比較重要的代碼不想讓別人看到,那就用C++來編寫,C++編譯成so檔案,反編譯之後只能成為組合語言,無疑加大了破解難度(當然不能百分百防破解,彙編大牛很多的)


我們先建立一個檔案夾,在裡面建立一個jni檔案夾,建立一個c檔案,內容如下:

#include<string.h>  #include<jni.h>  int Share(){return 1234561;}

就這麼一個函數吧,供C#調用。


然後建立一個Android.mk檔案,這是NDK編譯SO需要的一個mk檔案,在裡面指定了如何編譯。

LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_MODULE := Share  LOCAL_SRC_FILES := Share.c  include $(BUILD_SHARED_LIBRARY)

然後建立一個Application.mk檔案,指定編譯平台以及其它的依賴。

APP_ABI :=armeabi-v7aAPP_PLATFORM:=android-8APP_STL:=gnustl_staticAPP_CFLAGS += -Wno-error=format-security




然後在jni檔案夾中執行命令:

ndk-build

就會編譯出來so檔案,存放在上一級的lib中。



建立一個Unity3d的工程,然後編寫代碼,調用so中的的函數。

using UnityEngine;using System.Collections;using System.IO;using System.Runtime.InteropServices;using System;public class test : MonoBehaviour {[DllImport("Share")]private static extern int Share();// Use this for initializationvoid Start () {Debug.Log("Shared = "+Share());}// Update is called once per framevoid Update () {}}

注意 如果要調用SO中的函數一定要按照Unity指定的規則來編寫:

[DllImport("Share")]private static extern int Share();


在Unity工程中建立目錄

Plugins\Android

拷貝SO檔案到這裡



然後我們匯出APK安裝測試



Demo工程下載:

http://download.csdn.net/detail/cp790621656/8430985



Unity3d 調用 C++ 函數 實現加密防破解 (Android向)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.