0

我正在开发一个魅力库密码学程序。所以我在加密过程中创建了一些文件:

这是文件的第一种形式:

{'S': [2529547354104704434488815336454882971551258253442172664909262664872262308641828657180425763251762799434214676246389229439927937673355984381661698478673721, 7884939579955751675768865466204184622801192045100545653347054876204470548009966239814439012606265136851937377969101594874038755793215125888772591665445255], 'C': {'A': [4301049431384134767239465358013758632794165564671693037571560456238252229587135559146275523146001114893529229447189748225061786239679120035209122482303857, 6306063077181251815212984680976900836993638595149349533803859644566294454808531174150325672959842332667803658638420955016836702483346961178326399241054652], 'C': , 'B': [6352998185563729165088467393939385610730624735454084048756301367631062390264309076917543868773558763863051002854802153320010790248052150717986667832046361,3232330908513706152298027832774230059780104552931278853973284657391134433148157845009937793037162112475438532301331093442728792648078952136809080410765988]}}

这里我用charm加密库的objectToByte函数修改它:

        f = open( rootdir + '/sym_ciphertexts/'+client_mac+'.enc', 'w')
        f.write( objectToBytes(sym_key_ciphertext['S'], group)+"\n")
        f.write( objectToBytes(sym_key_ciphertext['C']['A'], group)+"\n")
        f.write( objectToBytes(sym_key_ciphertext['C']['B'], group)+"\n")
        f.write( str(sym_key_ciphertext['C']['C']))
        f.close()

我收到以下文件:

eJxTMrQKzfdPtEzOcS8Nzkg3Ta7wdHL1zHJ0cvROSdMucMzKC/RJNnEriQg30Q8OM/SM0jf1KKoqNAsxNAkNLS7MyPOKDMj2tjAsNTEN1Q511Xb0cwwoNkxOdHS0VQIASnccfw== eJxTMrRK9nc1KMzNL7VILQopN/VN982tDAjN8y9IrnL2SDUwzvWu9HMz0Tfy8w1x1891Sy10TylI9QpPDA4wCzc3SUt2L0vz8qgMrDJMyc8xrPILSQ5IMXcrDHS0VQIAhe8eSQ== eJwlzMkOQ0AAANB/cXUQpogmPZgYy2CCsbS92dqYKiaWqq/voe8DngDODPGWbebzS19JoNdxbOY60XqnhOt7BgiLjizv/iHX9tLhqGIS94oqj9wuvlE0TKMx4aGxCqZmwWEQtNkTW+YMxB8I00SkPZWk/wybEIbgMWZVSjYk0nuODtkCwTyUHHUj5khj6tpsLiQa9AE9NZ5ZtuTq7IoZqRwr3kX4AfuIOP8= 670859917002753633409302654664271647328249631254

为了再次接收起始文件,我使用以下代码:

f = open( rootdir + '/sym_ciphertexts/'+client_mac+'.enc', 'r')
json_data = json.loads(f.read())
f.close()
C_ = {'A':bytesToObject(json_data['C']['A'] ,group), 'B':bytesToObject(json_data['C']['B'] ,group), 'C':integer(int(json_data['C']['C']))} 
c = {'S': bytesToObject(json_data['S'], group) ,'C':C_}
print c

但我收到以下错误:

    Exception happened during processing of request from ('192.168.1.8', 52588)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
    method()
  File "server.py", line 228, in do_GET
    json_data = json.loads(f.read())
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

我正在做所有这些修改,因为我必须在设备之间传输文件。任何建议都会有所帮助并深表感谢......

4

2 回答 2

0

我正在使用pickle模块来序列化/反序列化数据,以便能够传输原始 Python 对象:

import pickle

def ObjectToString(obj):
    return pickle.dumps(obj, protocol=2)

def StringToObject(inp):
    return pickle.loads(inp)

您可以将您的 json 字典(或纯文本,如果您需要传输文件内容)传递到设备上的 ObjectToString,将数据移动到另一个设备,使用 StringToObject 解包并在接收器上获取原始对象。

于 2016-01-14T21:27:22.740 回答
0

您正在加载的字符串json.loads()不是有效的 json。尝试使用json.dump()orjson.dumps()代替f.write()

于 2016-01-14T21:22:49.240 回答