Common encryption and decryption methods for iOS

Source: Internet
Author: User

MD5 values are often calculated on other platforms, which are also available on the iOS platform, first importing header files

[CPP]View PlainCopy
    1. #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
  1. @implementation NSString (Cccryptutil)
  2. -(nsstring*) MD5 {
  3. Const char * cstrvalue = [self utf8string];
  4. unsigned char theresult[cc_md5_digest_length];
  5. CC_MD5 (Cstrvalue, strlen (cstrvalue), theresult);
  6. return [NSString stringwithformat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  7. Theresult[0], theresult[1], theresult[2], theresult[3],
  8. THERESULT[4], theresult[5], theresult[6], theresult[7],
  9. THERESULT[8], theresult[9], theresult[10], theresult[11],
  10. THERESULT[12], theresult[13], theresult[14], theresult[15]];
  11. }
  12. @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
    1. #import <CommonCrypto/CommonCryptor.h>

Classify NSData, add nsdata encryption and decryption method

[CPP]View PlainCopy
  1. @implementation NSData (Cccryptutil)
  2. -(nsdata*) Aes256encryptwithkey: (nsstring*) Key {
  3. Char keyptr[kcckeysizeaes256 + 1]; //Terminator (unused)
  4. Bzero (keyptr, sizeof (KEYPTR)); //Fill with zeroes (for padding)
  5. [Key getcstring:keyptr MaxLength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
  6. Nsuinteger datalength = [self length];
  7. size_t buffersize = datalength + kCCBlockSizeAES128;
  8. void* buffer = malloc (buffersize);
  9. size_t numbytesencrypted = 0;
  10. Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, kCCAlgorithmAES128, kccoptionpkcs7padding,
  11. Keyptr, kCCKeySizeAES256,
  12. NULL /* initialization vector (optional) */,
  13. [Self bytes], datalength,/ * input * /
  14. Buffer, buffersize,/ * output * /
  15. &numbytesencrypted);
  16. if (cryptstatus = = kccsuccess) {
  17. return [NSData datawithbytesnocopy:buffer length:numbytesencrypted];
  18. }
  19. Free (buffer);
  20. return nil;
  21. }
  22. -(nsdata*) Aes256decryptwithkey: (nsstring*) Key {
  23. Char keyptr[kcckeysizeaes256 + 1]; //Terminator (unused)
  24. Bzero (keyptr, sizeof (KEYPTR)); //Fill with zeroes (for padding)
  25. //Fetch key Data
  26. [Key getcstring:keyptr MaxLength:sizeof (KEYPTR) encoding:nsutf8stringencoding];
  27. Nsuinteger datalength = [self length];
  28. size_t buffersize = datalength + kCCBlockSizeAES128;
  29. void* buffer = malloc (buffersize);
  30. size_t numbytesdecrypted = 0;
  31. Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt, kCCAlgorithmAES128, kccoptionpkcs7padding,
  32. Keyptr, kCCKeySizeAES256,
  33. NULL /* initialization vector (optional) */,
  34. [Self bytes], datalength,/ * input * /
  35. Buffer, buffersize,/ * output * /
  36. &numbytesdecrypted);
  37. if (cryptstatus = = kccsuccess) {
  38. return [NSData datawithbytesnocopy:buffer length:numbytesdecrypted];
  39. }
  40. Free (buffer);  //free the buffer;
  41. return nil;
  42. }
  43. @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
  1. @implementation NSString (Cccryptutil)
  2. MD5 method omitted here
  3. + (nsdata*) Aes256encrypt: (nsstring*) strsource Withkey: (nsstring*) Key {
  4. NSData *datasource = [strsource datausingencoding:nsutf8stringencoding];
  5. return [DataSource aes256encryptwithkey:[key MD5]];
  6. }
  7. + (nsstring*) Aes256decrypt: (nsdata*) DataSource Withkey: (nsstring*) Key {
  8. NSData *decryptdata = [DataSource aes256decryptwithkey:[key MD5];
  9. return [[NSString alloc] Initwithdata:decryptdata encoding:nsutf8stringencoding];
  10. }
  11. @end

Common encryption and decryption methods for 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.