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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:24:24+00:00 2026-05-24T03:24:24+00:00

I am trying to get 2 programs to share encrypted data over a network

  • 0

I am trying to get 2 programs to share encrypted data over a network using public keys, but I am stuck with a difficult problem : the information that is shared (the keys and/or the encrypted data) seems to get modified. I am hoping to keep the encrypted data format as well as the format of the keys as simple as possible in order to allow compatibility with other languages.
To break down the problem, I have created 2 programs : Keyreceive and Keysend.
They execute in this order :

  1. Keyreceive starts up and waits to receive the encrypted data
  2. Keysend starts up and generates an RSA key, saving the exported private key to a file
  3. Keysend encrypts a piece of data and sends it to Keyreceive over the network
  4. Keyreceive imports the private key from the same file, and uses it to decrypt the encrypted data
  5. Keysend also decrypts the encrypted data to verify the result

Keysend.py

import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random

rng = Random.new().read
RSAkey = RSA.generate(1024, rng) 

privatekey = RSAkey
publickey = RSAkey.publickey()
print(privatekey.exportKey()) #export under the 'PEM' format (I think)
print(publickey.exportKey())

file = open("Keys.txt", "w")
file.write(privatekey.exportKey()) #save exported private key
file.close()

data = "hello world"
enc_data = publickey.encrypt(data, 16) #encrypt message with public key
print(str(enc_data))

host = "localhost"
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.connect((host, port))
connexion.send(str(enc_data)) # send encrypted data, this appears to be the source of the problem

dec_data = RSAkey.decrypt(enc_data) # test decryption
print(dec_data)

os.system("pause")

Keyreceive.py

import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random

host = ''
port = 12800

connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.bind((host, port))
connexion.listen(5)
clientconnexion, connexioninfo = connexion.accept()
enc_data = clientconnexion.recv(1024) # receive encrypted data
print(enc_data)

file = open("Keys.txt", "r")
privatestr = file.read() # retrieve exported private key from file
file.close()
print(privatestr)

privatekey = RSA.importKey(privatestr) # import private key
data = privatekey.decrypt(enc_data) # decrypt sent encrypted data
print(data)

os.system("pause")

After both files have finished decrypting the encrypted data, Keysender outputs the original message : “hello world”, whereas Keyreceiver outputs gibberish.
If there is “hidden” information in the encrypted data and key formats, would there be some way of writing them in a “pure” text format?

  • 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-24T03:24:26+00:00Added an answer on May 24, 2026 at 3:24 am

    You’re right about which line is the source of the problem.

    connexion.send(str(enc_data))
    

    enc_data here is a tuple, the first (and in fact only) element of which is a string containing the actual ciphertext. When you are calling str on it, you’re getting Python’s attempt to convert the tuple to a string, which is not what you want. If you change it to this:

    connexion.send(enc_data[0])
    

    then it should do what you want.

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

Sidebar

Related Questions

I'm trying to get this simple program to work on windows, but it crashes:
I'm trying to get console input in my Clojure program, but when it gives
I'm trying to use rusage statistics in my program to get data similar to
Problem I have been trying all sorts of byte counts trying to get WriteFile
Im just trying to share an integer between two processes, but the memory segment
I get the following error when trying to compile an application using pthreads on
I am trying to get OpenHardwareMonitor to read temperature data out of the Winbond
Im doing C homework and trying to get this program to run, but I
I am trying to get the program to call up the current date, add
i'm trying to get this program to break down a user defined amount of

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.