RSA加密:Java和Android的区别 [英] RSA Encryption: Difference between Java and Android
问题描述
我使用RSA加密的用户名和密码在Android和解密他们在服务器上(的Tomcat 6,JAVA 1.6)。 Android的加密:
I am using RSA to encrypt username and password on Android and decrypt them on server (tomcat 6, java 1.6). Android Encryption:
PublicKey pubKey = readPublicKeyFromFile(mod, ex);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(data);
return cipherData;
Java的Tomcat的解密:
Java Tomcat Decryption:
PrivateKey pubKey = readPrivateKeyFromFile(mod, ex);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(data);
return cipherData;
如果我用的是android的部分外的Android(只是在main方法),它工作正常。但不是我的Android(仿真器)内。在德服务器端出现以下错误:
If I use the android part OUTSIDE android (Just in a main method) it works fine. But not inside my android (Emulator). On de server side I get the following error:
javax.crypto.BadPaddingException: Blocktype mismatch: 0
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:311)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
我把mod和前因为BigIntegers常数,所以我不把它们写在一个文件中。 我知道有java1.6和java 1.5加密之间的差异,因此两者都与Java 1.6编译。
I keep the mod and ex as BigIntegers constants so I don't write them in to a file. I know that there are difference between java1.6 and java 1.5 encryption, so both are compiled with java 1.6.
一些调试信息:
在调试Android中我可以看到PUBKEY包含系数和指数的十六进制。如果我调试的主要方法(再次同code)我可以看到PUBKEY包含系数和指数以十进制。
During debug in android I can see that pubKey contains modulus and exponent in hexadecimal. And if I debug in a main method (again the same code) I can see that pubKey contains modulus and exponent in decimal.
我是什么做错了吗?
感谢
推荐答案
Im做RSA加密的安卓2.2+和解密在一个Tomcat 6 Java 1.6的服务器。
Im doing RSA Encrypt in Android 2.2+ and decrypt on a tomcat 6 java 1.6 server.
我得到这个确切的问题,看完所有的地方,部分归功于@Femi的答案,我碰到我需要的东西。
I was getting this exact problem, reading all over the place and in part thanks to @Femi 's answer I came across what I needed.
解决的办法是使用如下因素的init的密码:
The solution was to use the folowing init for the Cipher:
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
该工程从Android和BlackBerry智能手机做加密。我知道它已经四个月有人问,但以防万一别人穿过这个问题。
This works doing encryption from both Android and BlackBerry smartphones. I know its been four months since the question was asked, but just in case someone else goes through this problem.
这篇关于RSA加密:Java和Android的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!