淘先锋技术网

首页 1 2 3 4 5 6 7
1. /framework/base/encrypt 新建文件夹。
encrypt 目录结构:

        |--java

           |--android

             --encrypt

                --Encryption.java

           |--encryptlib.xml

           |--Android.mk

        |--jni

           |--android_encrypt_Encryption.cpp

            --EncryptManager.cpp

           |--Android.mk

 

Encryption.java:

 

package android.encrypt;

public class Encryption {
    
    static {
        System.loadLibrary("encrypt_jni");
    }
    
    static native boolean verifyPasswordNative(char[] src, char[] des);
    
    public static boolean  verifyPassword(char[] src, char[] des) {
        return verifyPasswordNative(src, des);   
        
        
    }
}

 

encryptlib.xml:

<?xml version="1.0" encoding="UTF-8"?>
<permissions>
	<library     
		name="android.encrypt"
		file="/system/framework/encrypt.jar" />
</permissions>


 

Android.MK:

LOCAL_PATH:= $(call my-dir)
#MAKE_JAR
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := encrypt
include $(BUILD_JAVA_LIBRARY)

#MAKE_XML
include $(CLEAR_VARS)
LOCAL_MODULE := encryptlib.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions

LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)


------------------------------------------------------------------------------------------------------------------

android_encrypt_Encryption.cpp:

/*
 * Copyright 2008, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Encryption"

#include "utils/Log.h"
#include <assert.h>

#include "jni.h"
#include "JNIHelp.h"
#include <encrypt/EncryptManager.h>
#include "android_runtime/AndroidRuntime.h"

using namespace android;

static jboolean
verify_Password(JNIEnv *env, jclass clazz, jcharArray src, jcharArray des)
{
	EncryptManager& encrypt(EncryptManager::getInstance());
	jchar* srcValues = env->GetCharArrayElements(src, NULL);
	jchar* desValues = env->GetCharArrayElements(des, NULL);
	return encrypt.verify_Password(srcValues, desValues);
}

static JNINativeMethod gMethods[] = {
    {"verifyPasswordNative",  "([C[C)Z",     (void*)verify_Password },
};

//}; // namespace android

int register_android_encrypt_Encryption(JNIEnv *env)
{
    return AndroidRuntime::registerNativeMethods(env,
    		"android/encrypt/Encryption", gMethods, NELEM(gMethods));
}

jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("ERROR: GetEnv failed\n");
        goto bail;
    }
    assert(env != NULL);

    if (register_android_encrypt_Encryption(env) < 0) {
        LOGE("ERROR: Encryption native registration failed\n");
        goto bail;
    }

    /* success -- return valid version number */
    result = JNI_VERSION_1_4;

bail:
    return result;
}



EncryptManager.cpp:(EncryptManager.h 放在/framework/include/encrypt)

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Sensors"

#include <stdint.h>
#include <sys/types.h>

#include <utils/Errors.h>
#include "utils/Log.h"
#include <utils/RefBase.h>
#include <utils/Singleton.h>

#include <encrypt/EncryptManager.h>

// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------

ANDROID_SINGLETON_STATIC_INSTANCE(EncryptManager);

EncryptManager::EncryptManager() {}

EncryptManager::~EncryptManager() {}


bool EncryptManager::verify_Password(const void* src, const void* des)
{	
	return true;
}

// ----------------------------------------------------------------------------
}; // namespace android


Android.mk:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
	android_encrypt_Encryption.cpp \
	EncryptManager.cpp

LOCAL_SHARED_LIBRARIES := \
	libandroid_runtime \
	libcutils \
	libutils \
	libbinder \

LOCAL_MODULE:= libencrypt_jni

LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_TAGS := optional


ifeq ($(TARGET_SIMULATOR),true)
    LOCAL_LDLIBS += -lpthread
endif

include $(BUILD_SHARED_LIBRARY)


编译后,最总会生成:

/system/etc/permissions/encryptlib.xml

/system/framework/encrypt.jar

/system/lib/libencrypt_jni.so

 

andriod 源码应用时,要在/build/core/pathmap.mk 添加新建的文件路径 /encrypt

FRAMEWORKS_BASE_SUBDIRS := \
 $(addsuffix /java, \
     core \
     graphics \
     location \
     media \
     opengl \
     sax \
     telephony \
     wifi \
     vpn \
     keystore \
     voip \
     encrypt \
  )

 

android 应用程序应用时,在AndroidManifest.xml中添加

<uses-library android:name="android.encrypt" />

指向对应的jar包