Professo Bean
Professo Bean

Reputation: 25

C# if I have private key How do I decrypt?

I encrypt in python like this

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

I tried in C# to decrypt like this but it doesn't work

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);

        }


    }
}

Bad Data exception appear

In python side I decode in this way .

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

How to impact the private key work in that situation?

Thank in advance!

Upvotes: 1

Views: 1128

Answers (2)

Ian Butler
Ian Butler

Reputation: 107

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

should be

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

because you are using PKCS1_OAEP padding in you python code.

Upvotes: 0

loopbackbee
loopbackbee

Reputation: 23312

On the python side you are base64 encoding the encrypted string. You'll need to base64 decode it before attempting to decrypt it with C#. You'll also need to make sure the cypher parameters (block mode, padding, etc) are the same on both sides.

Upvotes: 1

Related Questions