Two kinds of MD5 encryption methods in Android environment

Source: Internet
Author: User
Tags md5 encryption

in peacetime development process, MD5 encryption is a more commonly used algorithm, the most common use scenario is in the Account Register, the user input password by MD5 encryption, transfer to the server to save. Although MD5 encryption is often used. But MD5 's encryption principle I really can not say, the cognition of MD5 now only stay in the level that will use. It's going to take some time to figure it out. This is an introduction to the MD5 encryption algorithm. This paper mainly introduces two kinds of MD5 encryption methods under the Android platform, namely MD5 encryption based on Java language and MD5 encryption based on C language in NDK environment.

The following code is the Java-language-based MD5 encryption:

public string getMD5 (string info) {Try{messagedigest MD5 = messagedigest.getinstance ("MD5"); Md5.update (Info.getbytes ( "UTF-8")); byte[] encryption = Md5.digest ();  StringBuffer strbuf = new StringBuffer (); for (int i = 0; i < encryption.length; i++) {if (Integer.tohexstring (0xFF & Encryption[i]). Length () = = 1) {strbuf.append ("0"). Append (integer.tohexstring (0xFF & Encryption[i]);} Else{strbuf.append (integer.tohexstring (0xFF & Encryption[i));}} return strbuf.tostring ();} catch (NoSuchAlgorithmException e) {return "";} catch (Unsupportedencodingexception e) {return "";}}

The following code is the C-based MD5 encryption in the NDK environment:
#include <jni.h> #include <stdio.h> #include <string.h> #include "md5.h"//MD5 encryption Jniexport jstring Jnicall java_com_example_testmd5_mainactivity_encryptbymd5 (jnienv *env, Jclass obj, jstring strText) {char* SzText = ( char*) (*env)->getstringutfchars (env, strText, 0); Md5_ctx context = {0}; Md5init (&context); Md5update (&context, Sztext, strlen (sztext)); unsigned char dest[16] = {0}; Md5final (dest, &context);(*env)->releasestringutfchars (env, StrText, sztext); int i = 0;char szmd5[32] = {0};for ( i = 0; I < 16; i++) {sprintf (szMd5, "%s%02x", SzMd5, Dest[i]);} Return (*ENV)->newstringutf (env, SZMD5);}
just the above code is only part of the, because the MD5 encryption algorithm C source code file is longer, it is not shown here. Students who want to study can download the full Project view, Project download link is: http://download.csdn.net/detail/u013085897/8097613. Project execution results such as what you see, Hello World is the string to be encrypted.





Two kinds of MD5 encryption methods in Android environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.