iOS对称密钥加密/解密等同于Java [英] iOS symmetric key encryption / decryption equivalent to Java

查看:179
本文介绍了iOS对称密钥加密/解密等同于Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加密/解密数据ios到java& java到ios,但是在java中加密的I数据在ios&数据加密在ios没有在java中正确解密

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;

}

我使用了这个

 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ë| 8R,

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

键 - BW3dKDf2bkDC4Bq9xTdr1g ==

key - BW3dKDf2bkDC4Bq9xTdr1g==

请帮助我或建议我任何解决方案。

Please help me or suggest me any solution.

谢谢。

推荐答案


  1. Objective C代码使用ECB模式,而Java代码使用CBC模式。

  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.

由于keyBytes长度为24个字节,因此在CCrypt调用中使用零字节代替NULL而不是NULL来使用CBC模式。 ,Java将使用AES-192。 CCrypt将忽略额外的字节。指定AES-192到CCrypt或使用128位密钥(keykeykeykeykey应该工作)。

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

这篇关于iOS对称密钥加密/解密等同于Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆