Search code examples
javabouncycastlesnmpasn.1

How could I encode ip address using bouncy castle library?


I am trying to realize snmpset using java, and I am using bouncy castle version 1.52 library to encode the values of oids, and now I want to encode an IP address, how could I reallize it? I tried this:

new GeneralName(GeneralName.iPAddress,"xx.xx.xx.xx");

but get a

snmpInASNParseErrs

error

UPDATE:

OK, I think i am very near to the right answer now, I used this:

InetAddress ip = InetAddress.getByName("xx.xx.xx.xx");
byte[] bytes = ip.getAddress();
v.add(new DERApplicationSpecific(0, new DEROctetString(bytes)));

now, the result is:

C/x/IpAddress_04_04_0a_00_00_64

still need more help!


Solution

  • Finally, I solved this problem by this:

    InetAddress ip = InetAddress.getByName(ipv4Address);
    byte[] bytes = ip.getAddress();
    v.add(new DERApplicationSpecific(0, bytes));