RSA 加密:Java 和 Android 之间的区别

发布于 2024-11-08 18:57:21 字数 1303 浏览 0 评论 0原文

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

    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 解密:

    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 部分(仅在主方法中),它可以正常工作。但不在我的 Android(模拟器)内。在服务器端,我收到以下错误:

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 和 ex 保留为 BigIntegers 常量,因此我不会将它们写入文件中。 我知道java1.6和java 1.5加密之间存在差异,因此两者都是用java 1.6编译的。

一些调试信息:

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

我做错了什么?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枯叶蝶 2024-11-15 18:57:21

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

我遇到了这个确切的问题,到处阅读,部分感谢@Femi 的回答,我找到了我需要的东西。

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

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

这适用于 Android 和 BlackBerry 智能手机的加密。我知道自提出问题以来已经过去了四个月,但以防万一其他人也遇到这个问题。

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.

养猫人 2024-11-15 18:57:21

我建议您使用特定的密码初始化:作为示例,

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

对两者都适用。您收到的异常 (BadPaddingException) 的发生是因为桌面 JVM 和 Android JVM 之间的默认密码初始化填充似乎不同。

I suggest you use specific cipher initialization: as an example,

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

will work on both. The exception you are getting (BadPaddingException) is occuring because the default cipher initialization padding appears to be different between the desktop JVM and the Android JVM.

山田美奈子 2024-11-15 18:57:21

首先,看起来您正在使用公钥初始化两个密码。加密使用公钥,解密使用私钥。我希望这只是一个错字。

我在 RSA 加密方面也遇到了很多麻烦,大部分都是反复试验。我建议您尝试其他提供商。我设法使用 BouncyCastle 实现 RSA。

Cipher wrapper = Cipher.getInstance("RSA", "BC");
wrapper.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedData= wrapper.doFinal(unencryptedData);

不过,我生成了自己的密钥对,因为这是会话加密。

kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();

Firstly, it looks like you're initializing both ciphers with the public key. Encryption uses public key, decryption used private key. I hope that's just a typo though.

I had a lot of trouble with RSA encryption as well, much was trial and error. I suggest you try another provider. I managed to implement RSA using BouncyCastle.

Cipher wrapper = Cipher.getInstance("RSA", "BC");
wrapper.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedData= wrapper.doFinal(unencryptedData);

Although, I generated my own keypair since this was a session encryption.

kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文