RSA加密:Java和Android之间的区别 [英] RSA Encryption: Difference between Java and Android

查看:549
本文介绍了RSA加密:Java和Android之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RSA加密Android上的用户名和密码,并在服务器上解密(tomcat 6,java 1.6)。
Android加密:

  PublicKey pubKey = readPublicKeyFromFile(mod,ex); 
密码密码= Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE,pubKey);
byte [] cipherData = cipher.doFinal(data);
return cipherData;

Java Tomcat解密:

  PrivateKey pubKey = readPrivateKeyFromFile(mod,ex); 
密码密码= Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE,pubKey);
byte [] cipherData = cipher.doFinal(data);
return cipherData;

如果我使用android部分OUTSIDE android(只是在一个主要的方法)它工作正常。但不在我的android(Emulator)里面。在服务器端我得到以下错误:

  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 * ..)
在com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13 * ..)
在javax.crypto.Cipher.doFinal(DashoA13 * ..)

我将mod和ex保存为BigIntegers常量,因此我不会将它们写入文件。
我知道java1.6和java 1.5加密有区别,所以都用java 1.6编译。



有些调试信息:



在android调试期间,我可以看到pubKey包含十六进制的模数和指数。如果我在一个主要的方法中调试(再次是相同的代码),我可以看到pubKey包含十进制中的模数和指数。



我做错了什么? >

谢谢

解决方案

我在Android 2.2+中进行RSA加密,并在tomcat 6 java 1.6服务器。



我正在得到这个确切的问题,阅读全部,部分是由于@Femi的答案我遇到了我需要的。



解决方案是使用以下算法规范的密码:

 密码密码= Cipher.getInstance(RSA / ECB / PKCS1Padding); 

这可以从Android和BlackBerry智能手机进行加密。我知道这个问题已经过了四个月了,只是为了防止别人遇到这个问题。


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 Decryption:

    PrivateKey pubKey = readPrivateKeyFromFile(mod, ex);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    byte[] cipherData = cipher.doFinal(data);
    return cipherData;

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*..)

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.

Some debug info:

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.

What am I doing wrong?

Thanks

解决方案

Im doing RSA Encrypt in Android 2.2+ and decrypt on a tomcat 6 java 1.6 server.

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.

The solution was to use the folowing algorithm specification for the Cipher:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

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屋!

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