<p>Are you sure that the r and s that you have generated are correct? If yes converting this (r,s) to DER can be done in this way.</p>
<pre><code>def der(self):
        rbin = self.r.to_bytes(32, byteorder='big')
        # remove all null bytes at the beginning
        rbin = rbin.lstrip(b'\x00')
        # if rbin has a high bit, add a \x00
        if rbin[0] &amp; 0x80:
            rbin = b'\x00' + rbin
        result = bytes([2, len(rbin)]) + rbin  # &lt;1&gt;
        sbin = self.s.to_bytes(32, byteorder='big')
        # remove all null bytes at the beginning
        sbin = sbin.lstrip(b'\x00')
        # if sbin has a high bit, add a \x00
        if sbin[0] &amp; 0x80:
            sbin = b'\x00' + sbin
        result += bytes([2, len(sbin)]) + sbin
        return bytes([0x30, len(result)]) + result
</code></pre>
<p>here self.r and self.s are integers previously generated</p>

CC BY-SA 4.0
2024-04-11T05:32:26.027Z
2024-04-11T05:32:26.027Z
kenadams15
<pre><code>vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(pubKey),curve=ecdsa.SECP256k1)

if vk.verify(bytes.fromhex(signature), bytes.fromhex(sha256_hash), hashlib.sha256, sigdecode=sigdecode_der) == True: # True
        print(&quot;Signature is Valid&quot;)
else:
        print(&quot;Signature is not Valid&quot;)
</code></pre>
<p>I am trying to verify a transaction using secp256k1_generator, but I can't seem to get the signature into the correct (r,s) format, and am getting the following output</p>
<pre><code> File &quot;/.local/lib/python3.10/site-packages/ecdsa/keys.py&quot;, line 741, in verify_digest
    raise BadSignatureError(&quot;Signature verification failed&quot;)
ecdsa.keys.BadSignatureError: Signature verification failed
</code></pre>
<p>I have taken help of <a href="https://bitcoin.stackexchange.com/questions/72657/signature-verification-in-python-using-compressed-public-key/72701#72701">Signature verification in python using compressed public key</a>
and</p>
<p><a href="https://stackoverflow.com/questions/73630540/how-to-verify-ecdsa-signatures-with-python">https://stackoverflow.com/questions/73630540/how-to-verify-ecdsa-signatures-with-python</a>
But still I am getting this error. How can I resolve it?</p>

<transactions><signature><python>
How to convert signature to DER encoded format in python
aaravm
# How to convert signature to DER encoded format in python

(Asked by: username_0 on 2024-04-10)

vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(pubKey),curve=ecdsa.SECP256k1)

if vk.verify(bytes.fromhex(signature), bytes.fromhex(sha256_hash), hashlib.sha256, sigdecode=sigdecode_der) == True: # True
        print("Signature is Valid")
else:
        print("Signature is not Valid")

I am trying to verify a transaction using secp256k1_generator, but I can't seem to get the signature into the correct (r,s) format, and am getting the following output
 File "/.local/lib/python3.10/site-packages/ecdsa/keys.py", line 741, in verify_digest
    raise BadSignatureError("Signature verification failed")
ecdsa.keys.BadSignatureError: Signature verification failed

I have taken help of Signature verification in python using compressed public key
and
https://stackoverflow.com/questions/73630540/how-to-verify-ecdsa-signatures-with-python
But still I am getting this error. How can I resolve it?

**Tags:** transactions, signature, python


---

## 1 answers

### Answer by username_1 on 2024-04-11. Score: 0

Are you sure that the r and s that you have generated are correct? If yes converting this (r,s) to DER can be done in this way.
def der(self):
        rbin = self.r.to_bytes(32, byteorder='big')
        # remove all null bytes at the beginning
        rbin = rbin.lstrip(b'\x00')
        # if rbin has a high bit, add a \x00
        if rbin[0] & 0x80:
            rbin = b'\x00' + rbin
        result = bytes([2, len(rbin)]) + rbin  # <1>
        sbin = self.s.to_bytes(32, byteorder='big')
        # remove all null bytes at the beginning
        sbin = sbin.lstrip(b'\x00')
        # if sbin has a high bit, add a \x00
        if sbin[0] & 0x80:
            sbin = b'\x00' + sbin
        result += bytes([2, len(sbin)]) + sbin
        return bytes([0x30, len(result)]) + result

here self.r and self.s are integers previously generated


---

