Java tutorial
//package com.java2s; import java.security.SignatureException; public class Main { /** * See above. * Only supports sigs up to about 252 bytes. See code to fix BER encoding for bigger than that. * * @return len bytes * @since 0.8.7, moved to SigUtil in 0.9.9 */ private static byte[] aSN1ToSigBytes(byte[] asn, int len) throws SignatureException { //System.out.println("pre from asn1 len=" + len + "\n" + net.i2p.util.HexDump.dump(asn)); if (asn[0] != 0x30) throw new SignatureException("asn[0] = " + (asn[0] & 0xff)); // handles total len > 127 int idx = 2; if ((asn[1] & 0x80) != 0) idx += asn[1] & 0x7f; if (asn[idx] != 0x02) throw new SignatureException("asn[2] = " + (asn[idx] & 0xff)); byte[] rv = new byte[len]; int sublen = len / 2; int rlen = asn[++idx]; if ((rlen & 0x80) != 0) throw new SignatureException("FIXME R length > 127"); if ((asn[++idx] & 0x80) != 0) throw new SignatureException("R is negative"); if (rlen > sublen + 1) throw new SignatureException("R too big " + rlen); if (rlen == sublen + 1) System.arraycopy(asn, idx + 1, rv, 0, sublen); else System.arraycopy(asn, idx, rv, sublen - rlen, rlen); idx += rlen; int slenloc = idx + 1; if (asn[idx] != 0x02) throw new SignatureException("asn[s] = " + (asn[idx] & 0xff)); int slen = asn[slenloc]; if ((slen & 0x80) != 0) throw new SignatureException("FIXME S length > 127"); if ((asn[slenloc + 1] & 0x80) != 0) throw new SignatureException("S is negative"); if (slen > sublen + 1) throw new SignatureException("S too big " + slen); if (slen == sublen + 1) System.arraycopy(asn, slenloc + 2, rv, sublen, sublen); else System.arraycopy(asn, slenloc + 1, rv, len - slen, slen); //System.out.println("post from asn1\n" + net.i2p.util.HexDump.dump(rv)); return rv; } }