iOS string MD5 Encrypted, returns the encrypted string

Source: Internet
Author: User

IOS MD5 Encryption algorithm

1 #import<CommonCrypto/CommonDigest.h>//need to import for CC_MD5 access2 3 4-(NSString *) MD5: (NSString *) Str5 {6     Const Char*cstr =[str utf8string];7UnsignedCharresult[ -];8CC_MD5 (CStr, strlen (CSTR), result);//The MD5 call9     return[NSString stringWithFormat:Ten         @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", Oneresult[0], result[1], result[2], result[3],  Aresult[4], result[5], result[6], result[7], -result[8], result[9], result[Ten], result[ One], -result[ A], result[ -], result[ -], result[ the] the         ];  -}

The corresponding Java encryption algorithm

1  PackageCom.hiveview.phone.util;2 3 Importjava.security.MessageDigest;4 5 6  Public classMd5utils {7  8         Private Final StaticString[] hexdigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F" };9  Ten  One         /** A   - * Convert byte array to 16 binary string -   the          *  -          * @paramb -   - * byte array +   -          * @return16 binary strings +   A          */ at   -          Public StaticString bytearraytohexstring (byte[] b) { -   -StringBuffer RESULTSB =NewStringBuffer (); -   -                  for(inti = 0; i < b.length; i++) in                 { -   to resultsb.append (bytetohexstring (b[i)); +   -                 } the   *                 returnresultsb.tostring (); $  Panax Notoginseng         } -   the  +         Private StaticString bytetohexstring (byteb) { A   the                 intn =b; +   -                 if(N < 0) $   $n = 256 +N; -   -                 intD1 = n/16; the   -                 intD2 = n 16;Wuyi   the                 returnHEXDIGITS[D1] +HEXDIGITS[D2]; -   Wu         } -   About  $          Public Staticstring Md5encode (String origin) { -   -String resultstring =NULL; -   A  +                 Try { the   -Resultstring =NewString (origin); $   theMessageDigest MD = messagedigest.getinstance ("MD5"); the   theResultstring =bytearraytohexstring (Md.digest (Resultstring.getbytes ())); the   -}Catch(Exception ex) { in   the ex.printstacktrace (); the                 } About   the                 returnresultstring; the   the         } +   -}

iOS string MD5 Encrypted, returns the encrypted string

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.