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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:33:27+00:00 2026-05-27T14:33:27+00:00

Is there a module that has my searching has been unable to discover that

  • 0

Is there a module that has my searching has been unable to discover that would allow writing code like the following? The reason for wanting to write code like this is unimportant. All I am after is some code that has a simple API to generate public and private byte keys and to easily encode and decode data with those keys.

import module, os

method, bits, data = 'RSA', 1024, os.urandom(1024)
public, private = module.generate_keys(method, bits)

assert isinstance(public, bytes) and isinstance(private, bytes)
assert module.decode(module.encode(data, private), public) == data
assert module.decode(module.encode(data, public), private) == data

Most of what appears to be available requires downloading a package and only runs on Python 2.x. It is also quite common to find libraries that work with PEM files or other types of certificates. I would like to avoid having to deal with such files, to generate public and private keys on the fly, and quickly work with data in memory.

  • 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-05-27T14:33:27+00:00Added an answer on May 27, 2026 at 2:33 pm

    Public key encryption is not in the standard library. There are some third party libraries on PyPi for it though:

    • PyCrypto
    • RSA Python

    If you’re interested in the math behind it, Python makes it easy to experiment:

    code = pow(msg, 65537, 5551201688147)               # encode using a public key
    plaintext = pow(code, 109182490673, 5551201688147)  # decode using a private key
    

    The key generation is a little more involved. Here is a simplified example of how to do key generation in-memory using urandom as the source of entropy. The code runs under both Py2.6 and Py3.x:

    import random
    
    def gen_prime(N=10**8, bases=range(2,20000)):
        # XXX replace with a more sophisticated algorithm
        p = 1
        while any(pow(base, p-1, p) != 1 for base in bases):
            p = random.SystemRandom().randrange(N)
        return p
    
    def multinv(modulus, value):
        '''Multiplicative inverse in a given modulus
    
            >>> multinv(191, 138)
            18
            >>> 18 * 138 % 191
            1
    
        '''
        # http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
        x, lastx = 0, 1
        a, b = modulus, value
        while b:
            a, q, b = b, a // b, a % b
            x, lastx = lastx - q * x, x
        result = (1 - lastx * modulus) // value
        return result + modulus if result < 0 else result
    
    def keygen(N):
        '''Generate public and private keys from primes up to N.
    
            >>> pubkey, privkey = keygen(2**64)
            >>> msg = 123456789012345
            >>> coded = pow(msg, 65537, pubkey)
            >>> plain = pow(coded, privkey, pubkey)
            >>> assert msg == plain
    
        '''
        # http://en.wikipedia.org/wiki/RSA
        prime1 = gen_prime(N)
        prime2 = gen_prime(N)
        totient = (prime1 - 1) * (prime2 - 1)
        return prime1 * prime2, multinv(totient, 65537)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a module that has some generic, reusable code that I would
Is there a python module that will do a waterfall plot like MATLAB does?
When I import a module that has a class, what code is executed when
Is there any way to allow people that has a facebook account to post
I'm writing a module that has some functions dealing with text files. I'm new
I'm writing a module that has multiple questions users can chose to answer. Each
I am writing a kernel module that has access to a particular process's memory.
I'm writing something that is a bit like Facebook's shared link preview. I would
We have a Zend application that has these following modules: Users Shop etc... Front
Is there a module that I could use for playing back an audio file

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.