Octet string 解析

百度百科的 ASN.1 http://baike.baidu.com/view/26378.htm

什么是 octet string 结构化字节

怎么解析,这里有微软的解析方法

If the byte array contains fewer than 128 bytes, the Length field of the TLV triplet requires only one byte to specify the content length. If it is more than 127 bytes, bit 7 of the Length field is set to 1 and bits 6 through 0 specify the number of additional bytes used to identify the content length. This is shown in the following example where the high order bit of the second byte on the first line is set to 1 and the byte indicates that there is a trailing Length byte. The third byte therefore specifies that the content is 0x80 bytes long.

以上就是解析,字节是代表什么意思

例如

字符串字节数小于128的时候:

04 0a                              ; OCTET_STRING (a Bytes)
|     1e 08 00 55 00 73 00 65  00 72  ;   ...U.s.e.r

解析

04 代表 OCTET_STRING 这个结构(参看基本类型汇总表)

0a 代表 字节的数量(a

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java代码示例,用于带有界面的INTEGER和OCTET STRING的BER编解码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class BerCoderDecoder extends JFrame implements ActionListener { private JPanel panel; private JLabel label1, label2; private JTextField inputField, outputField; private JButton encodeButton, decodeButton; public BerCoderDecoder() { super("BER Coder/Decoder"); panel = new JPanel(new GridLayout(3, 2)); label1 = new JLabel("Input:"); inputField = new JTextField(); label2 = new JLabel("Output:"); outputField = new JTextField(); outputField.setEditable(false); encodeButton = new JButton("Encode"); encodeButton.addActionListener(this); decodeButton = new JButton("Decode"); decodeButton.addActionListener(this); panel.add(label1); panel.add(inputField); panel.add(encodeButton); panel.add(decodeButton); panel.add(label2); panel.add(outputField); getContentPane().add(panel); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { String input = inputField.getText(); String output = ""; if (e.getSource() == encodeButton) { try { int value = Integer.parseInt(input); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeByte(0x02); if (value >= 0) { dos.writeByte(Integer.toBinaryString(value).length() / 8 + 1); } else { dos.writeByte(0x01); } dos.writeInt(value); byte[] encoded = baos.toByteArray(); for (int i = 0; i < encoded.length; i++) { output += String.format("%02X", encoded[i]); } } catch (NumberFormatException ex) { output = "Invalid input!"; } catch (IOException ex) { ex.printStackTrace(); } } else if (e.getSource() == decodeButton) { try { byte[] encoded = hexStringToByteArray(input); if (encoded[0] != 0x02) { output = "Not an INTEGER encoding!"; } else { int length = encoded[1] & 0xFF; int value = 0; if (length == 1) { value = encoded[2] & 0xFF; } else if (length == 2) { value = (encoded[2] & 0xFF) << 8; value |= encoded[3] & 0xFF; } else if (length == 3) { value = (encoded[2] & 0xFF) << 16; value |= (encoded[3] & 0xFF) << 8; value |= encoded[4] & 0xFF; } else if (length == 4) { value = (encoded[2] & 0xFF) << 24; value |= (encoded[3] & 0xFF) << 16; value |= (encoded[4] & 0xFF) << 8; value |= encoded[5] & 0xFF; } else { output = "Invalid INTEGER encoding!"; } if (output.isEmpty()) { output = Integer.toString(value); } } } catch (Exception ex) { output = "Invalid input!"; } } outputField.setText(output); } private byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } public static void main(String[] args) { new BerCoderDecoder(); } } ``` 此代码使用Java Swing库创建了一个简单的GUI,该GUI包含一个输入框和一个输出框,用于输入和输出BER编码的数据。它还包括“Encode”和“Decode”按钮,用于执行相应的操作。 对于INTEGER编码,该代码将输入值转换为int,然后使用DataOutputStream将其编码为BER格式,并将结果转换为十六进制字符串。对于OCTET STRING编码,可以将类似的代码添加到该程序中,以便对其进行编码和解码。 请注意,此代码仅包含最基本的错误检查,并且不会处理所有可能的BER编码格式。对于真正的应用程序,您需要实现更全面的解析器/编码器,以确保正确处理各种情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值