在 jni 中返回错误的 md5 哈希

Pha*_*inh 3 c hash java-native-interface android md5

我正在尝试使用原始/未触及的 md5.h 和 md5c.c(github 源文件:https : //github.com/sinhpn92/encryption-in-C/tree/master/app/ src/main/cpp)。但我的结果在任何时候都不对。当我使用三星galaxy j设备进行测试时,我的结果是正确的。但是当我使用三星galaxy s7设备进行测试时,我的结果是错误的。我的代码有什么问题?对解决这个问题有什么建议吗?感谢您的支持。

这是我的项目:https : //github.com/sinhpn92/encryption-in-C

我使用 cmake 来配置 jni lib:

cmake_minimum_required(VERSION 3.4.1)

set(MD5SOURCES
    src/main/cpp/md5.c)


add_library(native-lib
             SHARED
             src/main/cpp/native-lib.cpp
              ${MD5SOURCES})


find_library(log-lib
              log )

target_link_libraries(native-lib
                       ${log-lib} )
Run Code Online (Sandbox Code Playgroud)

这是原生库:

#include <jni.h>
#include <string>
#include "md5.h"

extern "C"
jstring
Java_test_sinhpn_md5test_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */, jstring data) {
    char *cstr = (char *) (env)->GetStringUTFChars(data, 0);

    MD5_CTX context = {0};
    MD5Init(&context);
    MD5Update(&context, (unsigned char *) cstr, strlen(cstr));
    unsigned char dest[16] = {0};
    MD5Final(dest, &context);
    env->ReleaseStringUTFChars(data, cstr);

    int i;
    char destination[32] = {0};
    for (i = 0; i < 16; i++) {
        sprintf(destination, "%s%02x", destination, dest[i]);
    }
    return env->NewStringUTF(destination);
}
Run Code Online (Sandbox Code Playgroud)

我的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "test.sinhpn.md5test"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试结果:

在此处输入图片说明

在此处输入图片说明

Pha*_*inh 5

我已经通过替换解决了我的问题:

typedef unsigned long int UINT4;在 md5.c -> 中typedef uint32_t UINT4;

我在两台设备上再次测试,它工作正常。在 64 位机器上 long int 是(通常)64 位长而不是 32