iOS symmetric key encryption / decryption equivale

2020-07-31 04:52发布

问题:

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.

回答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).



回答2:

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