Python 调用 Java 生成的 RSA 私钥加密结果不一样

2017-03-08 22:49:33 +08:00
 iamxiaoq

Java 代码

public static String encrypt(String paramString)throws Exception
{
    return Base64Utils.encode(RSAUtils.encryptByPrivateKey(paramString.getBytes(), "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ==");
}
encrypt('159742081') --> GBn6RGIeEp8j/n35CgT5DdMmrvvNBtVCFQikO0vfJaYYrpEzBZ/F+5PkFLpzLDtYvQrj0Q/x/Fdxz3BtbEdLq57WFxB5MvkFxerWVeplA2vdlD7m+dgjsWyxBSbcVV1QX3UBNp+T3DtxL6uGuWUNxucy9yB5TOD3xCNchzGCnSU=

Python 代码

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5, PKCS1_OAEP
from base64 import b64decode, b64encode


key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ=='

rsakey = RSA.importKey(b64decode(key))
cipher = PKCS1_OAEP.new(rsakey)
b = cipher.encrypt(b'159742081')
print(b64encode(b))

cipher = PKCS1_v1_5.new(rsakey)
b = cipher.encrypt(b'159742081')
print(b64encode(b))

输出: b'ZkSAKcf9aGbRaOUaywZEQKIS4jdYpn+dlSxwpRFlmwjcNmVM7Cp0GQzz9yY2g7UdPVJCubRuPbKmyale2cuAqsVu+Y1n79EMoxSbShys0/o1o8V4UCuV+jP3e3EnyT86yoTyTDMXQzgDS+SRSkEF9U6db19T12/xEtfcKRcHing='

b'YQz8gjcslQ8QsYs9eU+Yu2owf55gHVedIksovrMczCRSaQ8vhml1ua6bAmf6xvg7zlw5BGZ7KlTygIgqKDvf6JgdZh5k2OoiK8uGYCFFCJFY0+3ZjhytG4KfwujqbCQWu3rZSaFZeF76MdFBQUWzJ2Q3V1BFTljF3Euu7X5KPpk='

各位老爷给看看,是哪里姿势不对

5325 次点击
所在节点    Python
10 条回复
zeiyso
2017-03-08 23:02:58 +08:00
rogerchen
2017-03-08 23:04:55 +08:00
为什么 java 里边不对 privatekey 的 b64 表示解码, python 不都先 decode 了么
iamxiaoq
2017-03-08 23:07:03 +08:00
@rogerchen encryptByPrivateKey 这个方法里面会对 primarykey 进行 base64 解码
iamxiaoq
2017-03-08 23:07:48 +08:00
@zeiyso 英文实在太弱了,老哥能再提示多一点吗?
zeiyso
2017-03-08 23:16:48 +08:00
iamxiaoq
2017-03-08 23:34:35 +08:00
@zeiyso 老哥稳,读了后面那篇已经明白为什么了,下一步就看怎么解决了,灰常感谢!!
julyclyde
2017-03-09 13:27:48 +08:00
不需要关注是否一致,只需要关注能否互操作
iamxiaoq
2017-03-14 14:34:57 +08:00
@julyclyde 服务器只接受 Java 加密出来的密文, python 加密的密文不接受。
julyclyde
2017-03-14 22:01:55 +08:00
@iamxiaoq 好奇怪……
shmily3929
2017-12-21 10:57:16 +08:00
您好 遇到同样的问题 .. 请问怎么解决的呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/345992

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX