Fix key array handling in importing/exporting private keys. Resolves issue 48.

This commit is contained in:
Miron Cuperman (devrandom)
2011-08-30 21:09:51 +00:00
parent 23b1325de7
commit 098671ffba
2 changed files with 28 additions and 3 deletions

View File

@@ -239,10 +239,15 @@ public class ECKey implements Serializable {
/** Returns a 32 byte array containing the private key. */
public byte[] getPrivKeyBytes() {
// Getting the bytes out of a BigInteger gives us an extra null byte on the end (for signedness), so snip
// it off here.
// Getting the bytes out of a BigInteger gives us an extra zero byte on the end (for signedness)
// or less than 32 bytes (leading zeros). Coerce to 32 bytes in all cases.
byte[] bytes = new byte[32];
System.arraycopy(priv.toByteArray(), 1, bytes, 0, 32);
byte[] privArray = priv.toByteArray();
int privStart = (privArray.length == 33) ? 1 : 0;
int privLength = Math.min(privArray.length, 32);
System.arraycopy(privArray, privStart, bytes, 32 - privLength, privLength);
return bytes;
}