ASN.1编码新手求助:修改ICCID值并生成同格式ASN.1文件
Hey there! Since you're new to ASN.1 work, let's walk through exactly how to swap out the ICCID value while keeping your output file in the exact same format as your input template. Here's what you need to do:
1. Understand the ICCID Format in Your ASN.1
First, note that your ICCID is defined as a hexadecimal string wrapped in single quotes, ending with 'H to denote hex encoding. When replacing it, you must follow this exact structure:
- Use a valid 19-20 digit ICCID converted to hex (or keep the same digit count as your original)
- Wrap the hex string in single quotes
- Append
Hat the end
2. Direct Text Replacement (For Human-Readable ASN.1)
If you're working directly with the text-based ASN.1 definition (not binary encoded data), you can simply edit the iccid line. Here's your modified template with a sample new ICCID (replace '89123456789012345678'H with your actual new value):
headerValue ProfileElement ::= header : { major-version 2, minor-version 0, profileType "GSMA Profile Package", iccid '89123456789012345678'H, eUICC-Mandatory-services { usim NULL, milenage NULL, javacard NULL }, eUICC-Mandatory-GFSTEList { -- see Note 1 id-MF, id-USIM }, -- These SMS connectivity parameters may be freely changed connectivityParameters 'A0090607#TON_NPI#DIALING_NUMBER'H }
All other lines, comments, punctuation, and structure are identical to your original template—only the ICCID hex value has been updated.
3. Working with Binary ASN.1 (BER/DER Encoded Data)
If your input is a binary ASN.1 file (not text), you'll need tools to decode, modify, and re-encode the data. For example, using Python's pyasn1 library:
First, define a matching ASN.1 structure, then decode the binary data, swap the ICCID, and re-encode:
from pyasn1.codec.der import decoder, encoder from pyasn1.type import univ, namedtype, constraint # Define the ProfileElement structure to match your ASN.1 template class ProfileElement(univ.Sequence): componentType = namedtype.NamedTypes( namedtype.NamedType('major-version', univ.Integer()), namedtype.NamedType('minor-version', univ.Integer()), namedtype.NamedType('profileType', univ.OctetString()), namedtype.NamedType('iccid', univ.OctetString().subtype( subtypeSpec=constraint.ValueSizeConstraint(10, 20) # ICCID is 10-20 bytes (20-40 hex chars) )), namedtype.NamedType('eUICC-Mandatory-services', univ.SetOf( univ.ObjectIdentifier() )), namedtype.NamedType('eUICC-Mandatory-GFSTEList', univ.SetOf( univ.ObjectIdentifier() )), namedtype.NamedType('connectivityParameters', univ.OctetString()) ) # Step 1: Decode the binary ASN.1 file with open('input_profile.bin', 'rb') as f: binary_data = f.read() profile, _ = decoder.decode(binary_data, asn1Spec=ProfileElement()) # Step 2: Replace the ICCID (convert your new hex string to bytes) new_iccid_hex = '89123456789012345678' profile['iccid'] = univ.OctetString.fromHexString(new_iccid_hex) # Step 3: Re-encode to DER binary format new_binary_data = encoder.encode(profile) with open('output_profile.bin', 'wb') as f: f.write(new_binary_data)
This will generate a binary file with the updated ICCID, maintaining the original ASN.1 structure.
内容的提问来源于stack exchange,提问作者dhiv

