iOS-study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 1 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // AESCipher.m
  3. // iOSFirst
  4. //
  5. // Created by 孙宇峰 on 2023/3/2.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "AESCipher.h"
  9. #import <CommonCrypto/CommonCryptor.h>
  10. NSString const *kInitVector = @"x00/x00/x00/x00/x00/x00/x00/x00";
  11. size_t const kKeySize = kCCKeySizeAES128;
  12. NSData * cipherOperation(NSData *contentData, NSData *keyData, CCOperation operation) {
  13. NSUInteger dataLength = contentData.length;
  14. void const *initVectorBytes = [kInitVector dataUsingEncoding:NSUTF8StringEncoding].bytes;
  15. void const *contentBytes = contentData.bytes;
  16. void const *keyBytes = keyData.bytes;
  17. size_t operationSize = dataLength + kCCBlockSizeAES128;
  18. void *operationBytes = malloc(operationSize);
  19. if (operationBytes == NULL) {
  20. return nil;
  21. }
  22. size_t actualOutSize = 0;
  23. CCCryptorStatus cryptStatus = CCCrypt(operation,
  24. kCCAlgorithmAES128,
  25. kCCOptionPKCS7Padding|kCCOptionECBMode,
  26. keyBytes,
  27. kCCKeySizeAES128,
  28. nil,
  29. contentBytes,
  30. dataLength,
  31. operationBytes,
  32. operationSize,
  33. &actualOutSize);
  34. if (cryptStatus == kCCSuccess) {
  35. return [NSData dataWithBytesNoCopy:operationBytes length:actualOutSize];
  36. }
  37. free(operationBytes);
  38. operationBytes = NULL;
  39. return nil;
  40. }
  41. NSString * aesEncryptString(NSString *content, NSString *key) {
  42. NSCParameterAssert(content);
  43. NSCParameterAssert(key);
  44. NSData *contentData = [content dataUsingEncoding:NSUTF8StringEncoding];
  45. NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
  46. NSData *encrptedData = aesEncryptData(contentData, keyData);
  47. return [encrptedData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  48. }
  49. NSString * aesDecryptString(NSString *content, NSString *key) {
  50. NSCParameterAssert(content);
  51. NSCParameterAssert(key);
  52. NSData *contentData = [[NSData alloc] initWithBase64EncodedString:content options:NSDataBase64DecodingIgnoreUnknownCharacters];
  53. NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
  54. NSData *decryptedData = aesDecryptData(contentData, keyData);
  55. return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
  56. }
  57. NSData * aesEncryptData(NSData *contentData, NSData *keyData) {
  58. NSCParameterAssert(contentData);
  59. NSCParameterAssert(keyData);
  60. NSString *hint = [NSString stringWithFormat:@"The key size of AES-%lu should be %lu bytes!", kKeySize * 8, kKeySize];
  61. NSCAssert(keyData.length == kKeySize, hint);
  62. return cipherOperation(contentData, keyData, kCCEncrypt);
  63. }
  64. NSData * aesDecryptData(NSData *contentData, NSData *keyData) {
  65. NSCParameterAssert(contentData);
  66. NSCParameterAssert(keyData);
  67. NSString *hint = [NSString stringWithFormat:@"The key size of AES-%lu should be %lu bytes!", kKeySize * 8, kKeySize];
  68. NSCAssert(keyData.length == kKeySize, hint);
  69. return cipherOperation(contentData, keyData, kCCDecrypt);
  70. }