3 Ways to encrypt IOS

Source: Internet
Author: User
Tags md5 encryption

Need to import #import <CommonCrypto/CommonCryptor.h>

==============MD5 Encryption ============

NSString *str = @ "Encrypted content";

String converted to C language

const char *CSTR=[STR utf8string];

The result of MD5 encryption is 128 bits, which requires a 16-byte space to be opened

unsigned char result[16];

Calling cryptographic functions

CC_MD5 (CSTR, (unsigned int) strlen (CSTR), result);

The MD5 obtained by the above method is a 16-character array that needs to be converted to a 32-bit MD5 value

nsmutablestring *string = [nsmutablestring stringwithcapacity:10];

for (int i=; i<16; i++) {

[String appendstring:[nsstring stringwithformat:@ "%02x", Result[i]];

}

NSLog (@ "MD5 encryption:%@", string);

===============base64 Encryption and decryption =============

iOS7 later provides the Base64 transcoding method

Encryption

NSString *[email protected] "encrypted content";

NSData *passdata=[pass datausingencoding:nsutf8stringencoding];

NSString *result = [PassData base64encodedstringwithoptions:nsdatabase64encodingendlinewithlinefeed];

NSLog (@ "base64 encryption:%@", result);

Decrypt

NSData *decodedata = [[NSData alloc]initwithbase64encodedstring:result options:];

NSString *decodestr = [[NSString alloc]initwithdata:decodedata encoding:nsutf8stringencoding];

NSLog (@ "Base64 decryption:%@", DECODESTR);

================aes Encryption and decryption ===============

Create a new NSData class and write a two-bit method

Encryption method

-(nsdata*) Aes256encryptwithkey: (nsstring*) Key {

Char keyptr[kcckeysizeaes256 + 1]; Terminator (unused)

Bzero (keyptr, sizeof (KEYPTR)); Fill with zeroes (for padding)

[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];

Nsuinteger datalength = [self length];

size_t buffersize = datalength + kCCBlockSizeAES128;

void* buffer = malloc (buffersize);

size_t numbytesencrypted =;

Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128, kccoptionpkcs7padding,

Keyptr, kCCKeySizeAES256,

NULL/* initialization vector (optional) */,

[Self bytes], datalength,/* input */

Buffer, buffersize,/* output */

&numbytesencrypted);

if (Cryptstatus = = kccsuccess) {

return [NSData Datawithbytesnocopy:buffer length:numbytesencrypted];

}

Free (buffer);

return nil;

}

Decryption method

-(nsdata*) Aes256decryptwithkey: (nsstring*) Key {

Char keyptr[kcckeysizeaes256 + 1]; Terminator (unused)

Bzero (keyptr, sizeof (KEYPTR)); Fill with zeroes (for padding)

Fetch key Data

[Key Getcstring:keyptr maxlength:sizeof (KEYPTR) encoding:nsutf8stringencoding];

Nsuinteger datalength = [self length];

size_t buffersize = datalength + kCCBlockSizeAES128;

void* buffer = malloc (buffersize);

size_t numbytesdecrypted =;

Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128, kccoptionpkcs7padding,

Keyptr, kCCKeySizeAES256,

NULL/* initialization vector (optional) */,

[Self bytes], datalength,/* input */

Buffer, buffersize,/* output */

&numbytesdecrypted);

if (Cryptstatus = = kccsuccess) {

return [NSData Datawithbytesnocopy:buffer length:numbytesdecrypted];

}

Free (buffer); Free the buffer;

In another class, call the method in the NSData above.

==========aes Encryption and decryption =============

NSString *key = @ "MyKey";//Key

NSString *secret = @ "Encrypt content";//Prepare encrypted content

NSData *plain = [Secret datausingencoding:nsutf8stringencoding];

NSData *ciper = [plain aes256encryptwithkey:key];

NSLog (@ "AES encryption%@", Ciper);

Decrypt

Plain = [Ciper Aes256decryptwithkey:key];

NSLog (@ "AES decryption:%@", [[NSString Alloc]initwithdata:plain encoding:nsutf8stringencoding]);

3 Ways to encrypt IOS

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.