MD5 values are often calculated on other platforms, which are also available on the iOS platform, first importing header files
[CPP]View PlainCopy
- #import <CommonCrypto/CommonDigest.h>
The Cc_md5 method can obtain a 32-bit MD5 value by obtaining a 16-character array of MD5 and outputting it in%02x form.
[CPP]View PlainCopy
- @implementation NSString (Cccryptutil)
- -(nsstring*) MD5 {
- Const char * cstrvalue = [self utf8string];
- unsigned char theresult[cc_md5_digest_length];
- CC_MD5 (Cstrvalue, strlen (cstrvalue), theresult);
- return [NSString stringwithformat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
- Theresult[0], theresult[1], theresult[2], theresult[3],
- THERESULT[4], theresult[5], theresult[6], theresult[7],
- THERESULT[8], theresult[9], theresult[10], theresult[11],
- THERESULT[12], theresult[13], theresult[14], theresult[15]];
- }
- @end
MD5 can only be called an irreversible encryption algorithm, can only be used as a test process, can not restore its original text.
Apple also provides RSA, DES, AES and other encryption algorithms, see the foreign website about AES encryption algorithm, this processing can be used for string encryption secret, can be used for high security requirements of the application.
First, you need to import the header file
[CPP]View PlainCopy
- #import <CommonCrypto/CommonCryptor.h>
Classify NSData, add nsdata encryption and decryption method
[CPP]View PlainCopy
- @implementation NSData (Cccryptutil)
- -(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 = 0;
- 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;
- }
- -(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 = 0;
- 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;
- return nil;
- }
- @end
The above code Aes256encryptwithkey method is the cryptographic function, Aes256decryptwithkey is the decryption function, the encryption and decryption methods use the parameter key is a 32-bit length string, Therefore, any string can be MD5 computed 32-bit string as the key, which allows the customer to enter any length of the key, and the MD5 value of the different keys will not be duplicated.
In combination with the above code, the NSString class is processed and the AES encryption and decryption method of the string is provided. The code is as follows:
[CPP]View PlainCopy
- @implementation NSString (Cccryptutil)
- MD5 method omitted here
- + (nsdata*) Aes256encrypt: (nsstring*) strsource Withkey: (nsstring*) Key {
- NSData *datasource = [strsource datausingencoding:nsutf8stringencoding];
- return [DataSource aes256encryptwithkey:[key MD5]];
- }
- + (nsstring*) Aes256decrypt: (nsdata*) DataSource Withkey: (nsstring*) Key {
- NSData *decryptdata = [DataSource aes256decryptwithkey:[key MD5];
- return [[NSString alloc] Initwithdata:decryptdata encoding:nsutf8stringencoding];
- }
- @end
Common encryption and decryption methods for iOS