Category - Objective-C

2017-06-17 15:26:02    165    0    0
  1. - (NSData *)AES128EncryptWithKey:(NSString *)key gIv:(NSString *)Iv{//加密
  2. char keyPtr[kCCKeySizeAES128+1];
  3. bzero(keyPtr, sizeof(keyPtr));
  4. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  5. char ivPtr[kCCKeySizeAES128+1];
  6. memset(ivPtr, 0, sizeof(ivPtr));
  7. [Iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
  8. NSUInteger dataLength = [self length];
  9. size_t bufferSize = dataLength + kCCBlockSizeAES128;
  10. void *buffer = malloc(bufferSize);
  11. size_t numBytesEncrypted = 0;
  12. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
  13. kCCAlgorithmAES128,
  14. kCCOptionPKCS7Padding,
  15. keyPtr,
  16. kCCBlockSizeAES128,
  17. ivPtr,
  18. [self bytes],