EzDevInfo.com

RNCryptor

CCCryptor (AES encryption) wrappers for iOS and Mac. -- For Swift, see swift branch and release 4.0.

Memory issues when encrypting/decrypting a large file with RNCryptor on iOS

I'm trying to use RNCryptor to encrypt and decrypt large files (600+MB) on iOS. On the github I found example code on how to use the library asynchronously on streams. This code is similar to the answer of Rob Napier on a question about this same subject.

However, although I think I implemented the code correctly, the app uses up to 1.5 GB of memory (in the iPad 6.1 simulator). I thought the code was supposed to prevent the app from keeping more than one block of data in-memory? So what is going wrong?

In my controller, I create a 'CryptController' which I message with encrypt/decrypt requests.

  // Controller.m
  NSString *password = @"pw123";
  self.cryptor = [[CryptController alloc] initWithPassword:password];

  //start encrypting file
  [self.cryptor streamEncryptRequest:self.fileName andExtension:@"pdf" withURL:[self samplesURL]];

  //wait for encryption to finish
  NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:1];
  do {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                             beforeDate:timeout];
  } while (![self.cryptor isFinished]);

In CryptController I have:

- (void)streamEncryptionDidFinish {
  if (self.cryptor.error) {
    NSLog(@"An error occurred. You cannot trust decryptedData at this point");
  }
  else {
    NSLog(@"%@ is complete. Use it as you like", [self.tempURL lastPathComponent]);
  }
  self.cryptor = nil;
  self.isFinished = YES;
}

- (void) streamEncryptRequest:(NSString *)fileName andExtension:(NSString *)ext withURL:(NSURL *)directory {

  //Make sure that this number is larger than the header + 1 block.
  int blockSize = 32 * 1024;

  NSString *encryptedFileName = [NSString stringWithFormat:@"streamEnc_%@", fileName];
  self.tempURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  self.tempURL = [self.tempURL URLByAppendingPathComponent:encryptedFileName isDirectory:NO];
  self.tempURL = [self.tempURL URLByAppendingPathExtension:@"crypt"];

  NSInputStream *decryptedStream = [NSInputStream inputStreamWithURL:[[directory URLByAppendingPathComponent:fileName isDirectory:NO] URLByAppendingPathExtension:ext]];
  NSOutputStream *cryptedStream = [NSOutputStream outputStreamWithURL:self.tempURL append:NO];

  [cryptedStream open];
  [decryptedStream open];

  __block NSMutableData *data = [NSMutableData dataWithLength:blockSize];
  __block RNEncryptor *encryptor = nil;

  dispatch_block_t readStreamBlock = ^{
    [data setLength:blockSize];
    NSInteger bytesRead = [decryptedStream read:[data mutableBytes] maxLength:blockSize];
    if (bytesRead < 0) {
      //Throw an error
    }
    else if (bytesRead == 0) {
      [encryptor finish];
    }
    else {
      [data setLength:bytesRead];
      [encryptor addData:data];
      //NSLog(@"Sent %ld bytes to encryptor", (unsigned long)bytesRead);
    }
  };

  encryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
                                           password:self.password
                                            handler:^(RNCryptor *cryptor, NSData *data) {
                                              //NSLog(@"Encryptor received %ld bytes", (unsigned long)data.length);
                                              [cryptedStream write:data.bytes maxLength:data.length];
                                              if (cryptor.isFinished) {
                                                [decryptedStream close];
                                                //call my delegate that i'm finished with decrypting
                                                [self streamEncryptionDidFinish];
                                              }
                                              else {
                                                readStreamBlock();
                                              }
                                            }];

  // Read the first block to kick things off
  self.isFinished = NO;
  readStreamBlock();
}

When I profile using the Allocation Instrument, the allocation categories I see consistently growing are malloc 32.50 KB, malloc 4.00 KB, NSConcreteData and NSSubrangeData. Especially the malloc 32.50 KB grows big, over 1 GB. The responsible caller is [NSConcreteData initWithBytes:length:copy:freeWhenDone:bytesAreVM:] For NSConcreteData the responsible caller is -[NSData(NSData) copyWithZone:].

When I profile using the Leaks Instrument, no leaks are found.

I'm new to Objective-C, and from what I understood, the new ARC is supposed to handle allocation and deallocation of memory. When googling on anything memory related, all the information I find is assuming you don't use ARC (or it didn't exist at time of writing). I sure am using ARC, since I get compile errors saying so when I try to manually deallocate memory.

If anyone can help me with this, it would be greatly appreciated! If any more information is needed, I'll be happy to provide it :) Also, I'm new to StackOverflow, so if there's anything I've overlooked that I should have done, kindly inform me!


Source: (StackOverflow)

Undefined symbols for architecture armv7 after importing RNCryptor

I imported RNCryptor, which can be found here: https://github.com/rnapier/RNCryptor into my app. However, I've been getting three errors with this in the log.

Undefined symbols for architecture armv7:
  "_SecRandomCopyBytes", referenced from:
      +[RNCryptor randomDataOfLength:] in RNCryptor.o
  "_kSecRandomDefault", referenced from:
      +[RNCryptor randomDataOfLength:] in RNCryptor.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I saw that this is normally caused when imported classes are not included in the target, but I have already checked the build phases, and all imported classes are in there. I do not know what is causing this, I have not touched the code of any of these classes. I have also cleaned the target several times.


Source: (StackOverflow)

Advertisements

Dispatch queues and asynchronous RNCryptor

This is a follow-up to Asynchronously decrypt a large file with RNCryptor on iOS

I've managed to asynchronously decrypt a large, downloaded file (60Mb) with the method described in this post, corrected by Calman in his answer.

It basically goes like this:

int blockSize = 32 * 1024;
NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:...];
NSOutputStream *decryptedStream = [NSOutputStream output...];

[cryptedStream open];
[decryptedStream open];

RNDecryptor *decryptor = [[RNDecryptor alloc] initWithPassword:@"blah" handler:^(RNCryptor *cryptor, NSData *data) {
    NSLog("Decryptor recevied %d bytes", data.length);
    [decryptedStream write:data.bytes maxLength:data.length];
    if (cryptor.isFinished) {
        [decryptedStream close];
        // call my delegate that I'm finished with decrypting
    }
}];

while (cryptedStream.hasBytesAvailable) {
    uint8_t buf[blockSize];
    NSUInteger bytesRead = [cryptedStream read:buf maxLength:blockSize];
    NSData *data = [NSData dataWithBytes:buf length:bytesRead];

    [decryptor addData:data];
    NSLog("Sent %d bytes to decryptor", bytesRead);
}

[cryptedStream close];
[decryptor finish];

However, I'm still facing a problem: the whole data is loaded in memory before being decrypted. I can see a bunch of "Sent X bytes to decryptor", and after that, the same bunch of "Decryptor recevied X bytes" in the console, when I'd like to see "Sent, received, sent, receives, ...".

That's fine for small (2Mb) files, or with large (60Mb) files on simulator; but on a real iPad1 it crashes due to memory constraints, so obviously I can't keep this procedure for my production app.

I feel like I need to send the data to the decryptor by using dispatch_async instead of blindly sending it in the while loop, however I'm completely lost. I've tried:

  • creating my own queue before the while, and using dispatch_async(myQueue, ^{ [decryptor addData:data]; });
  • the same, but dispatching the whole code inside of the while loop
  • the same, but dispatching the whole while loop
  • using RNCryptor-provided responseQueue instead of my own queue

Nothing works amongst these 4 variants.

I don't have a complete understanding of dispatch queues yet; I feel the problem lies here. I'd be glad if somebody could shed some light on this.


Source: (StackOverflow)

RNCryptor and Python server

I'm using RNCryptor to encrypt NSData on iOS and send it to a Python server. So far, so right. My problem comes when I try decrypt NSData on server side.

RNCryptor embeds all relevant data in encrypted header, but I can not get the IV in the server side to decrypt data received.

Any idea?

Configuration: iOS 5.0 with RNCryptor / Python + Tornado Web Server

Thanks!


Source: (StackOverflow)

RNCryptor not working with JSON string

Here are my method's to use RNCryptor to encrypt/decrypt a JSON string that I am sending to the web service. I am using a static IV variable which may be bad practice but please don't focus on that. Here is how I'm doing it:

Note: I'm using Matt Gallagher's NSData+Base64 category found here (at bottom of page)

-(NSString*)encryptString:(NSString*)plaintext withKey:(NSString*)key error:(NSError**)error{
    NSData *data = [plaintext dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptionKey = [NSData dataFromBase64String:key];
    NSData *IV = [NSData dataFromBase64String:ENCRYPTION_IV];

    RNCryptorEngine *engine = [[RNCryptorEngine alloc] initWithOperation:kCCEncrypt settings:kRNCryptorAES256Settings key:encryptionKey IV:IV error:error];
    [engine addData:data error:error];
    NSData *encryptedData = [engine finishWithError:error];

    NSString *based64Encrypted = [encryptedData base64EncodedString];
    NSLog(@"Encrytped: %@", based64Encrypted);
    return based64Encrypted;
}
-(NSString*) decryptString:(NSString*)cipherText withKey:(NSString*)key error:(NSError**)error;{
    NSData *data = [NSData dataFromBase64String:cipherText];
    NSData *encryptionKey = [NSData dataFromBase64String:key];
    NSData *IV = [NSData dataFromBase64String:ENCRYPTION_IV];

    RNCryptorEngine *engine = [[RNCryptorEngine alloc] initWithOperation:kCCDecrypt settings:kRNCryptorAES256Settings key:encryptionKey IV:IV error:error];
    [engine addData:data error:error];
    NSData *decryptedData = [engine finishWithError:error];
    NSString *decryptedString = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
    NSLog(@"Decrypted: %@", decryptedString);
    return decryptedString;
}

When I use a string like hello world it works fine. When I use a string like {"username":"developer","password":"abcdefG*12"} I imagine it hase something to do with the encoding but I really know what to use.

when I encrypt that string I get a base64 string and when I try to decrypt that I get an empty string.

UPDATE

It looks like it's failing because of the : in the json string. What's weirder is it only fails with the string is in json format, I thought it was the : cause I tried that first but upon further investigation if I broke any of the JSON requirements ,'s {'s }'s it stopped working. It works with the RNEncryptor however so I'm not sure what I'm doing wrong. Either way, I think we may redesign the current flow

UPDATE 2

Here is where I am calling these methods:

NSDictionary *credentials = @{@"username":@"developer",@"password":@"abcdefG*12"};
NSString *jsonString = [ credentials JSONStringWithOptions:JKSerializeOptionNone error:&error];
NSLog(@"json string: %@", jsonString); //OUTPUTS: {"username":"developer","password":"abcdefG*12"}
CCGEncryption *encryptionObject = [[CCGEncryption alloc] init]; //THIS IS THE OBJECT WHERE THE encrypt/decrypt methods are

NSString *encrypted = [encryptionObject encryptString:jsonString withKey:ENCRYPTION_KEY error:&error];
if(error){
    NSLog(@"Error:%@", error); //NO ERROR
}
NSString *decrypted = [encryptionObject decryptString:encrypted withKey:ENCRYPTION_KEY error:&error];
if(error){
    NSLog(@"Error:%@", error);  //NO ERROR
}
NSLog(@"decrypted: %@", decrypted); //OUTPUT: decrypted: 

Source: (StackOverflow)

RNCryptor: Which settings to change to improve performance?

I have a lot of short strings to decrypt in my app and decryption speed is critical.

Currently I am experimenting with RNCryptor but find its default settings a bit slow for my use case. Otherwise it is an awesome framework :-)

Encryption will be done only once and hence its performance is not important.

I am fine giving up protection for speed as I just want to have a very basic encryption in place.

Which RNCryptor settings would you recommend using to encrypt and decrypt in order to accomplish the fastest decryption performance? A short code sample would be great!

Again, I am fine with very basic encryption protection for the current use case.


Source: (StackOverflow)

How to encrypt in node.js and decrypt with RNCryptor

I'm having trouble encrypting data with node and decrypting with RNCryptor. My understanding is that RNCryptor uses a special data format?

The cryptotext is being generated as follows:

var crypto = require('crypto');
var cipher = crypto.createCipher('aes-256-cbc','InmbuvP6Z8');
var text = "123|123123123123123";
var crypted = cipher.update(text,'utf8','base64');
crypted += cipher.final('base64');

And I'm decrypting like this:

[RNDecryptor decryptData:plainText withPassword:password error:&error];

How am I supposed to do this? When I try to decrypt currently I get an empty NSData and no error.


Source: (StackOverflow)

RNCryptor/JNCryptor - decrypt string in ios which is encrypted in android

Hellp All,

I have used RNCryptor/JNCryptor for iOS and android following is the code for encrypt in Android and Decrypt in iOS.

Encrypt code in Android

public static String EncryptMessage(String message)
{
JNCryptor cryptor = new AES256JNCryptor();
byte[] plaintext = message.getBytes();
byte[] ciphertext = null;

try {
ciphertext = cryptor.encryptData(plaintext,Constants.ENCRYPTION_KEY.toCharArray());
} catch (CryptorException e) {
// Something went wrong
e.printStackTrace();
}

return  Base64.encodeBytes(ciphertext);
}

Decrypt code in iOS

 NSData *fromAData =     [@"AwEche05IIj/OYEN5brgF/p6jyfKfARJliUQwEwa2lko40+ScPMumlDSIZWB2O6XRZjIucXVbggieWaHb2x6sJkYYt7tn3k17V5Jd5PAnkIoFA==" base64DecodedData];

NSData *fromADecryptedData = [RNDecryptor decryptData:fromAData withSettings:kRNCryptorAES256Settings password:@"password" error:&decryptionError];
NSLog(@"decryptionError %@", decryptionError);
NSLog(@"Result = %@", fromPHPDecryptedData);
NSLog(@"Result = %@", [[NSString alloc] initWithData:fromADecryptedData encoding:NSUTF8StringEncoding]);

I have also tried simple decrypt with password

 NSData *fromADecryptedData = [RNDecryptor decryptData:fromAData withPassword:@"password" error:&decryptionError];

Getting the error of unknown header, error code=2

I am using same sting "password" as password at both the side.

If anyone has any idea please let me know if i am missing anything.


Source: (StackOverflow)

AES encryption/decryption for a beginner

I am trying to encrypt an NSString to both NSString and NSData in Objective-C and so I began a search.

I started off here, but that went way over my head, unfortunately.

I then found myself at this post and it came across to be very easy to follow, so I went along and tried to figure out the implementation. After looking over the implementation, I saw the second answer in the post and saw he had more adaptable implementations, which brought me to his gist. As per the gist readme, he "took down this Gist due to concerns about the security of the encryption/decryption". That leads me to believe that the security of the implementation from above has security flaws as well.

From that gist, however, he mentioned another alternative that I could use for encryption. After taking a look at the code, I noticed that it generates NSData with "a header, encryption salt, HMAC salt, IV, ciphertext, and HMAC". I know how to handle that to decode using the same library again, but how would I pass this off to a server guy, given that I don't quite know what I'm sending to him?

At the root of it all, I'm in over my head. Given what I said above and knowing that I don't have the time to take on a lot of learning for this, unless if it is absolutely necessary, how should I best handle going about this encoding/decoding process, given a private key with the end goal of shipping it off to a server that is not designed by me? (How's that for a run on sentence!)


Source: (StackOverflow)

RNCryptor AES256 to match PHP MCRYPT_RIJNDAEL_256

The PHP API I’m calling from within my iOS app requires the payload to be encrypted in a certain customised way. I’m having troubles replicating that approach in Objective-C, with RNCryptor.

Here is the PHP code used to encrypt a string:

function encrypt($string) {
    $key = 'some-random-key';
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
}

And this how I’m trying to achieve the same encryption result in Objective-C:

+ (NSData*)encryptData:(NSData*)sourceData {

    NSString *keyString = @"some-random-key";
    NSData *key = [[keyString MD5String] dataUsingEncoding:NSUTF8StringEncoding];
    NSData *iv = [[[keyString MD5String] MD5String] dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableData *encryptedData = [NSMutableData data];

    RNCryptorEngine *cryptor = [[RNCryptorEngine alloc] initWithOperation:kCCEncrypt settings:kRNCryptorAES256Settings key:key IV:iv error:nil];

    [encryptedData appendData:[cryptor addData:sourceData error:nil]];
    [encryptedData appendData:[cryptor finishWithError:nil]];

    return encryptedData;

}

But the results from the two functions never match. E.g., for the same one-word string, the PHP code returns J39gRcuBEaqMIPP1VlizdA8tRjmyAB6za4zG5wcOB/8=, while in Objective-C (after running base64EncodedStringWithOptions: on the resulting NSData) I’m getting 1FGpZpVm2p4z3BBY6KW2fw==.

Is there something I need to further tweak in the RNCryptor settings to make it work?

UPDATE

I’ve played around the the native iOS CommonCrypto framework directly, without using the third party RNCryptor lib altogether. I’m consisently getting the same result as with RNCryptor though. I even tried implementing AES128 in both my Objective-C and PHP snippets, but even that never made the results from the two environments match…

UPDATE 2

The MD5String method I‘m using is a category on NSString and is defined as follows:

- (NSString *)MD5String {
    const char *cstr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cstr, strlen(cstr), result);

    return [[NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ] lowercaseString];
}

Source: (StackOverflow)

Base64 issue in NSMutableURLRequest POST message?

I'm having communcation issues between my app and the server. I'm using RNCryptor to encrypt a message, which I then base64 encode and transfer to the server in the request. This is done in both the DATA header, and within the http body as post data. I think I'm making a mistake in how I'm converting & transferring the base64 encoded message via POST.

If I receive the encrypted message via the header, it decrypts perfectly fine, every single time. However, if I take the message via the POST data, I'm getting varying results. Most of the time, it fails, else it partially decrypts (first few letters), with 1 in 20 or so successful decryptions.

The objective-c code is:

- (NSString *)sendEncryptedTestMessage:(NSString *)address{
    NSString* messageContent    = @"Hello my name is Bob.";
    NSError * error             = nil;
    NSString* responseString2   = nil;

    NSData*   postData = [RNEncryptor encryptData:[messageContent dataUsingEncoding:NSUTF8StringEncoding]
                                    withSettings:kRNCryptorAES256Settings
                                        password:@"123456"
                                           error:&error];

    NSString* messageServer     = [NSString base64forData:postData];
    NSString* postMessage       = [@"message=" stringByAppendingString:messageServer];
              postData          = [postMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; // problem here I think

    NSString* postLength        = [NSString stringWithFormat:@"%ld",(unsigned long)[postData length]];

    NSURL*    URLToRequest      = [NSURL URLWithString:address];

    NSMutableURLRequest* semisystem = [[[NSMutableURLRequest alloc] initWithURL:URLToRequest] autorelease];

    [semisystem setHTTPMethod:@"POST"];

    [semisystem setHTTPBody:postData];
    [semisystem setValue:postLength                           forHTTPHeaderField:@"Content-Length"];
    [semisystem setValue:self.activationURL                   forHTTPHeaderField:@"EncryptionKey"];
    [semisystem setValue:messageServer                        forHTTPHeaderField:@"data"];

    NSURLResponse* response;
    NSData* data = [NSURLConnection sendSynchronousRequest:semisystem
                                         returningResponse:&response
                                                     error:&error];

    responseString2 = [NSString stringWithFormat:@"%.*s", (int)[data length], [data bytes]];
    return responseString2;
}

PHP code:

function decrypt2($b64_data,$password)
    {
           // back to binary
            //$bin_data = mb_convert_encoding($b64_data, "UTF-8", "BASE64");
            $bin_data = base64_decode($b64_data);
            // extract salt
            $salt = substr($bin_data, 2, 8);
            // extract HMAC salt
            $hmac_salt = substr($bin_data, 10, 8);
            // extract IV
            $iv = substr($bin_data, 18, 16);
            // extract data
            $data = substr($bin_data, 34, strlen($bin_data) - 34 - 32);
            $dataWithoutHMAC = chr(2).chr(1).$salt.$hmac_salt.$iv.$data;
            // extract HMAC
            $hmac = substr($bin_data, strlen($bin_data) - 32);
            // make HMAC key
            $hmac_key = pbkdf2('SHA1', $password, $hmac_salt, 10000, 32, true);
            // make HMAC hash
            $hmac_hash = hash_hmac('sha256', $dataWithoutHMAC , $hmac_key, true);
            // check if HMAC hash matches HMAC
            if($hmac_hash != $hmac) {
                echo "HMAC mismatch".$nl.$nl.$nl;
               // return false;
            }
            // make data key
            $key = pbkdf2('SHA1', $password, $salt, 10000, 32, true);
            // decrypt
            $ret = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);      
        return $ret;
    }
$passkey = "123456";

$messageBase64                  = $_POST['message'];// THIS barely works
$messageBase64              = $_SERVER['HTTP_DATA'];// THIS WORKS
$message                = decrypt2($messageBase64,$passkey);

Many thanks in advance!


Source: (StackOverflow)

using RNCryptor on android

I have some image files that are encrypted with RNCryptor and I want to decrypt them and use them as a background of a Framelayout. I walk through sample files, but my application force closes after a while and didn't show any images. I use JNCryptor library

I put encrypted image in raw folder as below:

here my project folder

and this is my code in my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FrameLayout img = (FrameLayout) findViewById(R.id.scrollView);

    final String password = "myPasswordString";

    JNCryptor cryptor = new AES256JNCryptor();

    Resources res = getResources();
    InputStream in_s = res.openRawResource(R.raw.troodon_ph);
    byte[] b = null;
    byte[] data = null;
    try {
        b = new byte[in_s.available()];
        in_s.read(b);
    } catch (IOException e) {
        Log.i("decrypt error", e.toString());
    }

    try {
        data = cryptor.decryptData(b, password.toCharArray());
    } catch (InvalidHMACException e) {
        Log.i("decrypt error", e.toString());
    } catch (CryptorException e) {
        Log.i("decrypt error", e.toString());
    }

    Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap);

    img.setBackgroundDrawable(bDrawable);
}

This file works great on iOS with this code:

NSString  *imagePath = [[NSBundle mainBundle] pathForResource:getName ofType:@"wod"];
NSData *encryptedData = [NSData dataWithContentsOfFile:imagePath];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
                                    withPassword:PASSWORD
                                           error:&error];

UIImage*  bgImage = [UIImage imageWithData:decryptedData];
UIImageView * movingImageView = [[UIImageView alloc]initWithImage:bgImage];

And here is logcat output:

08-28 19:52:11.720: E/AndroidRuntime(1063): FATAL EXCEPTION: main 08-28 19:52:11.720: E/AndroidRuntime(1063): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.appersian.android.wod/net.appersian.android.wod.MainActivity}: java.lang.NullPointerException 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.os.Looper.loop(Looper.java:137) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 19:52:11.720: E/AndroidRuntime(1063): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 19:52:11.720: E/AndroidRuntime(1063): at java.lang.reflect.Method.invoke(Method.java:511) 08-28 19:52:11.720: E/AndroidRuntime(1063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 19:52:11.720: E/AndroidRuntime(1063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 19:52:11.720: E/AndroidRuntime(1063): at dalvik.system.NativeStart.main(Native Method) 08-28 19:52:11.720: E/AndroidRuntime(1063): Caused by: java.lang.NullPointerException 08-28 19:52:11.720: E/AndroidRuntime(1063): at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:54) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.Activity.performCreate(Activity.java:5008) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 19:52:11.720: E/AndroidRuntime(1063): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 19:52:11.720: E/AndroidRuntime(1063): ... 11 more

What am I doing wrong about decrypting the file?

update: i added e.printStackTrace() to try/catch, and here is my new logcat:

08-28 20:54:10.496: W/System.err(1487): org.cryptonode.jncryptor.InvalidHMACException: Incorrect HMAC value. 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:244) 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:319) 08-28 20:54:10.496: W/System.err(1487): at org.cryptonode.jncryptor.AES256JNCryptor.decryptData(AES256JNCryptor.java:276) 08-28 20:54:10.496: W/System.err(1487): at net.appersian.android.wod.MainActivity.onCreate(MainActivity.java:50) 08-28 20:54:10.496: W/System.err(1487): at android.app.Activity.performCreate(Activity.java:5008) 08-28 20:54:10.496: W/System.err(1487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.access$600(ActivityThread.java:130) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 08-28 20:54:10.496: W/System.err(1487): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 20:54:10.496: W/System.err(1487): at android.os.Looper.loop(Looper.java:137) 08-28 20:54:10.496: W/System.err(1487): at android.app.ActivityThread.main(ActivityThread.java:4745) 08-28 20:54:10.496: W/System.err(1487): at java.lang.reflect.Method.invokeNative(Native Method) 08-28 20:54:10.496: W/System.err(1487): at java.lang.reflect.Method.invoke(Method.java:511) 08-28 20:54:10.496: W/System.err(1487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-28 20:54:10.496: W/System.err(1487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-28 20:54:10.496: W/System.err(1487): at dalvik.system.NativeStart.main(Native Method)


Source: (StackOverflow)

Encrypt 8 byte string / base64 encode / max length 20 bytes [closed]

What is the best method/approach to encrypt and base64 encode a UTF String that has a max size of 20 bytes in objective-c? I'm leaning towards Blowfish because of the 8 byte block size.

Please provide some sample code? Thanks.


Source: (StackOverflow)

AFNetworking + RNCryptor - decrypt data before JSON parse

How are you?

I'm trying to use RNCryptor to encrypt my transactions between the iOS devices and the webserver. For that I'm using RNCryptor on iOS and PHP, and AFNetworking to communicate from iOS to WebServer.

How do I decrypt data received from AFNetworking before it's been parsed to JSON response object?


To synthesize, I have:

  • Yii Framework, server side, PHP
  • AFNetworling, client side, Objective-C
  • RNCryptor, both sides

From iOS to PHP: I can decrypt data before using it.

From PHP to iOS: AFNetworking do not decrypt data before using it.


My PHP code is something like that:

RNCryptorHelper::init();
$encryptor = new \RNCryptor\Encryptor;

$data['Model1'] = Model1::model()->findAll();
$data['Model2'] = Model2::model()->findAll();

// EDITED TO REMOVE THIS HEADER
// header('Content-type: application/json');
echo $encryptor->encrypt(CJSON::encode($data), Yii::app()->params['cryptPassword']);
Yii::app()->end();

That outputs something like that:

AwFQ9+OfsHyXcSPynCrtveF7MQupQ+urd/VYeNMmt6OMxd6MhsDz4nxapvS1kVEHHbBZ4xLHqN7hTUDZos1LTWhB/CyeYoWpZSDhyFeHu9wNlHcRie6KTaHv/h14krvfb2/GHMt3GhIFqnyo7UKy/d06


My Objective-C code is something like that:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager GET:[NSString stringWithFormat:@"%@/Sync", URL_ROOT]
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

         NSLog(@"%@", [RNDecryptor decryptData:responseObject withPassword:CRYPT_PASSWORD error:nil]);

     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
     }];

That outputs the following error:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x15681970 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}


Thanks for your help!!


EDIT 1

I changed the code a little bit and now I can get the result:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager GET:[NSString stringWithFormat:@"%@/Sync", URL_ROOT]
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

         NSData *decryptedData = [RNDecryptor decryptData:[[NSData alloc] initWithBase64EncodedData:responseObject options:NSDataBase64DecodingIgnoreUnknownCharacters]
                                             withPassword:CRYPT_PASSWORD
                                                    error:nil];

         id json = [NSJSONSerialization JSONObjectWithData:decryptedData
                                                   options:kNilOptions
                                                     error:nil];

     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
     }];

Anyone has a hint on how to create a Response Serializer that automates that decrypt?


Source: (StackOverflow)

HMAC Mismatch Error Using PHP Decryptor for RNCryptor

I have an encrypted NSData variable that has been Base64 Encoded in Objective-C, and is being passed via POST to a PHP script. I am attempting to decrypt this string, but keep receiving a HMAC mismatch error. Here is my PHP code:

// back to binary
$bin_data = mb_convert_encoding($username_encrypted, "UTF-8", "BASE64");
// extract salt
$salt = substr($bin_data, 2, 8);
// extract HMAC salt
$hmac_salt = substr($bin_data, 10, 8);
// extract IV
$iv = substr($bin_data, 18, 16);
// extract data
$data = substr($bin_data, 34, strlen($bin_data) - 34 - 32);
// extract HMAC
$hmac = substr($bin_data, strlen($bin_data) - 32);

// make HMAC key
$hmac_key = pbkdf2('SHA1', $password, $hmac_salt, 10000, 32, true);
// make HMAC hash
$hmac_hash = hash_hmac('sha256', $data, $hmac_key, true);
// check if HMAC hash matches HMAC
if($hmac_hash != $hmac)
    exit("Error HMAC mismatch");

// make data key
$key = pbkdf2('SHA1', $password, $salt, 10000, 32, true);
// decrypt
$ret = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
echo trim(preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\xFF]/u', '', $ret));

function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
    $algorithm = strtolower($algorithm);
    if(!in_array($algorithm, hash_algos(), true))
        die('PBKDF2 ERROR: Invalid hash algorithm.');
    if($count <= 0 || $key_length <= 0)
        die('PBKDF2 ERROR: Invalid parameters.');

    $hash_length = strlen(hash($algorithm, "", true));
    $block_count = ceil($key_length / $hash_length);

    $output = "";
    for($i = 1; $i <= $block_count; $i++) {
        // $i encoded as 4 bytes, big endian.
        $last = $salt . pack("N", $i);
        // first iteration
        $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
        // perform the other $count - 1 iterations
        for ($j = 1; $j < $count; $j++) {
            $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
        }
        $output .= $xorsum;
    }

    if($raw_output)
        return substr($output, 0, $key_length);
    else
        return bin2hex(substr($output, 0, $key_length));
}

And this is my iOS code:

NSData *plain_data = [plain_string dataUsingEncoding:NSUTF8StringEncoding];
NSError *error_encryption = nil;
NSData *encrypted_data = [RNOpenSSLEncryptor encryptData:plain_data
                                            withSettings:kRNCryptorAES256Settings
                                                password:key
                                                   error:&error_encryption];

NSString *test_variable = [encrypted_data base64Encoding];

Any idea what is wrong?


Source: (StackOverflow)