How can I create X509Certificate using UiPath?

Hi guys,

I have been looking for the activities that can create X509Certificate with no luck. I have found a C# code that works when using VS CODE, and I have tried shifting the code to UiPath using invoke and failed.

Please help. Below is the code. Package used: BouncyCastle v1.8.9
cc: @Gokul001 , @sangeethaneelavannan1

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.OpenSsl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto.Prng;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Asn1.X509;

namespace GenCertificate
    class Cryptography
        public static X509Certificate2 CreateCertificate(string subjectName, string issuer, int ValidMonths, out AsymmetricCipherKeyPair KeyPair, int keyStrength = 2048)
            // Generating Random Numbers
            CryptoApiRandomGenerator randomGenerator = new();
            var random = new SecureRandom(randomGenerator);

            // The Certificate Generator
            X509V3CertificateGenerator certificateGenerator = new();

            // Serial Number
            var serialNumber = BigIntegers.CreateRandomInRange(Org.BouncyCastle.Math.BigInteger.One, Org.BouncyCastle.Math.BigInteger.ValueOf(Int64.MaxValue), random);

            // Issuer and Subject Name
            var subjectDN = new X509Name(subjectName);
            var issuerDN = new X509Name(issuer); 

            // Valid For
            var notBefore = DateTime.UtcNow.Date;
            var notAfter = notBefore.AddMonths(ValidMonths);


            certificateGenerator.AddExtension(X509Extensions.KeyUsage.Id, true, new KeyUsage(KeyUsage.KeyEncipherment));

            // Subject Public Key
            AsymmetricCipherKeyPair subjectKeyPair;
            var keyGenerationParameters = new KeyGenerationParameters(random, keyStrength);
            var keyPairGenerator = new RsaKeyPairGenerator();
            subjectKeyPair = keyPairGenerator.GenerateKeyPair();


            // Generating the Certificate
            var issuerKeyPair = subjectKeyPair;
            KeyPair = subjectKeyPair;

            // Selfsign certificate
            var certificate = certificateGenerator.Generate(issuerKeyPair.Private, random);
            var x509 = new System.Security.Cryptography.X509Certificates.X509Certificate2(certificate.GetEncoded());

            return x509;



You need all necessary imports in UiPath (everything near ‘using’ statement should be in imports tab in UiPath)
Probably you also need import appropriate nuget for that (BouncyCastle).
make sure you have UiPath project with C# language. Otherwise it is possible to translate this code to VB.
If everything from above is ok then we need error message from invoke code activity to find the issue.

1 Like

Hi @Konrad_Mierzwa , I have created a C# project with same code and get error from the variable : exist in both ‘BouncyCastle.Crypto’ and OpenIT.BouncyCastle

You have ambiguous name of the type which exists in two namespaces.
To avoid this error use full name.
So instead of:

AsymmetricCipherKeyPair subjectKeyPair;


Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair subjectKeyPair;

Hi @Konrad_Mierzwa , I tried that still didn’t work. I removed dependency OpenIT.BouncyCastle from Project Tab and it worked. The only issue I have now is to convert string to BigInteger

CryptoApiRandomGenerator randomGenerator = new();
var random = new SecureRandom(randomGenerator);
var serialNumber = BigIntegers.CreateRandomInRange(Org.BouncyCastle.Math.BigInteger.One, Org.BouncyCastle.Math.BigInteger.ValueOf(Int64.MaxValue), random);

Instead of having random number i want to use serial number from my device.

Probably because this type was used somewhere else.

How about .GetHashCode() method?
My second proposition is BitConverter.ToInt64() method, but first you need to convert string to byte array to use this method

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.