3 Replies
      Latest reply on Jan 16, 2020 1:46 PM by eskimo
      Alex Zhang Level 1 Level 1 (0 points)

        When I decrypt, there will be a decryption exception due to multiple version judgments.

        but,Remove the version judgment and normal results will be displayed.

        (Encrypted data & key & iv Encrypted data all identical)

        Encrypted Data from [NSUserDefaults standardUserDefaults]

         

         

         

        ================================================================

        Func:

         

        Step1:

         

        NSData* theData = [[[NSData alloc] initWithBase64EncodedString:userID options:NSDataBase64DecodingIgnoreUnknownCharacters]AES128DecryptWithKey: KEY withIV: IV];

         

         

        Step2:

        - (NSData *) AES128DecryptWithKey: (NSString *) _sKey withIV: (unsigned int) _iv {

          char keyPtr[kCCKeySizeAES128 + 1];

          bzero(keyPtr, sizeof(keyPtr));

          [_sKey getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];

         

          NSUInteger dataLength = [self length];

          NSData *nsData;

          size_t bufferSize = dataLength + kCCBlockSizeAES128;

          void *buffer = malloc(bufferSize);

          void *iv = malloc(16);

          memset((void *)iv, _iv, 16);

         

          size_t numBytesDecrypted = 0;

          CCCryptorStatus cryptStatus = CCCrypt( kCCDecrypt,

                                                                             kCCAlgorithmAES128,

                                                                              kCCOptionPKCS7Padding,

                                                                              keyPtr,

                                                                              kCCKeySizeAES128,

                                                                             iv ,                                                                                                                 

                                                                             [self bytes], dataLength,

                                                                             buffer, bufferSize,

                                                                             &numBytesDecrypted);                            

         

          if (cryptStatus == kCCSuccess)

               nsData = [NSData dataWithBytes: buffer length: numBytesDecrypted];

         

          free(iv);

          free(buffer);

         

          return nsData;

        }

        ================================================================

         

        Xcode Ver   : 11.3
        Test device : SimPhone 11~13 & Iphone 6(Ver.11)、IphoneXs(Ver.13)



        • Re: AES128decryption failed
          eskimo Apple Staff Apple Staff (12,715 points)

          I’m sorry to say that this code has numerous problems, but at the code level and at the cryptographic level (using a C string as-is as a cryptographic key is very poor form).  I can help you fix the code problems but I encourage you to discuss the cryptographic aspects of this with a security professional.

          Anyway, on the code front, I recommend that you look at the CryptoCompatibility sample code, which shows how to do AES encryption and decryption in a way that’s compatible with other major cryptographic libraries.  If you can’t work out from that, please post a code snippet and a test vector, that is, the inputs to the function and the expected outputs.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"