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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:09:18+00:00 2026-06-13T02:09:18+00:00

I want to encrypt some data in python with PyCrypto. However I get an

  • 0

I want to encrypt some data in python with PyCrypto.

However I get an error when using key = RSA.importKey(pubkey):

RSA key format is not supported

The key was generated with:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.pem

The code is:

def encrypt(data):
    pubkey = open('mycert.pem').read()
    key = RSA.importKey(pubkey)
    cipher = PKCS1_OAEP.new(key)
    return cipher.encrypt(data)
  • 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-13T02:09:19+00:00Added an answer on June 13, 2026 at 2:09 am

    PyCrypto does not support X.509 certificates. You must first extract the public key with the command:

    openssl x509 -inform pem -in mycert.pem -pubkey -noout > publickey.pem
    

    Then, you can use RSA.importKey on publickey.pem.


    If you don’t want or cannot use openssl, you can take the PEM X.509 certificate and do it in pure Python like this:

    from Crypto.Util.asn1 import DerSequence
    from Crypto.PublicKey import RSA
    from binascii import a2b_base64
    
    # Convert from PEM to DER
    pem = open("mycert.pem").read()
    lines = pem.replace(" ",'').split()
    der = a2b_base64(''.join(lines[1:-1]))
    
    # Extract subjectPublicKeyInfo field from X.509 certificate (see RFC3280)
    cert = DerSequence()
    cert.decode(der)
    tbsCertificate = DerSequence()
    tbsCertificate.decode(cert[0])
    subjectPublicKeyInfo = tbsCertificate[6]
    
    # Initialize RSA key
    rsa_key = RSA.importKey(subjectPublicKeyInfo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to encrypt some server data using .NET's RSACryptoServiceProvider and decrypt it when
I want to encrypt some data in a form using jQuery before it's sent
As the title says. Using the iPhone SDK, I want to RSA encrypt some
I want to encrypt some Core Data columns with the NSValueTransformer class but I'm
I want to decrypt the string in base64 format. I have some data in
I'm trying to use PHP and OpenSSL to encrypt some data using a public
I'm looking to encrypt some data using multiple ciphers (ie, AES, Serpent, Twofish...), and
What is the difference between encrypting some data vs signing some data (using RSA)?
I use the following code to encrypt some data and I want to move
I am now using Oracle 10g (10.0.2) I want to encrypt some columns in

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.