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 of the scene is in the account registration, the user entered the password by MD5 encryption, transfer to the server to save. Although MD5 encryption is often used, but the MD5 of the principle of encryption I really do not know, the MD5 of the current only to stay in the level of use, want to figure out or take some time, this is MD5 encryption algorithm related introduction. 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:

 Publicstring getMD5 (string info) {Try{messagedigest MD5= Messagedigest.getinstance ("MD5"); Md5.update (Info.getbytes ("UTF-8")); byte[] Encryption =md5.digest (); StringBuffer Strbuf=NewStringBuffer ();  for(inti =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])); }    }          returnstrbuf.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 EncryptionJniexport 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)); unsignedChardest[ -] = {0 }; Md5final (dest,&context); (*ENV)Releasestringutfchars (env, StrText, Sztext); inti =0; Charszmd5[ +] = {0 };  for(i =0; I < -; i++) {sprintf (SZMD5,"%s%02x", SzMd5, Dest[i]); }  return(*env)Newstringutf (env, SZMD5);}

But the above code is only part of it, because the MD5 encryption algorithm C source file is longer, it is not shown here, want to study the students can download the complete Project view, Project download link: http://download.csdn.net/detail/u013085897/8097613. The project runs as shown in Hello World as 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.