繁体   English   中英

如何使用 AES 加密字符串并在 Python 中使用服务器和客户端在 Python 中解密它

[英]How to encrypt string with AES and decrypt it in python with server and client in Python

我正在使用 Python 3.7。 我的目标是创建一个服务器,该服务器将使用 AES 加密的密钥(服务器和客户端都有“密钥”)加密随机字符串。 服务器将密码发送给客户端,客户端将对其进行解密。

但是,我编写的代码没有显示任何结果。 我的意思是,代码被“避免”了,我看不到代码的任何结果。 这是我写的代码(服务器和客户端):

服务器:

import socket
import Crypto
import Cryptodome


HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 8080        # Port to listen on (non-privileged ports are > 1023)


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print ('Socket binded to port 8080')
s.listen(0)
print ('socket is listening')

while True:
   c, addr = s.accept()
   print ('Got connection from ', addr)
   string = str(c.recv(1024))
   print(string)
   if string ==  "b'Register'" :
       import random
       import string


       def randomString(stringLength=10):
           letters = string.ascii_lowercase
           return ''.join(random.choice(letters) for i in range(stringLength))
       print("Random String is ", randomString())

       from Crypto.Cipher import AES

       from Crypto.Cipher import AES


       def do_encrypt(message):
           obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
           ciphertext = obj.encrypt(message)
           return ciphertext

客户端:


import socket

HOST = '127.0.0.1'                         # The server's hostname or IP address
PORT = 8080                                # The port used by the server



import Crypto
import sys
sys.modules['Crypto'] = Crypto


name = ''                                          # Sign in to the server!!

while name != 'Register':
   print('Would you like to Register/Login?')
   name = input()

s = socket.socket()
port = 8080
s.connect(('localhost', port))
z = 'Register'
s.sendall(z.encode())
s.close()
name = ''                                          # Sign in to the server!!

print ("Register request sent")




from Crypto.Cipher import AES

def do_decrypt(ciphertext):
    obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
    message = obj2.decrypt(ciphertext)
    return message






from Crypto.PublicKey import RSA

key = RSA.generate(2048)                             # Private key creation
private_key = key.export_key()
file_out = open("private.txt", "wb")
file_out.write(private_key)

public_key = key.publickey().export_key()            # Public key creation
file_out = open("receiver.txt", "wb")
file_out.write(public_key)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:   # sending the Public key to the server.
   s.connect((HOST, PORT))
   s.sendall(public_key)
   data = s.recv(1024)
   s.close()                                       # Close the socket when done

   print('Received', repr(data))

AES 加密代码来源: Sending Encrypted strings using socket in Python

您没有得到与 AES 加密代码相关的任何信息,因为您没有调用该函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM