iOS对称密钥加密/解密等同于Java

时间:2013-06-11 11:05:50

标签: java objective-c security cryptography

我正在尝试加密/解密数据ios到java& java到ios但我在java中加密的数据在ios&中没有被正确解密在ios中加密的数据在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代码 -

 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();

这是加密文本 - “SeáJbë| 8”R,

键 - BW3dKDf2bkDC4Bq9xTdr1g ==

请帮帮我或建议我解决。

谢谢。

2 个答案:

答案 0 :(得分:1)

你至少有两个问题:

  1. Objective C代码使用ECB模式,而Java代码使用CBC模式。在CCrypt调用中使用零字节而不是NULL来使用带有零IV的CBC模式,如Java代码。

  2. 由于keyBytes长度为24个字节,因此Java将使用AES-192。 CCrypt将忽略额外的字节。指定AES-192到CCrypt或使用128位密钥(“keykeykeykeykeyk”应该工作)。

答案 1 :(得分:0)

对于IOS和Java设备之间的安全通信,可以使用对称密钥加密。

在平台不同的情况下,建议生成的密钥是纯文本密钥而不是API生成密钥。

在这种情况下可以使用AES 128位加密。 IOS设备能够生成对称密钥并使用AES加密对文本进行加密。

下面的链接提供了使用纯文本对称密钥加密和解密的java代码

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