-->

Bouncy Castle example for encoding CDR

2019-09-09 23:55发布

问题:

I am trying to encode PGWRecord CDR using ASN.1 notation, starting on page 89 in this 3GPP TS Document

I've been looking for awhile for examples how I can do this, but to no avail. Are there any examples that can show me how to do this using Bouncy Castle? Or is there a better alternative than Bouncy Castle to encode this CDR?

Step by step instructions on how I can do this would be very nice!

Any help would be very appreciated. Thanks all!

回答1:

Try to look at BinaryNotes. You have ASN.1 definition of what you are trying to encode. BinaryNotes will generate classes for encoding/decoding objects to/from ASN.1.

BinaryNotes works with xlst transformations to generate classes. You could modify the transformations in a way that it will use bouncy classes form encoding/decoding.



回答2:

Ya, just two months back i have worked on Bouncy castle to decode CDRS which of 3GPP stranded encoded file. Currently i am good in decoding and if any issues on decoding i can give more example and i can solve it easily solve it.

But for your question i can suggest some points and if you have any question pleas add comment. It not only help for you it can help for new bees who start with encode / decode process.

a) You did right API choice (Bouncy Castle) for encoding CDRS files

b) You can go for paid version of encode / decode APIs but it is too expansive

c) I have found 3 paid APIs which gives good and expected results, those are

1) [OSS Nokalva][1]
2) Obj-Sys
3) unigone

d) For me Bouncy castle gave 100% result then paid APIs even though they work well and good. If you want to use Bouncy castle then you need to put some more effort to work on encoding, So Bouncy castle is an API which support basic and some advanced parser, to do any encoding / decoding you need to write the java classes as per your ASN.1 syntax specification and so on.. I can write the steps but it will be too long so end of this answer i am giving you a link which explain it better. click on this link and download bcprov-jdk15on-154.tar.gz file.

e) Unzip the package you downloaded and go to bcprov-jdk15on-154\bcprov-jdk15on-154\src\org\bouncycastle\asn1\test you will find N number of examples to encode/decode CDRs according to the ASN.1 syntax specification

f) If you want to understand with very simple example then go through this link you can easily do your job

Still if you find difficult to understand then write a comment i will try to help my best.



回答3:

I guess that the encoding of GPRSRecord CHOICE would be something like that below.Be careful to tag values and conversion from string view to hex and BCD packing for IMSI and GSNAddress value :

ASN1EncodableVector v = new ASN1EncodableVector();

v.add(new BERTaggedObject(true, 0, new ASN1Integer(18))); //e.g. sgsnPDPRecord (18)
v.add(new BERTaggedObject(true, 3, new BEROctetString(Hex.decode("490154203237518")); //IMSI. String to BCD bytes conversion needed
v.add(new BERTaggedObject(true, 4, new BERTaggedObject(true, 0, new DEROctetString(Hex.decode("994507776655"))))); // GSNAddress
v.add(new BERTaggedObject(true, 5, new BEROctetString(Hex.decode("00DB")))); //ChargingID

byte[] encoded =  BERTaggedObject.getInstance(new BERTaggedObject(true, 78, new BERSet(v))).getEncoded(); //SGWRecord