我有一个密码可以加密一串数字。
import piheaan as heaan
import numpy as np
# Step 1. Setting Parameters
params = heaan.ParameterPreset.SS7
context = heaan.make_context(params)
# Step 2. Generating Keys
key_dir_path = "./keys"
sk = heaan.SecretKey(context)
keygen = heaan.KeyGenerator(context, sk)
keygen.gen_common_keys()
pack = keygen.keypack
# Step 3. Encrypt Message to Ciphertext - with a specific vector
enc = heaan.Encryptor(context)
log_slots = 3
msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
for i, value in in enumerate([2,5,4,3]):
msg[i] = value
ctxt = heaan.Ciphertext(context)
enc.encrypt(msg, pack, ctxt)
# Step 4. multiply ciphertexts(i.e. square a ciphertext)
eval = heaan.HomEvaluator(context, pack)
ctxt_out = heaan.Ciphertext(context)
eval.mult(ctxt, ctxt, ctxt_out)
# Step 5. decrypt the ciphertext by Decryptor.
dec = heaan.Decryptor(context)
msg_out = heaan.Message()
dec.decrypt(ctxt_out, sk, msg_out)
#output - it has 9 slots (2 to the power of 3), so only encrypts the first 4 in this case.
msg_out
[ (4.000000+0.000000j), (25.000000+0.000000j), (16.000000+0.000000j), (9.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j) ]
然而,我的实际数据是数组的形式,例如:
l1 = [2, 5, 4, 3]
l2 = [1, 2, 2, 3]
..
arr_in = np.array([l1,l2])
arr_in
# array([[2, 5, 4, 3],
# [1, 2, 2, 3]])
我试过这个
for i, value in np.ndenumerate(arr):
msg[i] = value
但是它不与给出错误的Piheean的消息格式一起工作。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [44], in <cell line: 5>()
4 msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
5 for i, value in np.ndenumerate(arr):
----> 6 msg[i] = value
7 ctxt = heaan.Ciphertext(context)
8 enc.encrypt(msg, pack, ctxt)
TypeError: __setitem__(): incompatible function arguments. The following argument types are supported:
1. (self: piheaan.Message, idx: int, value: complex) -> None
Invoked with: [ (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j) ], (0, 0), 2
我如何编写代码,基本上从数组arr_in中获取每个列表,加密它并将其输出为形式为arr_out,array([[4,25,16,9],[1,4,4,9]])的数组?
1条答案
按热度按时间bnlyeluc1#
如果你打印这个:
您试图将
(0,0)..(1,3)
元组分配给msg[]
。这就是你犯错误的原因。相反,您可以:
msg
是2**3
,即8
值