title: ASN.1 encodings BER, PER etc. in C#
body: <p>I have been searching on this topic for a while now, without finding any relevant answers. So thought of taking it on 'Stackoverflow' ...</p>
  
  <p>We are trying to encode a string in order to pass it over a TCP/IP connection. Since ASN.1 is the most popular one to do it, so we are trying the various rules BER,DER,PER etc. to find out which one we can use. Our application is a .net based application and I was looking for freely available library which does this.</p>
  
  <p>Strangely i could not find any free libraries.So, i started looking in the .Net framework itself. I found the there is only a 'BERConverter'. So, i did a small example with it. Taking an example string</p>
  
  <pre><code>string str = "The BER format specifies a self-describing and self-delimiting format for encoding ASN.1 data structures. Each data element is encoded as a type identifier, a length description, the actual data elements, and, where necessary, an end-of-content marker. These types of encodings are commonly called type-length-value or TLV encodings. This format allows a receiver to decode the ASN.1 information from an incomplete stream, without requiring any pre-knowledge of the size, content, or semantic meaning of the data" 
  </code></pre>
  
  <p>In UTF-8 or ASCII it show as 512 bytes. I use the following code to encode it using BER    </p>
  
  <pre><code>public static byte[] BerConvert(byte[] inputbytes)
      {
          byte[] output = BerConverter.Encode("{o}", inputbytes);
          return output;
      }    
  </code></pre>
  
  <p>I get a byte array with size 522. In some of the other cases I find that the byte size increases compared to the original text. I thought encoding will decrease the size. Why is it happening like this ?</p>
  
  <p>Apart from BER, are there other encoding rules like PER or DER which can be used to reduce the encoding size ? Are there any examples, libraries, or support which will help is implementing the these encoding styles? </p>