简体   繁体   English

Java中的RSA加密:跨平台问题?

[英]RSA Encryption in Java: Cross Platform Issues?

The Situation 情况

I'm working with RSA encryption in Java. 我正在使用Java中的RSA加密。 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. 我正在尝试加密配备Cyanogenmod的Android 2.2的HTC Saphire(32B)开发人员手机上的数据,然后在运行Mandriva Linux 2010的64位服务器上解密所述数据。我在两台机器上使用相同的公钥,私钥对,可以正确加密/解密Android手机上的数据,可以正确加密/解密Linux服务器上的数据,但我无法加密手机上的数据,然后在服务器上解密。 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? 也许Java中的RSA算法有一些关于字大小的潜在假设?

Further information: 更多的信息:

  • 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. 我的加密密钥长度为2048位,但我看到了不同密钥大小的类似行为。
  • I have packaged my RSA encryption/decryption code into a jar file. 我已将RSA加密/解密代码打包到jar文件中。 It was compiled through Eclipse on the server's machine. 它是在服务器的机器上通过Eclipse编译的。
  • The program using the encryption library on the Android phone uses the above library. 使用Android手机上的加密库的程序使用上述库。 It too was built using Eclipse. 它也是使用Eclipse构建的。
  • The server program was built using Netbeans (as it was easier at the time to do so). 服务器程序是使用Netbeans构建的(因为当时这样做更容易)。

Other Questions 其他问题

  • Are there other free public-key encryption algorithms / libraries available for Java? 是否有其他可用于Java的免费公钥加密算法/库? Do they work cross-platform? 他们跨平台工作吗? What performance would one expect from them? 人们对他们的期望是什么? Etc., etc. I've looked into this and haven't found much; 等等我调查了这个并没有找到太多; perhaps I'm looking with the wrong keywords. 也许我正在寻找错误的关键词。

Phew! 唷! I think that's it. 我想就是这样。 Thanks for your help in advance! 感谢您的帮助!

RSA encryption (or any encryption algorithm) should work regardless of environment. 无论环境如何,RSA加密(或任何加密算法)都应该有效。 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. 确保在执行加密和解密时不仅要完全指定算法,还要指定操作模式(CBC等)和填充。 If that doesn't work, I suggest posting your code from both the device and the server so we can examine it more closely. 如果这不起作用,我建议从设备和服务器发布您的代码,以便我们可以更仔细地检查它。

Edit To address your question, in Java, when you get a cipher from the crypto package, you usually do so with the following code: 编辑为了解决您的问题,在Java中,当您从加密包中获得密码时,通常使用以下代码执行此操作:

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

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. 提供给getInstance的字符串指示运行时获取将使用AES算法,密码块链操作模式和PKCS5填充的密码实例。 There are a number of supported algorithms and paddings. 有许多支持的算法和填充。 I would check out this document from Oracle for more information about encryption in Java. 将从Oracle查看此文档,以获取有关Java加密的更多信息。

To be more specific, the string you use to request a cipher is in the format 更具体地说,用于请求密码的字符串采用格式

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

To make matters worse, despite Java providing a number of algorithms, modes of operation and paddings, not all of them will work together. 更糟糕的是,尽管Java提供了许多算法,操作模式和填充,但并非所有算法都可以协同工作。 You will need to read the documentation to find a configuration string that will work. 您需要阅读文档以查找可行的配置字符串。

也许你应该对数据进行校验和,并确保它正是你想要传递给加密/解密API的。

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

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