跨平台的问题:在Java的RSA加密? [英] RSA Encryption in Java: Cross Platform Issues?

查看:190
本文介绍了跨平台的问题:在Java的RSA加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的现状

我正与Java中RSA加密。我想对加密的HTC萨费尔(32B)开发的手机搭载的CyanogenMod的Andr​​oid 2.2的数据,然后解密上述运行在64位服务器上的数据的Mandriva Linux的2010年。我使用相同的公钥,私钥对在两台机器上,可以正确加密/解密的Andr​​oid手机上的数据,可以正确加密/解密在Linux服务器上的数据,但我无法加密手机上的数据,然后将其解密的服务器上。我得到不好的填充异常。我已经证实,该数据被发送正确地通过电话,正在由服务器正确分析。因此,我不明白,为什么解密失败。谁能帮我这个?也许RSA算法在Java中有大约字长一些基本的假设?

I'm working with RSA encryption in Java. I'm trying to encrypt data on an HTC Saphire (32B) developer phone equipped with Cyanogenmod's Android 2.2 and then decrypt said data on a 64 bit server running Mandriva Linux 2010. I am using the same public key, private key pair on both machines, can correctly encrypt/decrypt data on the Android phone, can correctly encrypt/decrypt data on the Linux server, but I am unable to encrypt data on the phone and then decrypt it on the server. I get bad padding exceptions. I have confirmed that the data is being sent correctly by the phone and is being parsed correctly by the server. As such, I cannot figure out why decryption fails. Can anyone help me with this? Perhaps the RSA algorithm in Java has some underlying assumption about word size?

更多信息:

  • 在我的加密/解密库是根据指南中找到这里
  • 在我的加密密钥是2048位的长度,但我看到不同的密钥大小类似的行为。
  • 我打包我的RSA加密/解密code到一个jar文件。它是通过Eclipse中的服务器的机器上编译。
  • 使用加密库在Android手机上的程序使用上述库。它也被使用Eclipse构建。
  • 服务器程序使用NetBeans(因为它在当时这样做更容易)建造的。
  • My encryption/decryption library is based on the guide found here.
  • My encryption key is 2048 bits in length, but I see similar behaviour with different key sizes.
  • I have packaged my RSA encryption/decryption code into a jar file. It was compiled through Eclipse on the server's machine.
  • The program using the encryption library on the Android phone uses the above library. It too was built using Eclipse.
  • The server program was built using Netbeans (as it was easier at the time to do so).

其他问题

  • 是否还有其他免费的公共密钥加密算法/库可用于Java?难道他们跨平台工作?将一个来自他们的期望是什么表现?等等,等等。我已经研究过这一点,并没有发现多少;也许是我期待与错误的关键词。

唷!我认为这是它。感谢您的帮助提前!

Phew! I think that's it. Thanks for your help in advance!

推荐答案

RSA加密(或任何加密算法)应该工作,无论环境。然而,有可能在某些系统做出关于默认填充和操作模式不同的假设。确保当您正在执行加密和解密,你完全指定不仅算法,而且操作方式(CBC等)和填充。如果还是不行,我建议张贴从设备和服务器都你code,所以我们可以更仔细地检查了。

RSA encryption (or any encryption algorithm) should work regardless of environment. However, it is possible that certain systems make different assumptions about default padding and the mode of operation. Make sure that when you are performing encryption and decryption that you fully specify not only the algorithm, but also the mode of operation (CBC, etc) and the padding. If that doesn't work, I suggest posting your code from both the device and the server so we can examine it more closely.

修改要解决你的问题,在Java中,当你从加密包中的密码,你通常这样做有以下code:

Edit To address your question, in Java, when you get a cipher from the crypto package, you usually do so with the following code:

Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

提供的的getInstance 的字符串指示运行时获得,将使用AES算法,操作密码块链接模式和PKCS5填充的密码实例。有许多支持的算法和填补处理的。我想看看从Oracle 本文件的详情,加密在Java中。

The string provided to getInstance instructs the runtime to get a cipher instance that will use the AES algorithm, the cipher block chaining mode of operation and the PKCS5 Padding. There are a number of supported algorithms and paddings. I would check out this document from Oracle for more information about encryption in Java.

具体而言,您使用请求加密的字符串格式为

To be more specific, the string you use to request a cipher is in the format

<algorithm>/<mode of operation>/<padding>

更糟糕的是,尽管Java的提供了一些算法,运行和垫的方式,不是所有的人将共同努力。您将需要阅读的文件发现,将工作一个配置的字符串。

To make matters worse, despite Java providing a number of algorithms, modes of operation and paddings, not all of them will work together. You will need to read the documentation to find a configuration string that will work.

这篇关于跨平台的问题:在Java的RSA加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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