gpt4 book ai didi

python - 如何在Python中实现类似C# RSACryptoServiceProvider的加密?

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:12 24 4
gpt4 key购买 nike

所以,在 C# 中我有以下代码:

public static void Main (string[] args)
{
publicKeyXml = "<Modulus>mFCubVhPGG+euHuVQbNObqod/Ji0kRe+oh2OCFR7aV09xYiOklqFQ8jgIgAHvyCcM1JowqfFeJ5jV9up0Lh0eIiv3FPRu14aQS35kMdBLMebSW2DNBkfVsOF3l498WWQS9/THIqIaxbqwRDUxba5btBLTN0/A2y6WWiXl05Xu1c=</Modulus><Exponent>AQAB</Exponent>";
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider ();
rSACryptoServiceProvider.FromXmlString (publicKeyXml);
Console.WriteLine(Convert.ToBase64String (rSACryptoServiceProvider.Encrypt (Encoding.ASCII.GetBytes(args[0]), false)));
}

当我用来加密消息时,它在远程服务器上运行得很好(我没有源代码)。但是,当尝试使用 PyCrypto 在 Python 中执行类似操作时,远程服务器无法解密。

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

KEY = RSA.importKey(open('login.key').read()) # Converted to standard format
KEY_CIPHER = PKCS1_v1_5.new(KEY)

testmsg = KEY_CIPHER.encrypt("test msg").encode('base64').replace('\n', '')
# send testmsg down a socket
# Response: {"info":"java.lang.IllegalArgumentException: Could not decrypt.","msg":"Fail"}

对于为什么会出现这种情况有什么想法吗?

最佳答案

好吧,就我而言,这很奇怪。服务器端正在期待我的东西向后。为了解决这个问题,我简单地这样做了:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

KEY = RSA.importKey(open('login.key').read()) # Converted to standard format
KEY_CIPHER = PKCS1_v1_5.new(KEY)

testmsg = KEY_CIPHER.encrypt("test msg").encode('base64').replace('\n', '')
testmsg = "".join(reversed([testmsg[i:i+2] for i in range(0, len(testmsg), 2)]))

关于python - 如何在Python中实现类似C# RSACryptoServiceProvider的加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18085952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com