1

I am trying to encrypt/decrypt data ios to java & java to ios but I data encrypted in java is not properly decrypted in ios & data encrypted in ios is not properly decrypted in java

- (NSData *) encrypt:(NSData *) dataToEncrypt symmetricKey:(NSData *)symmetricKey context:(CCOperation)encryptOrDecrypt{
    NSUInteger data_length= [dataToEncrypt length];
    uint8_t input_raw_data[data_length];

    //The [dataToEncrypt length] gives the number of chars present in the string.So say there are 10 chars.
    //Now,the getBytes needs to get the raw bytes from this i.e. binary NSData.But suppose the encoding was
    //full 16 bit encoding then the number of bytes needed wd have been double- 20.But as we are using the
    //NSUTF8StringEncoding,the number of byes needed is 1 per char as the chars(even if originally unicode are
    //compressed into an 8 bit UTF8 encoding.)

    [dataToEncrypt getBytes:&input_raw_data length:data_length];

//    [dataToEncrypt getBytes:&input_raw_data maxLength:data_length usedLength:NULL encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0,data_length) remainingRange:NULL];

    //According to the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t buffer_size           = data_length + kCCBlockSizeAES128;
    void* buffer                 = malloc(buffer_size);
    size_t num_bytes_encrypted   = 0;


    CCCryptorStatus crypt_status = CCCrypt(encryptOrDecrypt, kCCAlgorithmAES128, 0x0000,
                                           [symmetricKey bytes], kCCKeySizeAES128,
                                           NULL,
                                           input_raw_data, data_length,
                                           buffer, buffer_size,
                                           &num_bytes_encrypted);

//    NSLog(@"~~num bytes encrypted: %d",num_bytes_encrypted);
    if (crypt_status == kCCSuccess){
        NSLog(@"~~Data encoded successfully...");
        return [NSData dataWithBytesNoCopy:buffer length:num_bytes_encrypted];
    }

    free(buffer); //free the buffer;
    return nil;

}

I have used this

Java Code -

 Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
 String keyString = "keykeykeykeykeykeykeykey";
 byte[] keyBytes = keyString.getBytes("UTF-8"); 
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new IvParameterSpec(new byte[16])); 
byte[] resultBytes = cipher.doFinal("Hallo Welt!".getBytes("UTF8")); 
FileOutputStream out = new FileOutputStream(new File("encryptedFileJava")); 
out.write(resultBytes); out.close();

and this is encrypted text - “Se áJbë|8”R ,

key - BW3dKDf2bkDC4Bq9xTdr1g==

Please help me or suggest me any solution.

Thank you.

3
  • You are using AES in ECB mode here. Is that on purpose? Please show us the Java code you are trying to use to decrypt it. Showing us a example of input-data + key + encrypted-data + decrypted-data would also help us debug your problem. Commented Jun 11, 2013 at 13:24
  • @RasmusFaber hi this is sample code for java Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); String keyString = "keykeykeykeykeykeykeykey"; byte[] keyBytes = keyString.getBytes("UTF-8"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new IvParameterSpec(new byte[16])); byte[] resultBytes = cipher.doFinal("Hallo Welt!".getBytes("UTF8")); FileOutputStream out = new FileOutputStream(new File("encryptedFileJava")); out.write(resultBytes); out.close(); and this is encrypted text - “Se áJbë|8”R , key - BW3dKDf2bkDC4Bq9xTdr1g== Commented Jun 12, 2013 at 7:39
  • possible duplicate of Decrypting data that was AES encrypted with Objective-C with Java Commented Jun 12, 2013 at 8:21

2 Answers 2

1

You have at least two problems:

  1. The Objective C code is using ECB mode, while the Java code is using CBC mode. Use a bytearray of zeroes instead of NULL in the CCrypt-invocation to use CBC mode with a zero IV like the Java code.

  2. Since keyBytes is 24 bytes long, Java will use AES-192. CCrypt will just ignore the extra bytes. Either specify AES-192 to CCrypt or use a 128 bit key ("keykeykeykeykeyk" should work).

Sign up to request clarification or add additional context in comments.

1 Comment

CBC mode with NULL specified for the iv will use an array of 0x00. From the Apple header: "If CBC mode is selected (by the absence of any mode bits in the options flags) and no IV is present, a NULL (all zeroes) IV will be used." Consider correcting the answer.
0

For secured communication between IOS and Java devices, symmetric key encryption can be used.

In such cases where the platforms are different it is advisable that the key generated is a plain text key and not a API generated key.

AES 128 bit encryption can be used in such cases. IOS devices are capable of generating a symmetric key and encrypting the text using AES encryption.

Below link provides the java code to encrypt and decrypt using plain text symmetric key

http://www.java-redefined.com/2015/06/symmetric-key-encryption-ios-java.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.