Android RSA解密(失败)/服务器端加密(openssl_public_encrypt) [英] Android RSA decryption (fails) / server-side encryption (openssl_public_encrypt)

查看:110
本文介绍了Android RSA解密(失败)/服务器端加密(openssl_public_encrypt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的设备上生成的RSA密钥来解密我的Android应用程序中的一个字符串。加密由php服务完成,使用我的应用程序提供的公共rsa密钥。我的问题是解密,这是失败的。

I am trying to decrypt a string in my android application using RSA keys generated on my device. The encryption is done by a php service, using the public rsa key provided by my application. My problem is with decryption, which fails.

我正在做以下操作:


  • 在Android上生成KeyPair(使用KeyPairGenerator.getInstance(RSA)) - > OK

  • Generating KeyPair on Android (with KeyPairGenerator.getInstance("RSA")) -> OK

两个密钥(公有和私有)都被保存在使用Base64.encode(pubKey.getEncoded())编码的base64之后转换为文件,并且与私钥相同。 - > OK

Both keys (public and private) are saved into files after being "base64" encoded with Base64.encode(pubKey.getEncoded()) and the same with the private key. -> OK

当我打电话给我的网络服务时,我通过一个post变量 - > OK

When I am calling my webservice, I pass my public key (in base 64) in a post variable -> OK

Web服务(php服务)使用公钥加密一个短字符串,并带有openssl_public_encrypt函数。加密的字符串转换为base64。 - >似乎OK,该函数不返回FALSE。

The web service (a php service), uses the public key to encrypt a short string, with the openssl_public_encrypt function. The encrypted string is converted to base64. -> Seems OK, the function does not return FALSE.

应用程序检索服务结果并对其进行解码(Base64.decode()) - > OK(我已经检查,接收到的字节与openssl_public_encrypt()函数生成的字节匹配)

The application retrieves the result of the service, and decodes it (Base64.decode()) -> OK (I have check, the bytes received matches with the one generated by the openssl_public_encrypt() function)

最后一件事是解密该字符串,I我正在做以下操作: - > NOT OK

The last thing is to decrypt this string, I am doing the following : -> NOT OK

密码密码= Cipher.getInstance(RSA);

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.DECRYPT_MODE,privateKey);

cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte [] decryptedBytes = cipher.doFinal(cryptedBytes);

byte[] decryptedBytes = cipher.doFinal(cryptedBytes);

String decryptzedString = new String(decryptptedBytes);

String decryptedString = new String(decryptedBytes);

System.out.println(decryptptedString);

System.out.println(decryptedString);

解密结果与我的原始字符串不符。

The result of the decryption does not match my original string.

我缺少某些东西?

推荐答案

OpenSSL默认使用 padding = OPENSSL_PKCS1_PADDING 所以要在双方使用相同的填充机制,你应该使用 Cipher.getInstance(RSA / ECB / PKCS1Padding)。这也是您可以在Java SE中使用的。

OpenSSL uses padding = OPENSSL_PKCS1_PADDING by default. So to have the same padding mechanism at both sides you should use Cipher.getInstance("RSA/ECB/PKCS1Padding"). This is also what you could use in Java SE.

请注意,依赖于加密中的默认操作模式是非常危险的。许多实现具有不同的默认值,那些可能难以查找。所以总是尝试完全指定使用的算法/模式。

Note that it is very dangerous to depend on default modes of operation in cryptography. Many implementations have different defaults, and those can be hard to look up. So always try to fully specify the algorithm/mode to use.

您可以尝试其他的RSA填充模式,但请注意 - 不幸的是,Android已禁用了大量算法,来自他们修改的Bouncy Castle源代码的别名。

You could try other RSA padding modes, but note that - unfortunately - Android has disabled a lot of algorithms and aliases from the Bouncy Castle source code they adapted.

这是一个旧的答案,强烈建议OAEP填充现在或使用RSA-KEM的混合加密技术。

This is an old answer, OAEP padding is strongly advised by now, or hybrid cryptography using RSA-KEM.

这篇关于Android RSA解密(失败)/服务器端加密(openssl_public_encrypt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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