static byte[] buildDSA(DSAPublicKey key) { DataByteOutputStream out = new DataByteOutputStream(); BigInteger q = key.getParams().getQ(); BigInteger p = key.getParams().getP(); BigInteger g = key.getParams().getG(); BigInteger y = key.getY(); int t = (p.toByteArray().length - 64) / 8; out.writeByte(t); out.writeBigInteger(q); out.writeBigInteger(p); out.writeBigInteger(g); out.writeBigInteger(y); return out.toByteArray(); }
static byte[] buildDH(DHPublicKey key) { DataByteOutputStream out = new DataByteOutputStream(); BigInteger p = key.getParams().getP(); BigInteger g = key.getParams().getG(); BigInteger y = key.getY(); int pLength, gLength, yLength; if (g.equals(TWO) && (p.equals(DHPRIME768) || p.equals(DHPRIME1024))) { pLength = 1; gLength = 0; } else { pLength = BigIntegerLength(p); gLength = BigIntegerLength(g); } yLength = BigIntegerLength(y); out.writeShort(pLength); if (pLength == 1) { if (p.bitLength() == 768) out.writeByte((byte) 1); else out.writeByte((byte) 2); } else out.writeBigInteger(p); out.writeShort(gLength); if (gLength > 0) out.writeBigInteger(g); out.writeShort(yLength); out.writeBigInteger(y); return out.toByteArray(); }
static int BigIntegerLength(BigInteger i) { byte[] b = i.toByteArray(); return (b[0] == 0 ? b.length - 1 : b.length); }