请帮我使用Jpair

时间:2014-04-20 09:04:05

标签: java android

请帮助我帮助我使用Jpair_v1.03这个图书馆。因为我不明白他是如何工作的。

关于Jpair

Jpair是双线性配对的纯Java实现。它不依赖于外部库。虽然未经过全面测试,但它应与1.2以上的任何Java版本兼容。事实上,我已经在Android平台上使用它而不更改任何代码。

文档来源:

https://personal.cis.strath.ac.uk/changyu.dong/jpair/jpair.html

我很抱歉我的英语水平不好

这就是我所做的,但他没有工作:

package jpair;
import uk.ac.ic.doc.jpair.api.Pairing;
import uk.ac.ic.doc.jpair.ibe.BFCipher;
import uk.ac.ic.doc.jpair.ibe.BFCtext;
import uk.ac.ic.doc.jpair.ibe.key.BFMasterPublicKey;
import uk.ac.ic.doc.jpair.ibe.key.BFUserPrivateKey;
import uk.ac.ic.doc.jpair.ibe.key.BFUserPublicKey;
import uk.ac.ic.doc.jpair.pairing.*;
import uk.ac.ic.doc.jpair.api.*;

import java.security.KeyPair;
import java.util.Random;


public class main {

public static void main(String[] args){
     System.out.println("debut");


    //using a predefined pairing
     Pairing e = Predefined.nssTate();

     //get P, which is a random point in group G1 
     Point P = e.RandomPointInG1(new Random());

     //get Q, which is a random point in group G2 
     Point Q = e.RandomPointInG2(new Random());


    //compute e(P,Q)
     FieldElement epq =e.compute(P,Q);

    //the curve on which G1 is defined 
     EllipticCurve g1 = e.getCurve();
    //a is a 160-bit random integer
    BigInt a = new BigInt(160,new Random()); 
    //Point aP is computed over G1
    Point aP = g1.multiply(P, a);


    //The curve on which G2 is defined
     EllipticCurve g2 = e.getCurve2();
    //b is a 160-bit random integer
    BigInt b = new BigInt(160,new Random());
    //bQ is computed over G2
    Point bQ = g2.multiply(Q, b);

    //compute e(aP,bQ)
     FieldElement res = e.compute(aP,bQ);

     //compute e(P,Q)^ab, this is done in group Gt
     BigInt ab = a.multiply(b);
     //get the field on which Gt is defined
     Field gt = e.getGt();
     FieldElement res2 = gt.pow(epq,ab); 

     //compare these two

     if(res.equals(res2))
     {
         System.out.println("Correct! e(aP,bQ) = e(P,Q)^ab");
     }
     else{
         System.out.println("Something is wrong! e(aP,BQ) != e(P,Q)^ab"); 
         }
     //debut du test -->

     java.security.KeyPair masterKey=BFCipher.setup( e ,new Random());
     System.out.println(masterKey);

     java.security.KeyPair pk=BFCipher.extract( masterKey,"Nabil",new Random());
     System.out.println(pk);

     byte msg[]= new byte[50];
     String s = "test message";
     msg = s.getBytes();


        BFMasterPublicKey sdf = new BFMasterPublicKey(e,  P, aP) ;
        BFUserPublicKey m = new BFUserPublicKey("Nabil",sdf);
        System.out.println();

        BFCtext i=BFCipher.encrypt( m, msg, new Random()) ;

        BFUserPrivateKey sk = new BFUserPrivateKey(aP,sdf) ;

        byte msgre[]=new byte[50];

        msgre=BFCipher.decrypt(i, sk) ;

        System.out.println(msgre);// I am don't understand why to decrypt he doesn't work. Please help

     for(int j=0 ; j<msgre.length ; j++)
            System.out.print((char)msgre[j]);
    }



}

1 个答案:

答案 0 :(得分:0)

问题在于encrypt()和decrypt()函数的参数。

根据理论:

对于encrypt(), 第一个参数必须是从提取阶段

生成的用户公钥

用于解密()。第二个参数必须是用户私钥,再次从提取阶段

生成

尝试以下代码。它会起作用。

 /*
 * ENCRYPTION USING Jpair SCHEME
 * DONE ACCORDING IBE SCHEME
 */

package ibe;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Random;
import uk.ac.ic.doc.jpair.api.*;
import uk.ac.ic.doc.jpair.ibe.*;
import uk.ac.ic.doc.jpair.ibe.key.*;
import uk.ac.ic.doc.jpair.pairing.*;

public class Main {
    public static void main(String[] args) {

        //  GENERATE PAIRING "e"
        Pairing e = Predefined.nssTate();

        // SETUP
        java.security.KeyPair masterKey=BFCipher.setup( e ,new Random());


        //  EXTRACT
        java.security.KeyPair userKey=BFCipher.extract( masterKey,"abcd",new Random());
        //"abcd" can be replaced with an ID for user
        PublicKey uPub = userKey.getPublic();       //to use in encrypt()
        PrivateKey uPri = userKey.getPrivate();     //to use in decrypt()

        //  ENCRYPT
        byte msgenc[]= new byte[50];
        String s = "test message";
        msgenc = s.getBytes();
        System.out.println("MESSAGE BEFORE ENCRYPTION : " + s);

        BFCtext msgCipher = BFCipher.encrypt((BFUserPublicKey) uPub,msgenc, new Random()) ;

        //  DECRYPT
        byte msgdec[]=new byte[50];
        msgdec=BFCipher.decrypt(msgCipher, (BFUserPrivateKey) uPri) ;
        System.out.println("AFTER DECRYPTION : " );
        // Printing message
        for(int j = 0; j < msgdec.length; j++) {
            System.out.print((char)msgdec[j]);
        }

    }



}

这将输出为:

加密前的消息:测试消息 解密后:测试消息