Android平台下ASN.1 PER解码及相关技术问题咨询
Great question! Working with ASN.1 PER (aligned and unaligned) on Android is totally doable—let’s cover both your needs step by step.
一、Android兼容的ASN.1 PER编解码库
The go-to library for most ASN.1-related tasks on Android is BouncyCastle—it’s widely used, well-maintained, and supports both aligned and unaligned PER encoding/decoding. Here’s how to use it:
1. 添加依赖
First, add the BouncyCastle provider to your app’s build.gradle (Module level):
implementation 'org.bouncycastle:bcprov-jdk15to18:1.78.1'
Make sure to use a version compatible with your target Android SDK.
2. 解码PER数据(对齐/非对齐)
PER requires context from your ASN.1 schema to properly decode, but if you just need to parse the raw ASN.1 structure, here’s a basic example:
import org.bouncycastle.asn1.ASN1Primitive; import org.bouncycastle.asn1.per.PerInputStream; import java.io.ByteArrayInputStream; // 假设perData是从NFC标签获取的二进制字节数组 byte[] perData = ...; // 初始化PER输入流:第二个参数为true表示对齐PER,false为非对齐 try (PerInputStream perInputStream = new PerInputStream(new ByteArrayInputStream(perData), true)) { ASN1Primitive asn1Object = ASN1Primitive.fromInputStream(perInputStream); // 打印解析后的ASN.1对象(会输出结构化的字符串) System.out.println("Decoded ASN.1 object: " + asn1Object.toString()); // 如果你的数据是特定结构(比如Sequence),可以强制转换并遍历元素 // 例如: // if (asn1Object instanceof DERSequence) { // DERSequence sequence = (DERSequence) asn1Object; // for (int i = 0; i < sequence.size(); i++) { // System.out.println("Element " + i + ": " + sequence.getObjectAt(i)); // } // } } catch (Exception e) { e.printStackTrace(); }
更优方案:用ASN.1编译器生成Java类
If you have the official ASN.1 schema for your data, using an ASN.1 compiler (like asn1c or the OASIS ASN.1 Toolkit) to generate Android-compatible Java classes will make parsing far more intuitive. These generated classes map directly to your schema’s structures, so you can easily access fields and convert them to readable strings without manual parsing.
二、二进制数据转字符串
This depends on what kind of string you need:
1. 二进制转十六进制字符串(用于调试/查看原始数据)
If you just want to represent the raw binary data as a human-readable hex string, you can implement a simple utility method:
public static String bytesToHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(2 * bytes.length); for (byte b : bytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString().toUpperCase(); // 可选:转大写更易读 } // 使用示例: byte[] nfcBinaryData = ...; String hexStr = bytesToHex(nfcBinaryData);
2. 解码PER后转结构化字符串
Once you’ve decoded the PER data into an ASN.1 object (or generated Java class), you can convert it to a readable string by:
- Using the built-in
toString()method of BouncyCastle’s ASN.1 classes (as shown earlier) - For generated Java classes, overriding
toString()or writing a helper method to format fields into a human-friendly string (e.g., "Name: John Doe, ID: 1234")
关键注意事项
- Always confirm whether your PER data is aligned or unaligned—this must match the setting in
PerInputStreamto avoid decoding errors. - If you’re dealing with sensitive data (common in NFC use cases), ensure you’re handling the binary data securely (e.g., avoiding logging raw data in production).
内容的提问来源于stack exchange,提问作者Bijesh P V

