使用Java对使用Objective-C进行AES加密的数据进行解密 [英] Decrypting data that was AES encrypted with Objective-C with Java

查看:159
本文介绍了使用Java对使用Objective-C进行AES加密的数据进行解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解密最初用Java中的Objective-C加密的数据。

I try to decrypt data that was originally encrypted with Objective-C in Java.

还有其他问题,但他们真的很混乱,许多都没有解决,因此我会发布自己的。

There are other questions mentioning this but they are really cluttered and many of them aren't solved yet therefore I will post my own.

这是加密数据的代码:

  - (int) encryptWithKey: (NSString *) key
    {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char * keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
    bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding)

    // fetch key data
    [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];

    // encrypts in-place, since this is a mutable data object
    size_t numBytesEncrypted = 0;
    CCCryptorStatus result = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, 
                                     keyPtr, kCCKeySizeAES128,
                                     NULL /* initialization vector (optional) */, 
                                     [self mutableBytes], [self length], /* input */
                                     [self mutableBytes], [self length]+32, /* output */
                                     &numBytesEncrypted );
    return numBytesEncrypted;
}

我执行此函数并将结果数据写入光盘,

I execute this function and write the resulting data to the disc with this code:

NSString* strTest = @"Hallo Welt!";
NSLog(@"strTest = %@", strTest);

NSMutableData *protectedData = [NSMutableData dataWithData:[strTest dataUsingEncoding:NSUTF8StringEncoding]];

int laenge = [protectedData encryptWithKey:@"keykeykeykeykeykeykeykey"];

NSData* dataOutput = [[NSData alloc] initWithBytes:[protectedData bytes] length:laenge];


[dataOutput writeToFile:@"/encryptedFileObjC" atomically:YES];

在Java中,我使用此代码尝试实现相同的行为:

In Java I use this code to try to achieve the same behavior:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
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();



如果我现在尝试解密通过Objective-C加密的文件,我得到一个坏的填充异常。如果我打开这两个文件与加密的内容,他们是不同的:

If I now try decrypting the file that was encrypted via Objective-C I get a bad padding Exception. If I open the two files with the encrypted content they are different:

Hallo Welt!用Java加密: 96 C5 CB 51 39 B5 27 FB B3 93 BF 92 18 BB 16 9B

Hallo Welt!用ObjC加密: A3 61 32 8E A5 E6 66 E0 41 64 89 25 62 D3 21 16

Hallo Welt! encrypted with Java: 96 C5 CB 51 39 B5 27 FB B3 93 BF 92 18 BB 16 9B
Hallo Welt! encrypted with ObjC: A3 61 32 8E A5 E6 66 E0 41 64 89 25 62 D3 21 16

t文件内容是否相同?我想我没有得到所有的算法的参数在两种语言相同。

Shouldn't the file content be the same? I think I don't got all the parameters for the algorithm the same in the two languages.

我需要改变Java代码以获得与Objective-C代码相同的结果,以便能够解密用Objective-C加密的一些数据。 p>

I need to alter the Java Code to get the same result as the Objective-C Code to be able to decrypt some data that was encrypted with Objective-C.

推荐答案


  1. 我不会假设CCCrypt支持对输入和输出使用相同的数组。尝试使用两个不同的数组。

  2. 您必须自己调整输出数组的大小(调用后numBytesEncrypted应等于16)。

  3. 正如我可以看到,一个空IV信号使用ECB加密而不是CBC。

只要您的输入小于15个字节,它就不会产生任何差异, EDIT:另一个问题:

Another issue:


  1. 您使用的是24字节的密钥。 AES-128需要128位= 16字节的密钥,AES-192需要192位= 24字节的密钥,AES-256需要256位= 32字节的密钥。您显式指示AES-128到CCCrypt,这意味着它忽略密钥的最后8个字节。您只是将AES指示为Java,这意味着它会查看键大小以决定要使用哪个AES变量。由于您提供了一个24字节的密钥,它使用AES-192。修复它,所以两端使用相同的算法,你应该是好的。

这篇关于使用Java对使用Objective-C进行AES加密的数据进行解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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