Search code examples
c#bouncycastle

Read printable string from AttributeTypeAndValue using BouncyCastle


Trying to read string from Org.BouncyCastle.Asn1.Crmf.AttributeTypeAndValue.
The string value read from the AttributeTypeAndValue contains the junk value in the beginning.

My code:

//create type value
string id = "1.2";
string value = "hello";
var derValue = new DerPrintableString(value);
var typeValue = new Org.BouncyCastle.Asn1.Crmf.AttributeTypeAndValue(id, derValue);

//read type value
var decodedValue = new DerPrintableString(typeValue.Value.GetDerEncoded());
Console.WriteLine("Original: {0}, Decoded: {1}", 
            derValue.GetString(), decodedValue.GetString());

The output for above code is,
enter image description here

Please help me extract the original string value from AttributeTypeAndValue without junk characters.


Solution

  • Found the solution. The problem is with,

    var decodedValue = new DerPrintableString(typeValue.Value.GetDerEncoded());
    

    Changing it to the following helped me extract the string without junk value,

    var decodedValue = DerPrintableString.GetInstance(typeValue.Value);