Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create RSA SHA-256 (DNSSEC valid) keys?

Tags:

python

rsa

sha

I would like to create my own RSA/Sha256 Key Generator in Python for DNSSEC. I know that there is already a build-in keygen (dnssec-keygen) but I want to build it my own.

The keys which are accepted looks like this:

Private-key-format: v1.2

   Algorithm:       8 (RSASHA256)
   Modulus:         wVwaxrHF2CK64aYKRUibLiH30KpPuPBjel7E8ZydQW1HYWHfoGm
                    idzC2RnhwCC293hCzw+TFR2nqn8OVSY5t2Q==
   PublicExponent:  AQAB
   PrivateExponent: UR44xX6zB3eaeyvTRzmskHADrPCmPWnr8dxsNwiDGHzrMKLN+i/
                    HAam+97HxIKVWNDH2ba9Mf1SA8xu9dcHZAQ==
   Prime1:          4c8IvFu1AVXGWeFLLFh5vs7fbdzdC6U82fduE6KkSWk=
   Prime2:          2zZpBE8ZXVnL74QjG4zINlDfH+EOEtjJJ3RtaYDugvE=
   Exponent1:       G2xAPFfK0KGxGANDVNxd1K1c9wOmmJ51mGbzKFFNMFk=
   Exponent2:       GYxP1Pa7CAwtHm8SAGX594qZVofOMhgd6YFCNyeVpKE=
   Coefficient:     icQdNRjlZGPmuJm2TIadubcO8X7V4y07aVhX464tx8Q=

https://www.rfc-editor.org/rfc/rfc5702

My Python script can generate the RSA-parts, but I don't know how to mix it with SHA256:

#!/usr/bin/python

from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
import base64
import hashlib

key = RSA.generate(2048)

expo1 = ((key.d)%((key.p)-1))
expo2 = ((key.d)%((key.q)-1))

KEYVORLAGE = """Private-key-format: v1.2
Algorithm:       8 (RSASHA256)
Modulus: {0}
PublicExponent: {1}
PrivateExponent: {2} 
Prime1: {3}
Prime2: {4}
Exponent1: {5}
Exponent2: {6}
Coefficient: {7}"""

keystring = KEYVORLAGE.format(key.n,key.e,key.d,key.p,key.q,expo1,expo2,key.u)
print keystring

BTW: All my Key Parts generated by this script only has numbers and not random letters, like the valid key.

like image 714
user3056577 Avatar asked Mar 27 '26 05:03

user3056577


1 Answers

(Question was answered by OP, but in the question itself. Copy of text below.)

The answer is simple – by modifying the keystring:

keystring = KEYVORLAGE.format( 
    base64.standard_b64encode(str(key.n)), 
    base64.standard_b64encode(str(key.e)), 
    base64.standard_b64encode(str(key.d)), 
    base64.standard_b64encode(str(key.p)), 
    base64.standard_b64encode(str(key.q)), 
    base64.standard_b64encode(str(expo1)), 
    base64.standard_b64encode(str(expo2)), 
    base64.standard_b64encode(str(key.u)))
like image 142
3 revsDuncan Jones Avatar answered Mar 28 '26 18:03

3 revsDuncan Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!