Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:45:06+00:00 2026-06-03T02:45:06+00:00

I have a file with the public exponent and modulus in it. They’re not

  • 0

I have a file with the public exponent and modulus in it. They’re not in pem or xml or der format, they’re just the values written in at their offsets.

How can I make a public key out of them with M2Crypto? I also have the private key in the same format. I’ve managed to use code that someone posted here on Stackoverflow to generate a PEM file with php, but this seems like an extremely ridiculous way to do it.

This isn’t a one-time thing either, I need to be able to read the public exponent and modulus from files in this format to check the signature.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:45:07+00:00Added an answer on June 3, 2026 at 2:45 am

    Thank you very much to Lars here: http://blog.oddbit.com/2011/05/09/signing-data-with-ssh-agent/

    e is a Python long of the public exponent.
    n is a Python long of the public Modulus.

    The code he posted was:

    import M2Crypto
    key = M2Crypto.RSA.new_pub_key((
        M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hex(e)[2:])),
        M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hex(n)[2:])),
        ))
    

    hex will generate a hex string of the sort 0xA45E, so he’s just grabbing everything after the 0x.

    I’m reading the key from a file, so I don’t have it as a long. I ended up using:

    import M2Crypto
    from binascii import hexlify 
    e = f.read(4)
    n = f.read(0x80)
    key = M2Crypto.RSA.new_pub_key((
        M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hexlify(e))),
        M2Crypto.m2.bn_to_mpi(M2Crypto.m2.hex_to_bn(hexlify(n))),
        ))
    

    Worked like a charm!

    The accepted format of new_pub_key, as per the documentation, needs to be

    OpenSSL’s MPINT format – 4-byte big-endian bit-count followed by the
    appropriate number of bits

    I’m not sure if this is a typo, but for my exponent of (in hex) 00010001 ended up being 000003010001. I think it’s a byte count, not bit count. They also stripped the first 0x00. I don’t know if that’s standard or if because it was an empty byte.

    edit: I think I have a bit of a better understanding of the format.

    If the first byte is negative, a zero byte is added to the beginning.
    If there are any leading (at the beginning) zero bytes, they are stripped unless the first byte would become negative, in which case, only one zero byte is left.

    Some examples:

    Unformatted:
    \x23\x24\x25\x26
    Formatted:
    \x00\x00\x00\x04\x23\x24\x25\x26
    Explanation:
    String left as is and count of bytes packed in
    
    Unformatted:
    \x00\x23\x55\x35
    Formatted:
    \x00\x00\x00\x03\x23\x55\x35
    Explanation:
    leading zero byte removed, byte count now 3
    
    Unformatted:
    \x80\x43\x55\x27
    Formatted:
    \x00\x00\x00\x05\x00\x80\x43\x55\x27
    Explanation:
    leading zero byte added because \x80 is negative
    
    Unformatted:
    \x00\xff\x43\x23
    Formatted:
    \x00\x00\x00\x04\x00\xff\x43\x23
    Explanation:
    Leading zero byte left because \xff is negative
    
    Unformatted:
    \x23\x53\66\x00
    Formatted:
    \x00\x00\x00\x04\x23\x53\66\x00
    Explanation:
    Trailing zero byte left in string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In .NET I have generated the following public key file: <RSAKeyValue> <Modulus>xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>
Hi I have the following class which provides access to a xml file: public
Even though I have deleted some strings from my strings.xml file they still show
I have a method that is suppose to edit a xml file: public void
I want to work with XML file format of RSA public and private keys.
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)
I have a data structure which is as given below: class File { public
I have the following inide my Page_Load on an aspx.cs file: public abstract partial
I have my csv file in my public folder, and i'm trying to load
Given I have two File objects I can think of the following implementation: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.