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