BigInteger to byte[] without twos complement?
<p>I have a digital signature (<code>byte[] signedBytes</code>) in <code>byte[] array</code>. I need to convert this to <code>BigInteger</code> (<code>bigSignedBytes</code>), use the resulting number for something, and then convert back to <code>byte[] convertBackSignedBytes</code> from the <code>BigInteger</code>.</p>

<pre><code>byte[] signedBytes = rsa.sign();
BigInteger bigSigned = new BigInteger(signedBytes);
convertBackSignedBytes = bigSigned.toByteArray();
//signedBytes and convertBackSignedBytes differ.
</code></pre>

<p>Problem is after the reconversion from <code>BigInteger</code> back to <code>byte[] convertBackSignedBytes</code> - it appears it differs from my original <code>signedBytes</code> variable. I know <code>BigInteger.toByteArray()</code> returns a twos complement <code>byte[] value</code> - which could be responsible.</p>

<p>So how do I get original bytes from <code>BigInteger</code> without the twos complement?</p>

<p>Somebody recommended this:</p>

<pre><code>byte[] convertBackSignedBytes = bigIntegerValue.toByteArray();
if (convertBackSignedBytes[0] == 0) {
    byte[] tmp = new byte[convertBackSignedBytes.length - 1];
    System.arraycopy(convertBackSignedBytes, 1, tmp, 0, tmp.length);
    convertBackSignedBytes = tmp;
}   
</code></pre>

<p>Tried it - didn't work. Still returns different byte[]value from original <code>signedBytes</code>.</p>

<p>If I try to verify the original signature - it succeeds.</p>

<p>But if I try to verify using the converted signature - it fails. So <code>signedBytes</code> and <code>convertBackSignedBytes</code> are no longer the same.</p>

<p>Any quick pointers please?</p>
