C#如果我有私钥,如何解密?

C#如果我有私钥,如何解密?,c#,python,encryption,rsa,C#,Python,Encryption,Rsa,我用python像这样加密 def encrypt_RSA(public_key_loc, message): ''' param: public_key_loc Path to public key param: message String to be encrypted return base64 encoded encrypted string ''' from Crypto.PublicKey import RSA from Cr

我用python像这样加密

def encrypt_RSA(public_key_loc, message):
    '''
    param: public_key_loc Path to public key
    param: message String to be encrypted
    return base64 encoded encrypted string
    '''
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    key = open(public_key_loc, "r").read()
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    encrypted = rsakey.encrypt(message)
    return encrypted.encode('base64')
我试着用C#像这样解密,但没用

namespace ConsoleApplication1
{

    class Program
    {
        private static string _message = @"gvVweOVn/+IBKNrFV1sb+khVu8PdBC78WusGH7IuCXxK4pEsFo8JbOb68phJAMVM1F8XPoq1PX4D
0VuVPmDFHadOUr59IX0IBbQ72bQ1/BoINimSVOzXRbHOfsNxd0kIEdCv6jNlA7ut7hcoGUz6XzdM
b+k8N2K9Dykjehoo9gZEhaXnws1YiuBVN4B+XyjB1VUrgji9fW60lcpL+0UYZ5mcUvK6T7hS7R9W
9QIf5T02iZJLsp3hxS9j/UxPCvK5Cj6t2h4fRCOYgiQv0L21ZD23nKYWgiGyGEmfArqIswUmZ0h2
I2zMs9vC2JVFIid6FpExHUScItBeuM8qYLA/YQ==";

        static void Main(string[] args)
        {

            Decrypt(_message);

        }

        private static void Decrypt(string text)
        {
            StreamReader sr = new StreamReader("./key.private");
        PemReader pr = new PemReader(sr);
        AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pr.ReadObject();
        RSAParameters rsa = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);            
        RSACryptoServiceProvider csp = new RSACryptoServiceProvider(2048);
        csp.ImportParameters(rsa);

        var bytesCypherText = Convert.FromBase64String(text);
        var bytesPlainTextData = csp.Decrypt(bytesCypherText, false);
        var plainTextData = System.Text.Encoding.Unicode.GetString(bytesPlainTextData);

        Console.WriteLine(plainTextData);

        }


    }
}
出现错误数据异常

在python方面,我是这样解码的

def decrypt_RSA(private_key_loc, package):
    '''
    param: public_key_loc Path to your private key
    param: package String to be decrypted
    return decrypted string
    '''
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from base64 import b64decode
    key = open(private_key_loc, "r").read()
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    decrypted = rsakey.decrypt(b64decode(package))
    return decrypted
在这种情况下,如何影响私钥工作


提前感谢

在python端,您正在对加密字符串进行base64编码。您需要先对其进行base64解码,然后再尝试使用C#对其进行解密。您还需要确保两侧的密码参数(块模式、填充等)相同

var bytesPlainTextData = csp.Decrypt(bytesCypherText, false);
应该是

var bytesPlainTextData = csp.Decrypt(bytesCypherText, true);

因为您在python代码中使用的是PKCS1_OAEP填充。

FYI在线发布私钥是个坏主意。请关注@RobWatts的评论,从您的邮件中删除私钥不会改变您的密钥已被泄露的事实。实际上,我想知道的是如何导入私钥situation@ProfessoBean我不知道你说的“impack”是什么意思。从python导入私钥时遇到问题吗?您应该发布堆栈跟踪所引用的代码,因为它似乎不在问题中。在python方面,我很容易导入public和private,但在C#中,我对此有问题。当我用python加密,用C#解密时,错误像坏数据一样出现,我用相反的方法用C#加密,用python解码,错误再次出现。