Search code examples
pythonc#encryptiondllrsa

Accessing a function from a DLL using Python


I have an RSA encryption program that is written in C#. It works as follows:

  1. Decrypts the public key using a DLL
  2. Encrypts the data using a custom encryption function using the public key

This encrypted data is then passed to an API that returns a file as form data.

I want to achieve the same functionality via Python.

But I am stuck at the step where I need to access a function from the DLL file that is used to decrypt the data.

This is the code in C# that is used to decrypt the public key:

public string GetPassPublicKey(string mode)
    {
        string PublicKey = "";
        string FilePassword = "abcdef";
        string FilePath = "D:\PublicKey.zip";
try
        {
            if (FilePassword != "" && FilePath != "")
            {
                using (FileStream zipFile = File.Open(FilePath, FileMode.Open))
                {
                    using (Archive archive = new Archive(zipFile, new ArchiveLoadOptions() { DecryptionPassword = FilePassword }))
                    {
                        using (var stream = archive.Entries[0].Open())
                        using (var reader = new StreamReader(stream))
                        {
                            PublicKey = reader.ReadToEnd();
                            PublicKey = Security.EncryptDecrypt.Decrypt(PublicKey);
                        }
                    }
                }
            }
            else
            {

            }
        }
        catch (Exception ex)
        {
            PublicKey = "";
        }
        return PublicKey;

I tried to use Python as follows:

def decrypt_public_key(encrypted_key):
    try:
        # Load the Security.dll
        security_dll = ctypes.WinDLL(r"D:\Security.dll")

        # Define the function signature for Decrypt
        security_dll.EncryptDecrypt_Decrypt = security_dll.EncryptDecrypt.Decrypt()
        security_dll.EncryptDecrypt_Decrypt.argtypes = [ctypes.c_char_p]
        security_dll.EncryptDecrypt_Decrypt.restype = ctypes.c_char_p

        def decrypt(content):
            # Call Decrypt with the input string
            decrypted_key = security_dll.EncryptDecrypt_Decrypt(content.encode('utf-8'))
            return decrypted_key.decode('utf-8')

        return decrypt(encrypted_key)  # Call the decrypt function here

    except Exception as ex:
        print(f"An error occurred during decryption: {str(ex)}")
        return ""

But all I get is An error occurred during decryption: function 'EncryptDecrypt' not found

I tried to use dumpbin to understand the functions in the DLL but no luck, and the team that has used this DLL in their C# code has no documentation around it.


Solution

  • Thank you @JonasH

    I was able to solve this problem using pythonnet.

    Here is a snippet of the code that I used:

    import clr
    clr.AddReference(r"D:\Security.dll")
    from Security import EncryptDecrypt
    
    def decrypt_public_key(encrypted_key):
        try:
                    
            def decrypt(content):
                # Call Decrypt with the input string
                content_string = content.decode('utf-8')
                instance = EncryptDecrypt()
                decrypted_key = instance.Decrypt(content_string)
                return decrypted_key
    
            return decrypt(encrypted_key)  # Call the decrypt function here
    
        except Exception as ex:
            print(f"An error occurred during decryption: {str(ex)}")
            return ""