Objective-C string MD5, SHA1, SHA256, AES encryption implementation

Source: Internet
Author: User
Tags sha1 encryption asymmetric encryption


Problem:


When I log on as a user, the background API requires the MD5 encrypted student ID and the string that the request time is merged, so I found an encrypted framework on GitHub with the link: https: // github. (NIU) com/paulery2014/CocoaSecurity. I wrote a small Demo for your convenience.


Usage:



Download the CocoaSecurity framework to the local machine, import the four files of Base64 and CocoaSecurity, and then import the header file "CocoaSecurity. h" in the. m file to be called.



Various encryption methods:


// Signature algorithm (MD5, SHA1, HMAC)
    
    // string MD5 encryption
    CocoaSecurityResult *result_md5 = [CocoaSecurity md5:@"liuxiaofei_md5"];
    // string SHA1 encryption
    CocoaSecurityResult *result_sha1 = [CocoaSecurity sha1:@"liuxiaofei_sha1"];
    // String SHA256 encryption
    CocoaSecurityResult *result_sha256 = [CocoaSecurity sha256:@"liuxiaofei_sha256"];
    
    // Print hash (md5, sha1, sha256) encrypted character difference
    NSLog (@"MD5 encrypted string is hex:%@", result_md5.hex);
    NSLog (@"MD5 encrypted string is hexLower:%@", result_md5.hexLower);
    NSLog (@"MD5 encrypted string is base64:%@", result_md5.base64);
    
    NSLog (@"SHA1 encrypted string is hex:%@", result_sha1.hex);
    NSLog (@"SHA1 encrypted string is hexLower:%@", result_sha1.hexLower);
    NSLog (@"SHA1 encrypted string is base64:%@", result_sha1.base64);
    
    NSLog (@"SHA256 encrypted string is hex:%@", result_sha256.hex);
    NSLog (@"SHA256 encrypted string is hexLower:%@", result_sha256.hexLower);
    NSLog (@"SHA256 encrypted string is base64:%@", result_sha256.base64);

    // Symmetric encryption algorithm (3DS, AES)
    
    /*AES encryption:
     *Advanced encryption standard
     * is the next generation encryption algorithm standard, fast, high security level
     * One implementation of the AES standard is the Rijndael algorithm
     */
    CocoaSecurityResult *result_AES = [CocoaSecurity aesEncrypt:@"liuxiaofei" key:@"testKey"];
    NSLog (@"AES encrypted string: %@", result_AES.hex);
    NSLog (@"AES encrypted string: %@", result_AES.hexLower);
    NSLog (@"AES encrypted string: %@", result_AES.base64);
    
    // asymmetric encryption algorithm (small data volume)
    // RSA....blablabla


Related Article

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.