.Net BouncyCastle - CSR and Private Key Generation
ArrayList and Hashtable were superceded by List&lt;T&gt; and Dictionary&lt;TKey, TValue&gt;, respectively, in .NET 2.0.

The first parameter of the X509Name constructor, ordering, specifies the order in which the OID/string pairs will be processed. However we're using new ArrayList(attrs.Keys) for this parameter, and the documentation for Hashtable states "The order of the keys ... is unspecified", so we can't rely upon the keys being returned in the same order that we inserted them.

If the order isn't important in your case, replace ArrayList/Hashtable with List&lt;T&gt;/Dictionary&lt;TKey, TValue&gt;:

var attributes = new Dictionary&lt;DerObjectIdentifier, string&gt;
{
    { X509Name.CN, domainName },
    { X509Name.O, companyName },
    { X509Name.L, city },
    { X509Name.ST, state },
    { X509Name.C, countryIso2Characters }
};

if (division != null)
{
    attributes.Add(X509Name.OU, division);
}

if (email != null)
{
    attributes.Add(X509Name.EmailAddress, email);
}

var subject = new X509Name(attributes.Keys.ToList(), attributes);


If the order is important to you, then we need another solution since "The order of the keys in the Dictionary&lt;TKey, TValue&gt;.KeyCollection is unspecified".

The obvious solution is to use a List&lt;KeyValuePair&lt;DerObjectIdentifier, string&gt;&gt;, but this gets very verbose:

var attributes = new List&lt;KeyValuePair&lt;DerObjectIdentifier, string&gt;&gt;
{
    new KeyValuePair&lt;DerObjectIdentifier, string&gt;(X509Name.CN, domainName),
    ...
};


One trick I read about is to define a helper class like this

private class KeyValuePairList&lt;TKey, TValue&gt; : List&lt;KeyValuePair&lt;TKey, TValue&gt;&gt;
{
    public void Add(TKey key, TValue value)
    {
        this.Add(new KeyValuePair&lt;TKey, TValue&gt;(key, value));
    }

    public IEnumerable&lt;TKey&gt; Keys
    {
        get { return this.Select(item =&gt; item.Key); }
    }

    public IEnumerable&lt;TValue&gt; Values
    {
        get { return this.Select(item =&gt; item.Value); }
    }
}


The Add method lets us use the same initializer syntax we used for the Dictionary.

And then you can use the X509Name(IList, IList) constructor.

var attributes = new KeyValuePairList&lt;DerObjectIdentifier, string&gt;
{
    { X509Name.CN, domainName },
    { X509Name.O, companyName },
    { X509Name.L, city },
    { X509Name.ST, state },
    { X509Name.C, countryIso2Characters }
};

if (division != null)
{
    attributes.Add(X509Name.OU, division);
}

if (email != null)
{
    attributes.Add(X509Name.EmailAddress, email);
}

var subject = new X509Name(attributes.Keys.ToList(), attributes.Values.ToList());