Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. openssl genrsa -out example.com.rsa 1280
  5. dnssec-keyfromrsa example.com.rsa >Kexample.com.+008+33333.private
  6. """
  7.  
  8. from subprocess import Popen,PIPE
  9. import sys,StringIO,re,base64
  10.  
  11. # only these make sense with RSA keys
  12. dnssecalg={
  13. '1':'RSAMD5',
  14. '5':'RSASHA1',
  15. '8':'RSASHA256',
  16. '10':'RSASHA512',
  17. }
  18.  
  19. tag=33333
  20. alg=8
  21. file=sys.argv[1]
  22.  
  23. rsafile = open(file,'r')
  24.  
  25. rsaparser=Popen(["openssl","rsa","-text"],stdin=rsafile,stdout=PIPE)
  26. (output,outerr)=rsaparser.communicate()
  27.  
  28. # parse openssl RSA output into dict
  29. rsadict={}
  30. rsaitem=None
  31. for l in StringIO.StringIO(output):
  32. l = l.strip('\n\r')
  33. if l[0:5] == '-----':
  34. break
  35.  
  36. m = re.match('^([A-Za-z0-9-]+):\s*(.*?)\s*$',l)
  37. if m:
  38. rsaitem=m.group(1).lower()
  39. rsaline=m.group(2)
  40. rsadict[rsaitem]=rsaline
  41. continue
  42.  
  43. l = l.strip(' ')
  44. if rsaitem:
  45. rsadict[rsaitem] = rsadict[rsaitem]+l
  46.  
  47. # show the dict in the desired format
  48. for k in ['Private-key-format','Algorithm','Modulus',
  49. 'PublicExponent','PrivateExponent','Prime1','Prime2',
  50. 'Exponent1','Exponent2','Coefficient']:
  51. if k == 'Private-key-format':
  52. v = 'v1.2'
  53. elif k == 'Algorithm':
  54. v = '%s (%s)'%(alg,dnssecalg[str(alg)])
  55. elif k == 'PublicExponent':
  56. v = 'AQAB'
  57. else:
  58. vhex=rsadict[k.lower()]
  59. v = base64.b64encode(re.sub('[:]','',vhex).decode('hex'))
  60. #print '<%s/%s>'%(vhex,v)
  61.  
  62. print '%s: %s'%(k,v)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement