我正在研究全同态加密。因为只有完全同态加密允许对加密数据执行计算,这种机制由PySeal库提供,它是Microsoft库的python叉版本。我的数据框架中有3列。我希望使用可以对这些值进行计算的PySeal加密每一列的每个值。
df
| SNP | ID | Effect|
|:---- |:------:| -----:|
| 21515| 1 | 0.5 |
| 21256| 2 | 0.7 |
| 21286| 3 | 1.7 |PySeal:https://github.com/Lab41/PySEAL/blob/master/SEALPythonExamples/examples.py的相关文献
发布于 2021-05-29 12:06:21
有趣的问题,我可以帮助你使用熊猫库,但不是设置像模块这样的安全加密参数。
首先,让我们做一些导入:
import pandas
import seal
from seal import Ciphertext, \
Decryptor, \
Encryptor, \
EncryptionParameters, \
Evaluator, \
IntegerEncoder, \
FractionalEncoder, \
KeyGenerator, \
Plaintext, \
SEALContext现在我们设置加密参数。我不知道如何正确设置这些值,但正确设置这些值对于实现适当的安全性非常重要。A引自文件:
了解这些不同参数的行为、它们如何影响加密方案、性能和安全级别是至关重要的。由于本课题的复杂性,我们强烈建议用户直接咨询同态加密和基于RLWE的加密方案的专家,以确定其参数选择的安全性。
parms = EncryptionParameters()
parms.set_poly_modulus("1x^2048 + 1")
parms.set_coeff_modulus(seal.coeff_modulus_128(2048))
parms.set_plain_modulus(1 << 8)
context = SEALContext(parms)接下来,我们将设置密钥、编码器、密码器和解密器。
iEncoder = IntegerEncoder(context.plain_modulus())
fEncoder = FractionalEncoder(
context.plain_modulus(), context.poly_modulus(), 64, 32, 3)
keygen = KeyGenerator(context)
public_key = keygen.public_key()
secret_key = keygen.secret_key()
encryptor = Encryptor(context, public_key)
evaluator = Evaluator(context)
decryptor = Decryptor(context, secret_key)让我们设置一些方便的功能,我们将使用与DataFrames加密和解密。
def iencrypt(ivalue):
iplain = iEncoder.encode(ivalue)
out = Ciphertext()
encryptor.encrypt(iplain, out)
return out
def fencrypt(fvalue):
fplain = fEncoder.encode(fvalue)
out = Ciphertext()
encryptor.encrypt(fplain, out)
return out最后,我们将定义一个可用于熊猫的整数的乘法运算。为了简短地回答这个问题,我们不会演示对浮点数的操作,但是做一个操作应该不难。
def i_multiplied(multiplier):
m_plain = iEncoder.encode(multiplier)
out = Ciphertext()
encryptor.encrypt(m_plain, out)
def aux(enc_value):
# this is an in-place operation, so there is nothing to return
evaluator.multiply(enc_value, out)
return aux注意,Evaluator.multiple是一个内嵌操作,所以当我们将它与一个DataFrame一起使用时,它会改变里面的值!
现在,让我们把这一切付诸实施:
df = pandas.DataFrame(dict(
SNP=[21515, 21256, 21286],
ID=[1, 2, 3],
Effect=[0.5, 0.7, 1.7])
)
print("Input/Plaintext Values:")
print(df.head())这将打印您的示例:
Input/Plaintext Values:
SNP ID Effect
0 21515 1 0.5
1 21256 2 0.7
2 21286 3 1.7现在让我们来做一个加密的数据文件:
enc_df = pandas.DataFrame(dict(
xSNP=df['SNP'].apply(iencrypt),
xID=df['ID'].apply(iencrypt),
xEffect=df['Effect'].apply(fencrypt))
)
print("Encrypted Values:")
print(enc_df.head())指纹:
加密值:
_ xSNP
0 <seal.Ciphertext object at 0x7efcccfc2df8> <seal.Ciphertext object a
1 <seal.Ciphertext object at 0x7efcccfc2d88> <seal.Ciphertext object a
2 <seal.Ciphertext object at 0x7efcccfc2dc0> <seal.Ciphertext object a它只是DataFrame中的一堆对象。
现在我们来做个手术。
# multiply in place
enc_df[['xSNP','xID']].applymap(i_multiplied(2))
print("Encrypted Post-Op Values:")
print(enc_df.head())您不会注意到此时打印的值有什么不同,因为我们所做的只是对dataframe中的对象进行变异,所以它只会打印相同的内存引用。
现在让我们解密以查看结果:
enc_df[['xSNP','xID']]=enc_df[['xSNP','xID']].applymap(idecrypt)
print("Decrypted Post-Op Values:")
print(enc_df[['xSNP','xID']].head())这些指纹:
Decrypted Post-Op Values:
xSNP xID
0 43030 2
1 42512 4
2 42572 6这就是您期望的结果,将整数列乘以2。
要实际使用这一点,您必须先序列化加密的数据文件,然后发送到另一方进行处理,然后返回给您进行解密。图书馆强迫你用泡菜做这件事。从安全性的角度来看,这是不幸的,因为您是不应对不受信任的数据进行解密。。服务器可以信任客户机不会在泡菜串行化中放置任何讨厌的东西吗?当服务器返回答案时,客户端能相信服务器不会这样做吗?一般来说,这两个问题的答案都是否定的,更多的是因为客户端已经不信任服务器,否则就不会使用同态加密了!显然,这些python绑定更像是一个技术演示器,但我认为这是值得指出的限制。
库中有批处理操作,我还没有演示过。在DataFrames上下文中使用这些可能更有意义,因为它们应该比许多值具有更好的操作性能。
https://stackoverflow.com/questions/67731271
复制相似问题