The use of BASE64 encoded encryption and decryption in IOS objective-c

Source: Internet
Author: User
Tags base64

BASE64 uses commonly used URL ciphertext encoding to pass longer identity information in an HTTP environment. The adoption of BASE64 encoding is not only shorter but also unreadable.

The following address is the encrypted Thunderbolt dedicated download address, using the code.
such as thunder://qufodhrwoi8vd3d3lmjhawr1lmnvbs9pbwcvc3nsbtffbg9nby5nawzawg==

The method of encrypting and decrypting using BASE64 in iOS is also very simple and can be realized directly with Google-toolbox-for-mac GTMBase64.h.

The corresponding address of the GOOGLE-TOOLBOX-FOR-MAC is as follows:

http://code.google.com/p/google-toolbox-for-mac/

You can find a lot of the help you need, but here we only use the following 3 files
GTMDefines.h
GTMBase64.h
Gtmbase64.m


Download Address

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87

The use of the following methods:
Encryption:

The code is as follows Copy Code


[[NSString Alloc] initwithdata:[gtmbase64 Encodedata:datatoencode]
Encoding:nsutf8stringencoding];

Decrypt:

The code is as follows Copy Code

[[NSString Alloc] initwithdata:[gtmbase64 Decodestring:datatodecode]
Encoding:nsutf8stringencoding];

Combine the previous MD5 and SHA1 results to use:

The code is as follows Copy Code
-(NSString *) sha1_base64
{
const char *CSTR = [self cstringusingencoding:nsutf8stringencoding];
NSData *data = [NSData datawithbytes:cstr length:self.length];

uint8_t Digest[cc_sha1_digest_length];

CC_SHA1 (Data.bytes, Data.length, Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_sha1_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}

-(NSString *) md5_base64
{
const char *CSTR = [self utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CStr), Digest);

NSData * base64 = [[NSData alloc]initwithbytes:digest length:cc_md5_digest_length];
Base64 = [GTMBase64 encodedata:base64];

NSString * output = [[NSString alloc] initwithdata:base64 encoding:nsutf8stringencoding];
return output;
}
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.