MD5 算法(Android,IOS,JNI C++,Java)

1、Android 写法

  1. /**
  2. * MD5加密算法
  3. *
  4. * @param val 需要加密的字符串
  5. * @return
  6. * @throws NoSuchAlgorithmException
  7. */
  8. public static String getMD5(String val) {
  9. try {
  10. MessageDigest bmd5 = MessageDigest.getInstance("MD5");
  11. bmd5.update(val.getBytes());
  12. int i;
  13. StringBuffer buf = new StringBuffer();
  14. byte[] b = bmd5.digest();
  15. for (int offset = 0; offset < b.length; offset++) {
  16. i = b[offset];
  17. if (i < 0)
  18. i += 256;
  19. if (i < 16)
  20. buf.append("0");
  21. buf.append(Integer.toHexString(i));
  22. }
  23. return buf.toString();
  24. } catch (NoSuchAlgorithmException e) {
  25. e.printStackTrace();
  26. }
  27. return "";
  28. }

2、Java写法

  1. /**
  2. * md5或者sha-1加密
  3. *
  4. * @param inputText
  5. * 要加密的内容
  6. * @param algorithmName
  7. * 加密算法名称:md5或者sha-1,不区分大小写
  8. * @return
  9. */
  10. private static String encrypt(String inputText, String algorithmName) {
  11. try {
  12. MessageDigest m = MessageDigest.getInstance(algorithmName);
  13. m.update(inputText.getBytes("UTF8"));
  14. byte s[] = m.digest();
  15. // m.digest(inputText.getBytes("UTF8"));
  16. return hex(s);
  17. } catch (NoSuchAlgorithmException e) {
  18. e.printStackTrace();
  19. } catch (UnsupportedEncodingException e) {
  20. e.printStackTrace();
  21. }
  22. return "";
  23. }
  24. // 返回十六进制字符串
  25. private static String hex(byte[] arr) {
  26. StringBuffer sb = new StringBuffer();
  27. for (int i = 0; i < arr.length; ++i) {
  28. sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
  29. }
  30. return sb.toString();
  31. }

3、IOS写法

  1. + (NSString *)md5:(NSString *)str{
  2. const char *cStr = [str UTF8String];
  3. unsigned char result[32];
  4. CC_MD5( cStr, strlen(cStr), result );
  5. return [NSString stringWithFormat:
  6. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  7. result[0],result[1],result[2],result[3],
  8. result[4],result[5],result[6],result[7],
  9. result[8],result[9],result[10],result[11],
  10. result[12],result[13],result[14],result[15],
  11. result[16], result[17],result[18], result[19],
  12. result[20], result[21],result[22], result[23],
  13. result[24], result[25],result[26], result[27],
  14. result[28], result[29],result[30], result[31]];
  15. }

4、Android JNI 方法

  1. #include "md5.h"
  2. #include <jni.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. void ByteToHexStr(const char *source, char *dest, int sourceLen) {
  6. short i;
  7. char highByte, lowByte;
  8. for (i = 0; i < sourceLen; i++) {
  9. highByte = source[i] >> 4;
  10. lowByte = source[i] & 0x0f;
  11. highByte += 0x30;
  12. if (highByte > 0x39) {
  13. dest[i * 2] = highByte + 0x07;
  14. } else {
  15. dest[i * 2] = highByte;
  16. }
  17. lowByte += 0x30;
  18. if (lowByte > 0x39) {
  19. dest[i * 2 + 1] = lowByte + 0x07;
  20. } else {
  21. dest[i * 2 + 1] = lowByte;
  22. }
  23. }
  24. }
  25. //转成小写字符
  26. void MyToLower(char *s){
  27. int len=strlen(s);
  28. for(int i=0;i<len;i++){
  29. if(s[i]>='A'&&s[i]<='Z'){
  30. //s[i]=tolower(s[i]);
  31. s[i]+=32;//+32转换为小写,-32为大写
  32. //s[i]=s[i]-'A'+'a';
  33. }
  34. }
  35. }
  36. // byte数组转MD5字符串
  37. extern "C"
  38. jstring ToMd5(JNIEnv *env, jbyteArray source) {
  39. // MessageDigest类
  40. jclass classMessageDigest = env->FindClass("java/security/MessageDigest");
  41. // MessageDigest.getInstance()静态方法
  42. jmethodID midGetInstance = env->GetStaticMethodID(classMessageDigest, "getInstance",
  43. "(Ljava/lang/String;)Ljava/security/MessageDigest;");
  44. // MessageDigest object
  45. jobject objMessageDigest = env->CallStaticObjectMethod(classMessageDigest, midGetInstance,
  46. env->NewStringUTF("md5"));
  47. // update方法,这个函数的返回值是void,写V
  48. jmethodID midUpdate = env->GetMethodID(classMessageDigest, "update", "([B)V");
  49. env->CallVoidMethod(objMessageDigest, midUpdate, source);
  50. // digest方法
  51. jmethodID midDigest = env->GetMethodID(classMessageDigest, "digest", "()[B");
  52. jbyteArray objArraySign = (jbyteArray) env->CallObjectMethod(objMessageDigest, midDigest);
  53. jsize intArrayLength = env->GetArrayLength(objArraySign);
  54. jbyte *byte_array_elements = env->GetByteArrayElements(objArraySign, NULL);
  55. size_t length = (size_t) intArrayLength * 2 + 1;
  56. char *char_result = (char *) malloc(length);
  57. memset(char_result, 0, length);
  58. // 将byte数组转换成16进制字符串,发现这里不用强转,jbyte和unsigned char应该字节数是一样的
  59. ByteToHexStr((const char *) byte_array_elements, char_result, intArrayLength);
  60. // 在末尾补\0
  61. *(char_result + intArrayLength * 2) = '\0';
  62. //如果需要转小写,请添加这个方法
  63. //MyToLower(char_result);
  64. jstring stringResult = env->NewStringUTF(char_result);
  65. // release
  66. env->ReleaseByteArrayElements(objArraySign, byte_array_elements, JNI_ABORT);
  67. // 释放指针使用free
  68. free(char_result);
  69. env->DeleteLocalRef(classMessageDigest);
  70. env->DeleteLocalRef(objMessageDigest);
  71. return stringResult;
  72. }