A socket is a communications connection point (endpoint) that you can name and address in a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes.
Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect.
A socket has a typical flow of events. In a connection-oriented client-to-server model, the socket on the server process waits for requests from a client. To do this, the server first establishes (binds) an address that clients can use to find the server. When the address is established, the server waits for clients to request a service. The client-to-server data exchange takes place when a client connects to the server through a socket. The server performs the client’s request and sends the reply back to the client.
defgetLowLevelPrime(n): whileTrue: prime_candidate = nBitRandom(n) for divisor in first_primes_list: if prime_candidate % divisor == 0and divisor**2 <= prime_candidate: break # If no divisor found, return value else: return prime_candidate
3. Miller Rabin Primary Test (High-Level Primality test)
候选素数通过低级素数检验后要再一次被 Miller Rabin 素数测试 检验,由 CMU 的教授 Miller 首次提出,并由希大的Rabin教授作出修改,故称 Miller Rabin 素数测试。
defisMiller_Rabin_Test(p): # 1. preparation m = p-1# p 奇 -> m 偶 q = 0
while m%2 == 0 : # print("1") tested √ q += 1 m = m//2# ÷2 # while 循环结束时满足如下条件 # p-1 = 2^q · m assert(p-1 == 2**q * m) tested = [] # 存放测试过的数据 # 2.算法开始 t = 20# 20轮足够了 for _ inrange(t): composite = True # picking a random integer -> a a = random.randint(2,p-2) while a in tested: a = random.randint(2,p-2) tested.append(a) r = pow(a,m,p) # 快速模除 if r == 1or r == p-1: composite = False# 根据二次探测定理:如果满足if的条件,就是素数 else: # 再次判断 for j inrange(1,q): r = (r * r)%p if r == p-1: composite = False break
if composite: print( str(p) + " is Composite !") returnFalse print( str(p) + " is Prime !") returnTrue
for divisor in first_primes_list: if prime_generated % divisor == 0and divisor**2 <= prime_generated: break else: return prime_generated
defisMiller_Rabin_Test(p): # 0. # if p < 3 or (p & 1 == 0): # return p == 2
# 1. preparation m = p-1# p 奇 -> m 偶 q = 0
while m%2 == 0 : # print("1") tested √ q += 1 m = m//2# 右移做快速÷2 # while 循环结束时满足如下条件 # p-1 = 2^q · m assert(p-1 == 2**q * m) tested = [] # 存放测试过的数据 # 2.算法开始 t = 20# 20轮足够了 for _ inrange(t): composite = True # picking a random integer -> a a = random.randint(2,p-2) while a in tested: a = random.randint(2,p-2) tested.append(a) r = pow(a,m,p) # 快速模除 if r == 1or r == p-1: composite = False# 根据二次探测定理:如果满足if的条件,就是素数 else: # 再次判断 for j inrange(1,q): r = (r * r)%p if r == p-1: composite = False break
if composite: print( str(p) + " is Composite !") returnFalse print( str(p) + " is Prime !") returnTrue
if __name__ == '__main__': whileTrue: n = 1024# 生成1024位大素数 p = getLowLevelPrime(n) ifnot isMiller_Rabin_Test(p): continue else: print(n, "bit prime is: \n",p) break
测试成功!
5. largePrime.py文件构造
1 2 3 4 5 6 7 8 9 10 11 12 13
import random
first_primes_list
defnBitRandom(n):
defgetLowLevelPrime(n):
defisMiller_Rabin_Test(p):
defgetGenerator(n):
defgetLargePrime(n):
其中getGenerator()和getLargePrime(n)是对上面三个函数的封装:
1 2 3 4 5 6 7 8 9 10 11 12
defgetGenerator(n): return( nBitRandom(n) )
defgetLargePrime(n): whileTrue: p = getLowLevelPrime(n) ifnot isMiller_Rabin_Test(p): continue else: print(n, "bit prime is: \n",p) break return p
0x04 client_File.py
1. 前期准备
1 2 3 4 5 6 7
import os import random import socket import largePrime
Encodes obj using the codec registered for encoding.
Errors may be given to set the desired error handling scheme. The default error handler is 'strict' meaning that encoding errors raise ValueError (or a more codec specific subclass, such as UnicodeEncodeError). Refer to Codec Base Classes for more information on codec error handling.
Decodes obj using the codec registered for encoding.
Errors may be given to set the desired error handling scheme. The default error handler is 'strict' meaning that decoding errors raise ValueError (or a more codec specific subclass, such as UnicodeDecodeError). Refer to Codec Base Classes for more information on codec error handling.
创建 AES 对象
1 2
from Crypto.Cipher import AES aes = AES.new(key,AES.MODE_ECB)