简体   繁体   English

Android / PHP RSA加密/解密?

[英]Android/PHP RSA encryption/decryption?

I would like to encrypt a String on Android and decrypt it with PHP (and vice versa). 我想在Android上加密一个String,然后用PHP解密(反之亦然)。

The problem is the key generation. 问题是密钥生成。 What is recommended? 推荐什么? Should I create it on Android or with openssl with PHP? 我应该在Android上还是在带有PHP的openssl上创建它? I tried the phpseclib. 我尝试了phpseclib。

Creation of the key worked well but when I tried to load the key with BC on Android, it said: InvalidkeyException. 密钥的创建工作良好,但是当我尝试在Android上用BC加载密钥时,它说:InvalidkeyException。 It was a PKCS1 key with sha1 in PEM Format. 这是PEM格式的带有sha1的PKCS1密钥。 I am talking about the public key. 我说的是公钥。

Can you help me to generate and load a key? 您能帮我生成和加载密钥吗?

You should always generate the key pair on the device that should store the private key value (in your case, that would be the server containing the PHP code). 您应该始终在应该存储私钥值的设备上生成密钥对(在您的情况下,这将是包含PHP代码的服务器)。 Otherwise you need to load the key into that device, and to do that securely you may need another key (etc.). 否则,您需要将密钥加载到该设备中,并且要安全地执行此操作,您可能需要另一个密钥(等)。 You should still make sure that the public key gets distributed to the clients while considering man-in-the-middle attacks (injecting another public key, for instance). 在考虑中间人攻击(例如注入另一个公共密钥)时,您仍应确保将公共密钥分发给客户端。

To load the key on Android you need to remove the PEM format/ASCII armor. 要在Android上加载密钥,您需要删除PEM格式/ ASCII防护。 You can do this by using the Bouncy Castle libraries, or - for Android - the Spongy Castle libraries which contain PEM encoding / decoding utilities . 您可以使用Bouncy Castle库或(对于Android) 使用Spongy Castle库包含PEM编码/解码实用程序)来完成此操作

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

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