V
V
vitaliyharchenko2015-02-20 14:01:50
PHP
vitaliyharchenko, 2015-02-20 14:01:50

What is wrong with AES encryption?

For authorization to work in the application, it is necessary to carry out AES256 decryption. There is an equivalent in PHP. I can’t debug work on Obj C to the end.
Analogue in PHP on SandBox
Code on SandBox
On Objective C I execute the code:

NSString *rndEncoded = @"CzN5txPZjwyfG2/YhQ8ddqpGDIY8meeW2oZL9+Rvbzp4B1UBVsOCsTFqGJ528TfX";
        
        const char *pass_md5 = "92877af70a45fd6a2ed7fe81e1236b78";
        char key1[32];
        for(int i = 0; i < 16; i++){
            int q;
            sscanf(pass_md5 + i * 2, "%02x", &q);
            key1[i] = q;
        }
        for(int i = 16; i < 32; i ++)
            key1[i] = 0;
        
        int iv_size = 16;
        
        NSMutableData *ciphertext_dec_data = [[NSMutableData alloc] initWithBase64EncodedString:rndEncoded options:0];

        NSInteger ciphertext_length = ciphertext_dec_data.length;

        NSRange iv_dec_range = NSMakeRange(0, iv_size);
        NSData *iv_dec_data = [ciphertext_dec_data subdataWithRange:iv_dec_range];
        
        NSRange ciphertext_dec_range = NSMakeRange(iv_size, ciphertext_length - iv_size);
        NSData *ciphertext_dec = [ciphertext_dec_data subdataWithRange:ciphertext_dec_range];
       
        NSData *key = [NSData dataWithBytes:key1 length:32];
        NSData *iv = iv_dec_data;
        NSData *data = ciphertext_dec;
        
        NSMutableData *cipherData = [NSMutableData dataWithLength:32];
        size_t outLength;
        
        CCCryptorStatus result
        = CCCrypt(kCCDecrypt, // operation, replace with kCCDecrypt to decrypt
                  kCCAlgorithmAES128, // Same as MCRYPT_RIJNDAEL_256
                  kCCOptionECBMode , // CBC mode
                  key1, // key
                  16, // Since you are using AES256
                  iv.bytes,// iv
                  data.bytes, // dataIn
                  data.length, // dataInLength,
                  cipherData.mutableBytes, // dataOut
                  cipherData.length, // dataOutAvailable
                  &outLength); // dataOutMoved
        if (result == kCCSuccess) {            
            NSString *resultString = [cipherData base64Encoding];
            NSLog(@"chiperdata length = %lu", (unsigned long)cipherData.length);
            NSLog(@"chiperdata = %@", cipherData);
            NSLog(@"result = %@", resultString);
            NSLog(@"should be = 78f8aceb24b60fb12990397f52a62067");
            NSData *must = [@"78f8aceb24b60fb12990397f52a62067" dataUsingEncoding:4];
            NSLog(@"should bytes = %@", must);
        } else {
            NSLog(@"Crypthor status: %d", result);
        }

I output all variables to the console and compare them with php byte by byte, everything matches until CCCrypt is triggered.
result = PAsfj3K66m6tLw3utWl/R5h/NbYPoNDw77QqwdZfWQ0=
should be = 78f8aceb24b60fb12990397f52a62067
result bytes = <3c0b1f8f 72baea6e ad2f0dee b5697f47 987f35b6 0fa0d0f0 efb42ac1 d65f590d>
must bytes= <37386638 61636562 32346236 30666231 32393930 33393766 35326136 32303637>

I get at the output: "PAsfj3K66m6tLw3utWl/R5h/NbYPoNDw77QqwdZfWQ0="
And it should be:
"78f8aceb24b60fb12990397f52a62067"
What is the error? Tell me please.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vladimir Martyanov, 2015-02-20
@vilgeforce

Why am I always commenting? :-(
In the above code - kCCOptionECBMode, and in PHP - CBC mode.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question