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 8330629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:10:49+00:00 2026-06-09T02:10:49+00:00

So I have generated a self signed certificate and a private key with OpenSSL.

  • 0

So I have generated a self signed certificate and a private key with OpenSSL.

Right now I am trying to:

a) print the public key as a string. This:

f = open(CERT_FILE)
cert_buffer = f.read()
f.close()
cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_buffer)
pub_key = cert.get_pubkey()
print pub_key

Prints something like:

<OpenSSL.crypto.PKey object at 0x7f059864d058>

b) encrypt a string with this public key

c) decrypt the encrypted string with a private key

I would like to see some code examples. Please use only OpenSSL, no wrappers.

  • 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-09T02:10:50+00:00Added an answer on June 9, 2026 at 2:10 am

    Is this what you want? It uses PyCrypto, not PyOpenSSL (I’m not sure if this is what you wanted to avoid when you mention no wrappers)

    #!/usr/bin/env python
    
    from Crypto.Cipher import AES
    from Crypto.Cipher import PKCS1_OAEP
    from Crypto.PublicKey import RSA
    
    def step1():
        rsaKey = RSA.importKey(open("./myKey.der", 'r'))
        print "Step 1: This is my rsa-key:\n%s" % rsaKey.exportKey()
    
    def step2_encrypt(string):
        rsaKey = RSA.importKey(open("./myKey.der", 'r'))
        pkcs1CipherTmp = PKCS1_OAEP.new(rsaKey)
        encryptedString = pkcs1CipherTmp.encrypt(string)
        print "Step 2: encrypted %s is %s" % (string, encryptedString)
        return encryptedString
    
    def step3_decrypt(encryptedString):
        rsaKey = RSA.importKey(open("./myKey.der", 'r'))
        pkcs1CipherTmp = PKCS1_OAEP.new(rsaKey)
        decryptedString = pkcs1CipherTmp.decrypt(encryptedString)
        print "Step 3: decryptedString %s is %s" % (encryptedString, decryptedString)
        return decryptedString
    
    
    if __name__ == "__main__":
        step1()
        encryptedString = step2_encrypt("hello, duuude")
        decryptedString = step3_decrypt(encryptedString)
        print "Tadaaaa: %s" % decryptedString
    

    The key files contain the public/private parts, so the encryption/decryption modules will know what to do.

    Do you need the public/private key in two separate files (should be kind of straight forward, right)?

    Be aware that when using asymmetric encryption, the maximum number of characters you can encrypt depends on the modulus used in your key. In the example above, if you use a regular RSA key (SHA-1, with 20 bytes modulus), you’ll get errors for strings bigger than 214 bytes. As cyroxx pointed out in the comments, there’s not theoretical limitation to the algorithm (you can encrypt long strings with very long keys) but the computational time it would take makes it pretty inviable for practical purposes.

    If you need to cypher big chunks of data, you’ll probably want to encrypt that data with a symmetric algorithm (like AES) and send the password encrypted with the RSA (asymmetric) keys along in the transferred data… but that’s a different matter with a number of other issues 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a self-signed SSL certificate using OpenSSL (the version that
This is an extension question to: how-do-i-create-a-self-signed-certificate-for-code-signing-on-windows To take this further, if I have
I have generated using openssl mycert.pem which contents the certificate. And I converted the
I want to know the difference between self signed and certificate generated by certification
I've generated a self-signed certificate for my Java app using keytool. However, when I
I have created a self generated certificate to sign a DLL. When I load
I have a WCF service being hosted over https with a self-signed certificate. I'm
We have a valid PKCS#10 Certificate Request generated on the Client using CertEnroll. Now
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
I have a tornado server that provide an https connection with a self signed

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.