JPBC密码学库封装函数

2024-06-15 08:38
文章标签 函数 封装 密码学 jpbc

本文主要是介绍JPBC密码学库封装函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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密码学库封装函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062962

相关文章

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高