Guest User

Untitled

a guest
Jul 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. NSString *Input = [Inputbox text];
  2. [Input AES256encryptWithKey];
  3.  
  4. @implementation NSData (AES256)
  5.  
  6. - (NSData *)AES256EncryptWithKey:(NSString *)key {
  7. // 'key' should be 32 bytes for AES256, will be null-padded otherwise
  8. char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
  9. bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
  10.  
  11. // fetch key data
  12. [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
  13.  
  14. NSUInteger dataLength = [self length];
  15.  
  16. size_t bufferSize = dataLength + kCCBlockSizeAES128;
  17. void *buffer = malloc(bufferSize);
  18.  
  19. size_t numBytesEncrypted = 0;
  20. CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
  21. keyPtr, kCCKeySizeAES256,
  22. NULL /* initialization vector (optional) */,
  23. [self bytes], dataLength, /* input */
  24. buffer, bufferSize, /* output */
  25. &numBytesEncrypted);
  26. if (cryptStatus == kCCSuccess) {
  27. //the returned NSData takes ownership of the buffer and will free it on deallocation
  28. return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
  29. }
  30.  
  31. free(buffer); //free the buffer;
  32. return nil;
  33. }
  34.  
  35. @implementation NSString (AES256)
Add Comment
Please, Sign In to add comment