Boneh的PEKS的python实现

我们将大数据存储在云服务器上,为了保护数据隐私,通常会选择先将数据加密后再上传。可搜索加密(Searchable Encryption)研究如何在密文上进行关键字搜索,分为对称可搜索加密(Searchable Symmetric Encryption)和公钥可搜索加密(Public-key Encryption with Keyword Search,PEKS)。

   虽然对称可搜索加密比较快,但在多用户数据共享方面,公钥可搜索加密的应用场景比对称可搜索加密更为广阔。双线性对(Bilinear Pairing)作为构造PEKS的常用工具,本文将贴出其相应开源函数库Pairing-based cryptography library (pbc library,链接)的python版本示例(pypbc), 目标是方便大家在做学术研究工程实验时能够对pypbc进行快速入门。 (注: 本文代码以2004年Boneh等人的方案为示例,仅面向研究人员,入门请结合该论文方案;因方案不能抵抗关键字猜测攻击,不保证商用安全性)

实验环境
pypbc 基于GMP 和PBC,安装可以参考64位Ubuntu14.04下配置PBC环境一文。先给出完整依赖关系,pbc依赖gmp,gmp依赖M4,bison,flex,我们先安装M4、flex和bison。

sudo apt-get install M4
sudo apt-get install flex
sudo apt-get install bison
安装gmp,下载https://gmplib.org/ ,转到下载目录运行如下命令。

lzip -d gmp-6.1.0.tar.lz
tar -zxvf gmp-6.1.0.tar
cd gmp-6.1.0

./configure
make
make check
sudo make install
接下来安装pbc, 下载https://crypto.stanford.edu/pbc/download.html ,转到下载目录运行如下命令。

tar -zxvf pbc-0.5.14.tar.gz
cd pbc-0.5.14

./configure
make
sudo make install
因为pypbc是用python3 语言,我们先安装python3和pip3。

sudo apt-get install python3
sudo apt-get install python3-pip
我们运行如下命令,从GitHub上下载pypbc代码。

git clone https://github.com/debatem1/pypbc
转到下载目录,运行如下代码,具体过程我不太记得了,请参考pypbc/INSTALL文件里的描述。

sudo python3 setup.py install
sudo pip3 install pypbc

Pypbc代码
代码请结合2004年Boneh的论文方案来阅读,其中[params, g]是公共参数,近来我在看学术论文时经常看到公共参数生成算法,该算法返回公共参数pp,pp作为方案其余各算法的隐含输入,这样写法是为了结合工程习惯。Pbc library公共参数生成算法中的qbits和rbits用法具体请阅读官方文档,

Generate type A pairing parameters and store them in p, where the
group order r is rbits long, and the order of the base field q is
qbits long. Elements take qbits to represent.
我对这块不熟悉,参数是根据官方示例设置的(再次强调代码示例不保证商用安全性)。

#coding=utf-8

from pypbc import *
import hashlib

Hash1 = hashlib.sha256
Hash2 = hashlib.sha256

公钥可搜索加密-2004-Boneh

密钥生成算法,输入安全参数qbits和rbits,返回[params, g, pk, sk]

def KeyGen(qbits=512, rbits=160):
params = Parameters(qbits=qbits, rbits=rbits)
pairing = Pairing(params)
g = Element.random(pairing, G2)
sk = Element.random(pairing, Zr)
pk = Element(pairing, G2, value = g ** sk)
return [params, g, sk, pk]

PEKS算法,输入公共参数[params, g],公钥pk,关键字word,返回[A, B] (具体参考论文)

def PEKS(params, g, pk, word):
pairing = Pairing(params)
hash_value = Element.from_hash(pairing, G1, Hash1(str(word).encode(‘utf-8’)).hexdigest())
r = Element.random(pairing, Zr)
temp = pairing.apply(hash_value, pk ** r)
return [g ** r, Hash2(str(temp).encode(‘utf-8’)).hexdigest()]

陷门生成算法,输入公共参数[params],私钥sk,待查关键字word,返回陷门td

def Trapdoor(params, sk, word):
pairing = Pairing(params)
hash_value = Element.from_hash(pairing, G1, Hash1(str(word).encode(‘utf-8’)).hexdigest())
return hash_value ** sk

测试算法,输入公共参数[params],公钥pk,S=[A, B],陷门td,返回布尔值True/False

def Test(params, pk, cipher, td):
pairing = Pairing(params)
[A, B] = cipher
td = Element(pairing, G1, value=str(td))
temp = pairing.apply(td, A)
temp = Hash2(str(temp).encode(‘utf-8’)).hexdigest()
return temp == B

测试代码
if name == ‘main’:
[params, g, sk, pk] = KeyGen(512, 160)
cipher = PEKS(params, g, pk, “GQW”)
td = Trapdoor(params, sk, “GQW”)
assert(Test(params, pk, cipher, td))
td = Trapdoor(params, sk, “GQK”)
assert(not Test(params, pk, cipher, td))

官方示例
pypbc的详细用法请参考官方示例(链接),测试代码中给出了BLS签名实现。共勉,愿科研人员前途似锦!

Acknowledgements
The main author of the PBC library is Ben Lynn.

Thanks to the many others who have contributed, including Hovav Shacham, Matt Steiner, Joe Cooley, Rob Figueiredo, Roger Khazan, Dmitry Kosolapov, John Bethencourt, Paul Miller, Michael Cheng, Ian Goldberg, Adam Aviv, Adam McLaurin, and Michael Adjedj.

Reference
64位Ubuntu14.04下配置PBC环境

Pypbc的GitHub地址

https://crypto.stanford.edu/pbc/download.html

Dan B, Crescenzo G D, Ostrovsky R, et al. Public Key Encryption with Keyword Search[C]// International Conference on the Theory and Applications of Cryptographic Techniques. 2004.

转载于:https://my.oschina.net/keyven/blog/1600820

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值