简体   繁体   English

如何从Java Cipher加密的文件中解析objective-c

[英]How can I decrypt in objective-c from a file encrypted by Java Cipher

I have an encrypt method in Java. 我在Java中有一个加密方法。

public static String encrypt(String orignal){
    SecretKeySpec key = new SecretKeySpec(keyString.getBytes(), "AES");
    IvParameterSpec initalVector = new IvParameterSpec(initialVectorParam.getBytes());

    try{
        Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
        /////////////// encrypt /////////////////
        cipher.init(Cipher.ENCRYPT_MODE, key, initalVector);
        Log.d("AES", "oriByte: "+ orignal.getBytes());
        int length = orignal.length();
        for(int i=0; i<length; i++){

        }
        byte[] test = cipher.doFinal(orignal.getBytes());
        Log.d("AES", "encByte: "+ test);
        return bytes2Hex(test);
    }catch (Exception e) {
        Log.d("AES", "Encrypt Exception:"+orignal);
        return "";
    }
}

For compatibility with PHP, I use "AES/CFB8/NoPadding" options. 为了与PHP兼容,我使用“AES / CFB8 / NoPadding”选项。 In PHP: $sCipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $sKey, $sStr, MCRYPT_MODE_CFB, $sIV); 在PHP中:$ sCipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ sKey,$ sStr,MCRYPT_MODE_CFB,$ sIV);

And I have a Objective-c Cipher code from here. 我有一个Objective-c Cipher代码。 https://gist.github.com/838614 https://gist.github.com/838614

I found that there is no IvParameterSpec in Objective-c Cipher like java. 我发现在Objective-c Cipher中没有像java这样的IvParameterSpec。 Besides, the getBytes method returns a different value with java. 此外,getBytes方法使用java返回不同的值。 (I think this is because java uses different encoding way.) (我认为这是因为java使用不同的编码方式。)

So, how can I apply IvParameterSpec in Objective-c. 那么,我如何在Objective-c中应用IvParameterSpec。 And is there any way to get 'getBytes' value like java in Objective-c? 有没有办法在Objective-c中获得像'java'这样的'getBytes'值?

For the initialization vector, see line 24 in your pastie: 有关初始化向量,请参阅pastie中的第24行:

NULL /* initialization vector (optional) */,

That's where you would pass your IV. 那就是你通过IV的地方。

But without knowing the string encoding the Java code used to create the bytes used as the IV, you won't be able to seed the encryption properly to decrypt the data, even if you know what the string displays to the screen as. 但是,如果不知道用于创建用作IV的字节的Java代码的字符串编码,即使您知道字符串在屏幕上显示的内容,您也无法正确地对加密进行种子密封以解密数据。 Put another way, just because the IV looks like "abc123" doesn't mean the bytes Java is writing to the IV buffer are going to be the same bytes you'll get if you strncpy() from a C character literal buffer. 换句话说,仅仅因为IV看起来像“abc123”并不意味着Java写入IV缓冲区的字节将与从C字符文字缓冲区strncpy()得到的字节相同。 You have to agree on the encoding as part of the protocol for handling the data. 您必须同意编码作为处理数据的协议的一部分。

You will also need to agree on a key size. 您还需要就密钥大小达成一致。 Your Java code does not specify how many bits are in the AES key. 您的Java代码未指定AES密钥中的位数。

Once you've got that worked out, you'll want to use a call like: 一旦你得到了解决方案,你就会想要使用如下调用:

const void *key = /* KEY BYTES */;
const void *iv = /* IV BYTES */;
const void *text = /* CIPHER TEXT */;
size_t textlen = /*...*/;
size_t outlen = 0;
(void)CCCrypt(kCCDecrypt, kCCAlgorithmAES128, 0/*use CBC mode*/,
        key, kCCKeySizeAES128, iv,
        text, textlen,
        &text, textlen, &outlen);

The plaintext will be written over the ciphertext, assuming all goes well. 假设一切顺利,明文将写在密文上。 The amount o data written to text during decryption will be stored in outlen . 在解密期间写入text数据量将存储在outlen Error checking is your responsibility; 错误检查是您的责任; the header is well-commented. 标题评论很好。

Once you have the data, you'll want to slurp it into an NSString with the correct encoding ( +[NSString initWithData:encoding:] would work), and then you have a string you can work with from Obj-C like any other string. 一旦你有了数据,你就会想要用正确的编码(或+[NSString initWithData:encoding:]可以工作)将它插入一个NSString ,然后你就可以使用Obj-C中的一个字符串了串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM