iPhone如何加密AES256输出32字节十六进制字符串?

发布于 2024-11-06 18:29:58 字数 1933 浏览 5 评论 0原文

亲爱的大师们, 我在带有充气城堡库的java上有代码,如下所示:

private static String AES (boolean encrypt, String inputString, String keyString) {
        byte [] input, cipherText;
        int outputLen;

            BufferedBlockCipher cipher = new BufferedBlockCipher(new AESEngine());
        input = HexString.hexToBytes(inputString);

        cipher.init (encrypt, new KeyParameter (HexString.hexToBytes(keyString)));
        cipherText = new byte[cipher.getOutputSize(input.length)];
        outputLen = cipher.processBytes( input, 0, input.length, cipherText, 0);

        try {
            cipher.doFinal (cipherText, outputLen);
        } catch (DataLengthException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidCipherTextException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return HexString.bytesToHex(cipherText);
    }

此输出方法如下:

screet:6a7573742074657374696e6700000000

key:6f2ca4106748c1eaeff88bdad4049285

结果: f1ecd6bc3109b1f2355be3a32dd49874

这是我在 IOS 上的代码:

NSLog( @"Screet     : %@", secret );
NSLog( @"Key        : %@", key );
NSString *encryptedString = [secret AES256EncryptWithKey:key];
NSLog( @"Result     : %@", encryptedString );
NSLog( @"Result Hex : %@", [NSString stringToHex:encryptedString] );

Screet:6a7573742074657374696e6700000000

密钥:6f2ca4106748c1eaeff88bdad4049285

结果: UBXp6lFnM+XAZvVeOuYkI/T4+brXyGBmzGOUMO+XrGS8MWGDVb0RrZGBHyw2l2Ml

结果十六进制: 55425870366c466e4d2b58415a7656654f75596b492f54342b6272587947426d 7a474f554d4f2b58724753384d57474456623052725a4742487977326c324d6c

我使用这个论坛的 NSData+AESCrpt 。 有没有建议为了结果是相同的ios和java(字符串上的32字节)? 感谢各位大师的帮助:)。

Dear Gurus,
I have code on java with bouncy castle lib, like this :

private static String AES (boolean encrypt, String inputString, String keyString) {
        byte [] input, cipherText;
        int outputLen;

            BufferedBlockCipher cipher = new BufferedBlockCipher(new AESEngine());
        input = HexString.hexToBytes(inputString);

        cipher.init (encrypt, new KeyParameter (HexString.hexToBytes(keyString)));
        cipherText = new byte[cipher.getOutputSize(input.length)];
        outputLen = cipher.processBytes( input, 0, input.length, cipherText, 0);

        try {
            cipher.doFinal (cipherText, outputLen);
        } catch (DataLengthException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidCipherTextException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return HexString.bytesToHex(cipherText);
    }

this output methode like this :

screet : 6a7573742074657374696e6700000000

key : 6f2ca4106748c1eaeff88bdad4049285

Result : f1ecd6bc3109b1f2355be3a32dd49874

and this is my code on IOS :

NSLog( @"Screet     : %@", secret );
NSLog( @"Key        : %@", key );
NSString *encryptedString = [secret AES256EncryptWithKey:key];
NSLog( @"Result     : %@", encryptedString );
NSLog( @"Result Hex : %@", [NSString stringToHex:encryptedString] );

Screet : 6a7573742074657374696e6700000000

Key : 6f2ca4106748c1eaeff88bdad4049285

Result : UBXp6lFnM+XAZvVeOuYkI/T4+brXyGBmzGOUMO+XrGS8MWGDVb0RrZGBHyw2l2Ml

Result Hex : 55425870366c466e4d2b58415a7656654f75596b492f54342b6272587947426d7a474f554d4f2b58724753384d57474456623052725a4742487977326c324d6c

I use NSData+AESCrpt from this forum.
is there any suggestion that in order the result is same ios and java (32 Byte on String) ?
Thanks for gurus help :).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文