Use utility method
import org.bouncycastle.jcajce.spec.DHUParameterSpec;
import org.bouncycastle.jcajce.spec.MQVParameterSpec;
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;
+import org.bouncycastle.util.BigIntegers;
/**
* Diffie-Hellman key agreement. There's actually a better way of doing this
@@ -104,28 +105,7 @@ public KeyAgreementSpi(
//
int expectedLength = (p.bitLength() + 7) / 8;
- byte[] tmp = r.toByteArray();
-
- if (tmp.length == expectedLength)
- {
- return tmp;
- }
-
- if (tmp[0] == 0 && tmp.length == expectedLength + 1)
- {
- byte[] rv = new byte[tmp.length - 1];
-
- System.arraycopy(tmp, 1, rv, 0, rv.length);
- return rv;
- }
-
- // tmp must be shorter than expectedLength
- // pad to the left with zeros.
- byte[] rv = new byte[expectedLength];
-
- System.arraycopy(tmp, 0, rv, rv.length - tmp.length, tmp.length);
-
- return rv;
+ return BigIntegers.asUnsignedByteArray(expectedLength, r);
}
protected Key engineDoPhase(
