2

I am creating both an Android and iOS version of an app and have a piece of data that needs to be encrypted. I am using the JNCryptor/RNCryptor libraries for Android and iOS respectively because they are claimed to be compatible.

However, when I encrypt with Android, my encrypted key is 114 characters long, whereas with iOS it is 112 characters long. The only difference I noticed between the two library source codes is that Android uses PKCS5Padding whereas iOS uses PKCS7Padding. Is this significant, considering that the two libraries are apparently supposed to be compatible? If so, how do I go about changing this to make the two encrypted strings of equal length?

EDIT: Android Code:

    JNCryptor cryptor = new AES256JNCryptor();
    byte[] plaintext = data.getBytes();
    String password = key;
    String a;
    try {
        byte[] ciphertext = cryptor.encryptData(plaintext, password.toCharArray());

        a = Base64.encodeToString(ciphertext, Base64.DEFAULT);

       return a;


    } catch (CryptorException e) {
        // Something went wrong
        e.printStackTrace();

        return "0";
    }

iOS Code:

NSData *data = [@"mystring" dataUsingEncoding:NSUTF8StringEncoding];



NSError *error;

NSData *encryptedData = [RNEncryptor encryptData:data

                                    withSettings:kRNCryptorAES256Settings

                                        password:DEV_AES_KEY

                                           error:&error];



NSString *myotherstring = [encryptedData base64EncodedStringWithOptions:0];

Or did you mean to compare the two libraries? These are available here: https://github.com/RNCryptor/JNCryptor

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user3794585
  • 203
  • 1
  • 11
  • 3
    It is not significant. For all intents and purposes PKCS5Padding and PKCS7Padding are synonyms and are implemented identically. If you really want help you'll need to to provide a [short, self-contained, correct example](http://sscce.org/) of your two versions. – President James K. Polk Jul 22 '14 at 22:23
  • 1
    I just added two pieces of the code – user3794585 Jul 22 '14 at 22:41
  • 3
    Thanks. You want to avoid defaults. Really, avoid defaults everywhere. For example, `byte[] plaintext = data.getBytes();` is asking for trouble. Instead, specify an encoding like UTF-8, i.e. byte[] `plaintext = data.getBytes("UTF-8");`. Likewise for going the other way, from bytes to a string: use the `String(byte [] x, String charset)` constructor. – President James K. Polk Jul 22 '14 at 23:10
  • 1
    Hex dump the data and keys to make sure both methods are getting the same input. Also hex dump the output prior to Base64 encoding the data. It looks like `RNEncryptor` does not Base64 encode the output. Add all of this to your question. create the Android keying a separate step from `password.toCharArray()`, hex dump that. – zaph Jul 23 '14 at 01:59
  • When dealing with encryption and things are not working, drop back the the bare minimum encryption, put all pre-calculations/conversions in intermediate vars so there are no in-line calculations as inputs. Once that works add the bells & whistles. – zaph Jul 23 '14 at 02:06
  • Side note: this issue is also being separately discussed on GitHub (https://github.com/RNCryptor/JNCryptor/issues/19). – Duncan Jones Aug 04 '14 at 08:45

0 Answers0