C# 中的 ASN.1 编码 BER、PER 等

avi*_*ght 5 c# encoding asn.1

我一直在搜索这个主题一段时间,没有找到任何相关的答案。所以想把它放在'Stackoverflow'上......

我们正在尝试对字符串进行编码,以便通过 TCP/IP 连接传递它。由于 ASN.1 是最流行的一种,所以我们正在尝试各种规则 BER、DER、PER 等,以找出我们可以使用哪一种。我们的应用程序是一个基于 .net 的应用程序,我正在寻找可以免费使用的库。

奇怪的是我找不到任何免费的库。所以,我开始寻找 .Net 框架本身。我发现只有一个“BERConverter”。所以,我用它做了一个小例子。以字符串为例

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" 
Run Code Online (Sandbox Code Playgroud)

在 UTF-8 或 ASCII 中,它显示为 512 字节。我使用以下代码使用 BER 对其进行编码

public static byte[] BerConvert(byte[] inputbytes)
    {
        byte[] output = BerConverter.Encode("{o}", inputbytes);
        return output;
    }    
Run Code Online (Sandbox Code Playgroud)

我得到一个大小为 522 的字节数组。在其他一些情况下,我发现字节大小与原始文本相比有所增加。我认为编码会减小尺寸。为什么会这样?

除了 BER,是否还有其他编码规则(如 PER 或 DER)可用于减小编码大小?是否有任何示例、库或支持有助于实现这些编码样式?