package Util; /*** FileName: Util.Util* Author:   star* Date:     2019/10/24 17:27* Description: 一些公共的处理参数或者返回结果的方法* History:* <author>          <time>          <version>          <desc>* 作者姓名           修改时间           版本号              描述*/import BCPA.roles.BCPA_PKG;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;/*** 〈一句话功能简述〉<br> * 〈一些公共的处理参数或者返回结果的方法〉** @author star* @create 2019/10/24* @since 1.0.0*/
public class Util {private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();private static Pairing pairing;//从PKG设置pairing方便后续使用public static void setPairing(Pairing pairing) {Util.pairing = pairing;}//16进制的byte[]数组转换为字符串public static String hexBytesToString(byte[] bytes) {char[] hexChars = new char[bytes.length * 2];for (int j = 0; j < bytes.length; j++) {int v = bytes[j] & 0xFF;hexChars[j * 2] = HEX_ARRAY[v >>> 4];hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];}return new String(hexChars);}//16进制的字符串转换为byte[]数组public static byte[] hexStringToBytes(String s) {int len = s.length();byte[] data = new byte[len / 2];for (int i = 0; i < len; i += 2) {data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)+ Character.digit(s.charAt(i+1), 16));}return data;}//G1中获取随机元素,获取1,获取0public static Element getRandomFromG1() {return pairing.getG1().newRandomElement().getImmutable();}public static Element getOneFromG1() {return pairing.getG1().newOneElement().getImmutable();}public static Element getZeroFromG1() {return pairing.getG1().newZeroElement().getImmutable();}//Zp中获取随机元素,获取1,获取0public static Element getRandomFromZp() {return pairing.getZr().newRandomElement().getImmutable();}public static Element getOneFromZp() {return pairing.getZr().newOneElement().getImmutable();}public static Element getZeroFromZp() {return pairing.getZr().newZeroElement().getImmutable();}//H1,H2 : {0, 1}∗ → G1public static Element hashFromStringToG1(String str) {return pairing.getG1().newElement().setFromHash(str.getBytes(), 0, str.length()).getImmutable();}public static Element hashFromBytesToG1(byte[] bytes) {return pairing.getG1().newElement().setFromHash(bytes, 0, bytes.length).getImmutable();}//H : {0, 1}∗ → Zppublic static Element hashFromStringToZp(String str) {return pairing.getZr().newElement().setFromHash(str.getBytes(), 0, str.length()).getImmutable();}public static Element hashFromBytesToZp( byte[] bytes) {return pairing.getZr().newElement().setFromHash(bytes, 0, bytes.length).getImmutable();}//h : G1 → Zppublic static Element hashFromG1ToZp( Element g1_element) {// h(y) : G1 -> Zpbyte[] g1_bytes = g1_element.getImmutable().toCanonicalRepresentation();byte[] zp_bytes = g1_bytes;try {MessageDigest hasher = MessageDigest.getInstance("SHA-512");zp_bytes = hasher.digest(g1_bytes);   //先把G1元素hash成512bits} catch (Exception e) {e.printStackTrace();}//再把hash后的bits映射到ZpElement hash_result = pairing.getZr().newElementFromHash(zp_bytes, 0, zp_bytes.length).getImmutable();return hash_result;}//{0,1}* -> key space of πkey  int空间public static int h1_pai_key(String data) {try {MessageDigest hasher = MessageDigest.getInstance("SHA-256");byte[] result = hasher.digest(data.getBytes());ByteBuffer wrapped = ByteBuffer.wrap(result);return wrapped.getShort();} catch (Exception e) {e.printStackTrace();return -1;}}//伪随机置换 pseudorandom permutation πkey() 用于随机选择哪些块进行抽查public static List<Integer> pseudoPerm(int key, int n, int c) {List<Integer> result = new ArrayList<Integer>(c);if(c < n) {List<Integer> list = new ArrayList<>(n);for(int i = 0; i < n; i ++) {list.add(i);}for(int i = 0; i < key; i ++)java.util.Collections.shuffle(list);for(int i = 0; i < c; i ++) {result.add(list.get(i));}} else {System.out.println(" pseudorandom permutation error!");}return result;}//{0,1}* -> key space of fkey 字符串空间public static String h2_f_key(String data) {try {MessageDigest hasher = MessageDigest.getInstance("SHA-512");byte[] result = hasher.digest(data.getBytes());return result.toString();} catch (Exception e) {e.printStackTrace();return "error";}}//伪随机函数 pseudorandom function fkey():{0,1}* -> Zppublic static Element pseudoFunc(String key, int id) {try {MessageDigest hasher = MessageDigest.getInstance("SHA-512");byte[] hash_bytes = hasher.digest((key + id).getBytes());   //先把G1元素hash成512bitsreturn pairing.getZr().newElementFromHash(hash_bytes, 0, hash_bytes.length).getImmutable();} catch (Exception e) {e.printStackTrace();return pairing.getZr().newRandomElement();}}public static void main(String[] args) throws Exception {BCPA_PKG pkg = new BCPA_PKG();System.out.println(Util.hashFromStringToZp(123+"name"));System.out.println(Util.hashFromStringToZp(123+"name"));System.out.println(Util.hashFromStringToZp(111+"name"));}
}

关于JPBC的密码学实现参考:

https://blog.csdn.net/zhangwenjiezw886/article/details/51006774

https://blog.csdn.net/liuweiran900217/article/details/23414629

https://blog.csdn.net/qifuchenluo/article/details/45100851

http://gas.dia.unisa.it/projects/jpbc/java-docs/api/index.html

JPBC密码学库封装函数相关推荐

  1. linux封装函数,libc库和封装函数 | 求索阁

    Linux系统调用这部分经常出现两个词:libc库和封装函数,不知道你是否清楚它们的含义? libc 1)libc概念 libc是Stantard C Library的简称,它是符合ANSI C标准的 ...

  2. js的常用封装函数库之Number操作

    js的常用封装函数库之Number操作: /* * 函数功能:Number */class NumberFn {/*随机数范围*/random (min, max) {if (arguments.le ...

  3. c语言添加miracl库,密码学C语言函数库——Miracl库快速上手中文指南(VC)

    一.简介 密码学学习.研究人员往往着重于理论研究,难以与实践直接挂钩,今天介绍一下国外著名密码学C语言函数库--Miracl库的使用方法. 该库针对公钥密码学和椭圆曲线密码学的实现,写了很多函数,在这 ...

  4. #STM32标准固件库的硬件SPI(NSS为软件)封装函数库

    #基于STM32标准固件库的硬件SPI(NSS为软件)封装函数库: 最近来回顾之前写过的SPI_Flash,打算重写一次SPI配置文件 spi协议最大的特点大概就是全双工了,因此stm32硬件spi的 ...

  5. 密码学C语言函数库——Miracl库快速上手中文指南(VC)

    一.简介 密码学学习.研究人员往往着重于理论研究,难以与实践直接挂钩,今天介绍一下国外著名密码学C语言函数库--Miracl库的使用方法. Miracl库的官方网站是http://www.shamus ...

  6. 从封装函数到实现简易版自用jQuery (一)

    温馨提示 本文阅读对象: 对 JavaScript 有一定的了解,如果你没有学过或者忘记 JavaScript 某些操作,请看 阮一峰 JavaScript 教程 . 导语 DOM 有许多 API , ...

  7. CUDA动态库封装以及调用

    CUDA动态库封装以及调用 参考:http://blog.sina.com.cn/s/blog_618941f701016d26.html 通过将CUDA相关计算操作放在库中,方便在项目中调用,省去了 ...

  8. 关于js封装函数的一些东西

    关于封装函数,函数封装是一种函数的功能,它把一个程序员写的一个或者多个功能通过函数.类的方式封装起来,对外只提供一个简单的函数接口.当程序员在写程序的过程中需要执行同样的操作时,程序员(调用者)不需要 ...

  9. VC++动态库封装及调用

    https://blog.csdn.net/zhangfuliang123/article/details/71515796 一直对动态库的封装理解不是很透彻,虽然之前写过一个Demo,不过并没有真正 ...

最新文章

  1. 【android-tips】关于android应用R文件无法读取
  2. python有参装饰器 多个装饰器装饰一个
  3. 贾斯帕马斯基林的故事笔记
  4. mysql导入导出数据库文件(转载)
  5. Spring Cloud的架构
  6. 单点登录 之 OAuth
  7. 2018.10.22 20:10
  8. 写代码需要注意的几方面
  9. vs2017_enterprise正式版离线安装包bt下载
  10. C++11 继承构造函数与委托构造函数
  11. C++实现一个基于mfc的学生信息管理系统
  12. origin将柱状图和折线图画一起
  13. 如何简单轻松的把360度全景图变成视频
  14. PCA与2DPCA及2D-2DPCA零基础理解(上)
  15. 怒爬某破Hub站资源,只为撸这个鉴黄平台!
  16. 36-基于51单片机士壤湿度检测及自动浇花系统
  17. (C++/python)LeetCode 589. N叉树的前序遍历
  18. Grails(Java笨狗)系列-更好的理解闭包(closure)
  19. pulltorefresh+recycleview 实现的瀑布流(带下拉刷新,上拉加载更多)
  20. 实验一段有趣的js代码。

热门文章

  1. 阿卡迪亚大学计算机专业好考吗,普通高中学生如何考取阿卡迪亚大学?
  2. ngx_http_core_module模块提供的变量
  3. linux 建立http连接失败,【linux】http请求建立连接的时候为啥是tcp三次握手,而不是二次或者四次?...
  4. [Kerberos基础]-- kerberos认证原理---讲的非常细致,易懂
  5. 生物特征识别六大技术,你知道多少?
  6. 【T3】win10系统成功注册加密锁后,登录软件依然提示“产品未找到合法的license授权”
  7. flowchart图
  8. iOS和Android和H5交互WebViewJavascriptBridge
  9. 嵌入式Linux的MiniGUI研究和移植
  10. PAT乙级——1003