如何在Java中解密从PHP加密的反应? [英] How to decrypt in Java an encrypted response from PHP?

查看:113
本文介绍了如何在Java中解密从PHP加密的反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我打算在加密PHP JSON响应,而一旦Android应用程序接收到响应,它是解密

Goal: I intend to encrypt a JSON response in PHP, and once the android app receives that response, it decrypts it.

我能找到在PHP中的加密和解密功能:

I was able to find the Encryption and Decryption functions in PHP:

<?php
   function simple_encrypt($text)
    {
    $salt = "anything";
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
    }

    function simple_decrypt($text)
    {
        $salt = "anything";
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    }
?>

(我实际上并不需要的 simple_decrypt 功能,我只是测试它。我确实需要简单地说就是在服务器端加密,并在客户端解密(我的Andr​​oid应用程序))

(I do not actually need the simple_decrypt function, I'm just testing it. What I need actually is simply the encryption on server side, and decryption on client side ( my android app) )

所以,我需要类似simple_decrypt但用Java编写的,所以我可以解密我的回应的功能。

So I need a function similar to the simple_decrypt but written in Java so I can decrypt my response.

我不知道该怎么做,因为我很新的加密/解密。我希望在这里有专家可以指导我合适的解决方案。

I have no idea how to do it as I'm very new to Encryption/Decryption. I hope some experts here can guide me to the appropriate solution.

推荐答案

下面是一些PHP / Java的code,我发现 - 这是你的PHP code的改进,因为它使用的加密反馈模式(CFB),这比更安全ECB(电子codebook)。要在Java中使用256位AES你需要安装Java加密扩展上的此页面。注意,无论是PHP和Java code将需要使用相同的初始化向量 - 这应该是唯一的每个密文,但可以在明文传送

Here is some PHP/Java code I found - this is an improvement on your PHP code because it uses Cipher Feedback Mode (CFB), which is more secure than ECB (Electronic Codebook). To use 256-bit AES in Java you'll need to install the Java Cryptography Extension, available on the bottom of this page. Note that both the PHP and Java code will need to use the same initialization vector - this should be unique to each ciphertext, but can be transmitted in plaintext.

如果你使用的是SSL,这是没有必要的 - 你只需要做到这一点,如果将数据从PHP去一个数据库/文件/其他一些持久存储,然后到Java

If you're using SSL then this isn't necessary - you only need to do this if the data is going from PHP to a database / file / some other persistent storage and then to Java.

这篇关于如何在Java中解密从PHP加密的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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