V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
iamxiaoq
V2EX  ›  Python

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

  •  
  •   iamxiaoq · 2017-03-08 22:49:33 +08:00 · 5276 次点击
    这是一个创建于 2798 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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='

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

    10 条回复    2017-12-21 10:57:16 +08:00
    zeiyso
        1
    zeiyso  
       2017-03-08 23:02:58 +08:00   ❤️ 1
    rogerchen
        2
    rogerchen  
       2017-03-08 23:04:55 +08:00 via iPad
    为什么 java 里边不对 privatekey 的 b64 表示解码, python 不都先 decode 了么
    iamxiaoq
        3
    iamxiaoq  
    OP
       2017-03-08 23:07:03 +08:00
    @rogerchen encryptByPrivateKey 这个方法里面会对 primarykey 进行 base64 解码
    iamxiaoq
        4
    iamxiaoq  
    OP
       2017-03-08 23:07:48 +08:00
    @zeiyso 英文实在太弱了,老哥能再提示多一点吗?
    zeiyso
        5
    zeiyso  
       2017-03-08 23:16:48 +08:00
    iamxiaoq
        6
    iamxiaoq  
    OP
       2017-03-08 23:34:35 +08:00
    @zeiyso 老哥稳,读了后面那篇已经明白为什么了,下一步就看怎么解决了,灰常感谢!!
    julyclyde
        7
    julyclyde  
       2017-03-09 13:27:48 +08:00
    不需要关注是否一致,只需要关注能否互操作
    iamxiaoq
        8
    iamxiaoq  
    OP
       2017-03-14 14:34:57 +08:00
    @julyclyde 服务器只接受 Java 加密出来的密文, python 加密的密文不接受。
    julyclyde
        9
    julyclyde  
       2017-03-14 22:01:55 +08:00
    @iamxiaoq 好奇怪……
    shmily3929
        10
    shmily3929  
       2017-12-21 10:57:16 +08:00
    您好 遇到同样的问题 .. 请问怎么解决的呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3049 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:37 · PVG 21:37 · LAX 05:37 · JFK 08:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.