ubuntu系统安装charm-crypto

ubuntu系统安装charm-crypto,第1张

charm-crypto 安装 一、安装环境
  • ubuntu 16.04
  • python 3.4.3
  • pip3 6.0.8
二、安装步骤 2.1 首先安装部分所需组件
  • m4、flex、bison组件
sudo apt-get install m4
sudo apt-get install flex
sudo apt-get install bison
  • python安装相关组件
pip3 install setuptools
pip3 install distribute==0.7.3 
pip3 install pyparsing==1.5.6
2.2 安装gmp
  • 下载地址:https://ftp.gnu.org/gnu/gmp/
  • 解压后执行如下命令
./configure --enable-cxx
sudo make
sudo make check
sudo make install
2.3 安装pbc
  • 下载地址:https://crypto.stanford.edu/pbc/files/pbc-0.5.14.tar.gz
  • 解压后执行如下命令
./configure 
sudo make
sudo make check
sudo make install
2.3 安装Charm
  • 下载地址:https://github.com/JHUISI/charm/releases/download/v0.43/Charm-Crypto-0.43_Python3.tar.gz
  • 将压缩包解压后对setup.py文件修改
    • 将第二行进行注释
      • #use_setuptools() #bootstrap installs Distribute if not installed
  • 之后 *** 作如下:
./configure 
sudo make
sudo make check
sudo make install
  • 如果make check报错没关系继续运行make install
三、修改libpbc链接库
  • 修改/etc/ld.so.conf添加libpbc.so.1路径
    • 路径查看如下:find / -name libpbc.so.1
四、测试
  • 测试代码
from charm.toolbox.pairinggroup import PairingGroup, ZR, G1, G2, GT, pair
from charm.toolbox.secretutil import SecretUtil
from charm.toolbox.ABEnc import ABEnc, Input, Output

# type annotations
pk_t = {'g': G1, 'g2': G2, 'h': G1, 'f': G1, 'e_gg_alpha': GT}
mk_t = {'beta': ZR, 'g2_alpha': G2}
sk_t = {'D': G2, 'Dj': G2, 'Djp': G1, 'S': str}
ct_t = {'C_tilde': GT, 'C': G1, 'Cy': G1, 'Cyp': G2}

debug = False


class CPabe_BSW07(ABEnc):
    """
    >>> from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
    >>> group = PairingGroup('SS512')
    >>> cpabe = CPabe_BSW07(group)
    >>> msg = group.random(GT)
    >>> attributes = ['ONE', 'TWO', 'THREE']
    >>> access_policy = '((four or three) and (three or one))'
    >>> (master_public_key, master_key) = cpabe.setup()
    >>> secret_key = cpabe.keygen(master_public_key, master_key, attributes)
    >>> cipher_text = cpabe.encrypt(master_public_key, msg, access_policy)
    >>> decrypted_msg = cpabe.decrypt(master_public_key, secret_key, cipher_text)
    >>> msg == decrypted_msg
    True
    """

    def __init__(self, groupObj):
        ABEnc.__init__(self)
        global util, group
        util = SecretUtil(groupObj, verbose=False)
        group = groupObj

    @Output(pk_t, mk_t)
    def setup(self):
        g, gp = group.random(G1), group.random(G2)
        alpha, beta = group.random(ZR), group.random(ZR)
        # initialize pre-processing for generators
        g.initPP();
        gp.initPP()

        h = g ** beta;
        f = g ** ~beta
        e_gg_alpha = pair(g, gp ** alpha)

        pk = {'g': g, 'g2': gp, 'h': h, 'f': f, 'e_gg_alpha': e_gg_alpha}
        mk = {'beta': beta, 'g2_alpha': gp ** alpha}
        return (pk, mk)

    @Input(pk_t, mk_t, [str])
    @Output(sk_t)
    def keygen(self, pk, mk, S):
        r = group.random()
        g_r = (pk['g2'] ** r)
        D = (mk['g2_alpha'] * g_r) ** (1 / mk['beta'])
        D_j, D_j_pr = {}, {}
        for j in S:
            r_j = group.random()
            D_j[j] = g_r * (group.hash(j, G2) ** r_j)
            D_j_pr[j] = pk['g'] ** r_j
        return {'D': D, 'Dj': D_j, 'Djp': D_j_pr, 'S': S}

    @Input(pk_t, GT, str)
    @Output(ct_t)
    def encrypt(self, pk, M, policy_str):
        policy = util.createPolicy(policy_str)
        a_list = util.getAttributeList(policy)
        s = group.random(ZR)
        shares = util.calculateSharesDict(s, policy)

        C = pk['h'] ** s
        C_y, C_y_pr = {}, {}
        for i in shares.keys():
            j = util.strip_index(i)
            C_y[i] = pk['g'] ** shares[i]
            C_y_pr[i] = group.hash(j, G2) ** shares[i]

        return {'C_tilde': (pk['e_gg_alpha'] ** s) * M,
                'C': C, 'Cy': C_y, 'Cyp': C_y_pr, 'policy': policy_str, 'attributes': a_list}

    @Input(pk_t, sk_t, ct_t)
    @Output(GT)
    def decrypt(self, pk, sk, ct):
        policy = util.createPolicy(ct['policy'])
        pruned_list = util.prune(policy, sk['S'])
        if pruned_list == False:
            return False
        z = util.getCoefficients(policy)
        A = 1
        for i in pruned_list:
            j = i.getAttributeAndIndex();
            k = i.getAttribute()
            A *= (pair(ct['Cy'][j], sk['Dj'][k]) / pair(sk['Djp'][k], ct['Cyp'][j])) ** z[j]

        return ct['C_tilde'] / (pair(ct['C'], sk['D']) / A)


def main():
    groupObj = PairingGroup('SS512')

    cpabe = CPabe_BSW07(groupObj)
    attrs = ['ONE', 'TWO', 'THREE']
    access_policy = '((four or three) and (three or one))'
    if debug:
        print("Attributes =>", attrs);
        print("Policy =>", access_policy)

    (pk, mk) = cpabe.setup()

    sk = cpabe.keygen(pk, mk, attrs)
    print("sk :=>", sk)

    rand_msg = groupObj.random(GT)
    if debug: print("msg =>", rand_msg)
    ct = cpabe.encrypt(pk, rand_msg, access_policy)
    if debug: print("\n\nCiphertext...\n")
    groupObj.debug(ct)

    rec_msg = cpabe.decrypt(pk, sk, ct)
    if debug: print("\n\nDecrypt...\n")
    if debug: print("Rec msg =>", rec_msg)

    assert rand_msg == rec_msg, "FAILED Decryption: message is incorrect"
    if debug: print("Successful Decryption!!!")


if __name__ == "__main__":
    debug = True
    main()

  • 结果
参考
  • https://blog.csdn.net/qq_37272891/article/details/99631319
  • https://blog.csdn.net/thereblue/article/details/107854261

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/716216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存