Esempio n. 1
0
void ABEDemo() {
  std::cout << "ABE" << std::endl;

  const size_t Q = size_t(1) << K;

  std::cout << "generating samples ..." << std::endl;
  long* samples = gring::readOrBuildSamples<N,Q,K>( "samples", SS );

  std::cout << "creating master secret ..." << std::endl;
  auto msk = new gring::ABEMasterSecret<N,Q,K>{L, samples};
  std::cout << "creating public..." << std::endl;
  auto pk = msk->publicKey();

  std::vector< size_t > addWeights{1, 2, 3, 4, 5};
  auto gAdd = new gring::ABEAddGate<N,Q,K>{addWeights};
  gAdd->print();

  std::vector< size_t > mulWeights{1}; 
  auto gMul = new gring::ABEMulGate<N,Q,K>{mulWeights};
  gMul->print();

  std::vector< size_t > badWeights{5, 4, 3, 2, 1};
  auto gBad = new gring::ABEAddGate<N,Q,K>{badWeights};
  gBad->print();

  std::cout << "creating secret keys ..." << std::endl;
  auto skAdd = msk->keyGen( gAdd );
  auto skMul = msk->keyGen( gMul );
  auto skBad = msk->keyGen( gBad );

  std::vector< size_t > ident{0, Q-1, Q-1, 0, 1};
  auto abeId = gring::ABEId{ident};
  abeId.print();

  std::cout << "encrypting ..." << std::endl;
  auto ctxt = pk->encrypt( abeId, msg );

  std::cout << "decrypting with skAdd ..." << std::endl;
  std::string msgPrimeAdd = skAdd->decrypt( ctxt );
  std::cout << "msgPrimeAdd: " << msgPrimeAdd << std::endl;
  std::cout << std::endl;

  std::cout << "decrypting with skMul ..." << std::endl;
  std::string msgPrimeMul = skMul->decrypt( ctxt );
  std::cout << "msgPrimeMul: " << msgPrimeMul << std::endl;
  std::cout << std::endl;

  std::cout << "decrypting with skBad ..." << std::endl;
  std::string msgPrimeBad = skBad->decrypt( ctxt );
  std::cout << "msgPrimeBad: " << msgPrimeBad << std::endl;

  delete [] samples;
}
Esempio n. 2
0
bool hashTable::remove(char * product){
  node * curr;
  node * prev;
  char aProduct[100];

  int key;
  key = keyGen(product);
  curr = table[key];
  prev = table[key];
  curr->data.getProduct(aProduct);
  if(aProduct == product){  //head of list removed
    table[key] = curr->next;
    delete curr;
    curr = nullptr;
    return true;
  }
  curr = curr->next;
  while(curr){
    curr->data.getProduct(aProduct);
    if(aProduct == product){
      prev->next = curr->next;
      delete curr;
      curr = nullptr;
      return true;
    }else{
      curr = curr->next;
      prev = prev->next;
    }
  }
  return false;
}
Esempio n. 3
0
void hashTable::add(node * newNode){
  int key;
  char product[100];
  newNode->data.getProduct(product);
  key = keyGen(product);

  //first node of table[key]
  if(table[key] == nullptr){
    newNode->next = nullptr;
    table[key] = newNode;
    return;
  }
  else{
    node * curr;
    node * prev;
    prev = nullptr;
    curr = table[key];
    /********************************************************
    * Sort nodes alphabetically
    ********************************************************/

	//get names to compare
	char newName[100];
	newNode->data.getName(newName);
	char currentName[100];
	curr->data.getName(currentName);

    //create new head
    if(newName < currentName){
      newNode->next = table[key];
      table[key] = newNode;
      return;
    }else{
      while(curr != nullptr && currentName < newName){
        //advance to next node
		prev = curr;
        curr = curr->next;
		//get name of next node
		curr->data.getName(currentName);
        }
        //reached end of list, new node to tail
        if(curr == nullptr){
            prev->next = newNode;
            newNode->next = nullptr;
            return;
        }else{
            prev->next = newNode;
            newNode->next = curr;
            return;
        }
    }
  }
}
Esempio n. 4
0
bool hashTable::retrieve(char * product, vendor& aVendor){
  int key;
  key = keyGen(product);
  char aProduct[100];

  node * curr;
  curr = table[key];
  while(curr){
    curr->data.getProduct(aProduct);
    if(aProduct == product){
      aVendor = curr->data;
      return true;  //return true if products match
    }
    curr = curr->next;
  }
  return false; //reached end of list and no match found
}
Esempio n. 5
0
    master_conn::master_conn() {
        typedef botnet::http::http_request http_request;
        typedef botnet::http::http_client http_client;
        std::string content;
        std::string confirm_srv;
        std::vector<std::string> arr;
        count_conn = 1;
        char* alex = "alex";

        std::string host = HOST + ':' + std::to_string(PORT);

        http_request *http_req = new http_request(botnet::http::GET);
        http_request::headers header;
        header.add("Host", host);
        client_key = keyGen();
        confirm_msg = rand();
        std::string path = "?k_c=" + std::to_string(client_key) +
            "&confirm=" + std::to_string(confirm_msg);

        http_req->setPath(path);
        http_req->setHeaders(header);

        http_client *http_c = new http_client(HOST, PORT);
        http_c->request(http_req);

        content = http_c->getContent();

        arr = split(content, "\n");

        if (arr.size() < 3)
            return;

        client_id = base64_decode(arr[1]);

        hash_key = combineKeys(std::atoi(base64_decode(arr[2]).c_str()));

        confirm_srv = arr[3];

        // Delete the last char because node put a \r in the end
        confirm_srv = confirm_srv.substr(0, confirm_srv.length() - 1);

        bool b = confirm(base64_decode(confirm_srv));
    }
Esempio n. 6
0
   // PD_TRACE_DECLARE_FUNCTION ( SDB__IXMINXCB_GETKEY, "_ixmIndexCB::getKeysFromObject" )
   INT32 _ixmIndexCB::getKeysFromObject ( const BSONObj &obj,
                                          BSONObjSet &keys ) const
   {
      INT32 rc = SDB_OK ;
      SDB_ASSERT ( _isInitialized,
                   "index details must be initialized first" ) ;
      PD_TRACE_ENTRY ( SDB__IXMINXCB_GETKEY );
      ixmIndexKeyGen keyGen(this) ;
      rc = keyGen.getKeys ( obj, keys ) ;
      if ( rc )
      {
         PD_LOG ( PDERROR, "Failed to generate key from object, rc: %d", rc ) ;
         goto error ;
      }

   done :
      PD_TRACE_EXITRC ( SDB__IXMINXCB_GETKEY, rc );
      return rc ;
   error :
      goto done ;
   }
Esempio n. 7
0
void encrypt(char *x, int *out, int size)
{
    int buf;
    int ind=0;
    int inc=0;
    int pub=nsqrandr(100,1000);
    keyGen(pub);
    int dex=(pub*private2)-(7*private2);
    out[0]=dex^private2;
    while(ind < size && x[ind] != '\0')
    {
        buf=alg(x[ind],pub,inc);
        out[ind+1]=buf;
        ind++;
        inc++;
        if(inc>=strlen(XKey))
            inc=0;
    }
//    print(out, size);

}
Esempio n. 8
0
int main(int argc, char *argv[])
{
	char* infilename;
	char* outfilename;
	unsigned char* init_vector = malloc(8 * sizeof(char));
	unsigned char* init_key = malloc(10 * sizeof(char));
	char* vectorstr;
	char* keystr;
	int decryp = 0;
	//check inputs
	if(argc < 5 || argc > 6)					
	{
		printf("Usage: mycipher [-d] <init_key> <init_vector> <original_file> <result_file>\n");
		return 2;
	}
	if(argc == 5)
	{
		if(strncmp(argv[1], "-d", 2) == 0)
		{
			printf("Usage: mycipher [-d] <init_key> <init_vector> <original_file> <result_file>\n");
			return 3;
		}
	}

	//check for -d decryption
	if(argc == 6)				
	{
		if(strncmp(argv[1], "-d", 2) != 0)
		{
			printf("Usage: mycipher [-d] <init_key> <init_vector> <original_file> <result_file>\n");
			return 3;
		}
		decryp = 1;
		keystr = argv[2];
		vectorstr = argv[3];
		infilename = argv[4];
		outfilename = argv[5];
	}

	else
	{
		keystr = argv[1];
		vectorstr = argv[2];
		infilename = argv[3];
		outfilename = argv[4];
	}

 	//convert key to bit array and check if 0 or 1
 	if(strlen(keystr) == 10)
 	{
	 	init_key[0] = keystr[0] - 0x30;// convert from ascii number to int number
		init_key[1] = keystr[1] - 0x30;
		init_key[2] = keystr[2] - 0x30;
		init_key[3] = keystr[3] - 0x30;
		init_key[4] = keystr[4] - 0x30;
		init_key[5] = keystr[5] - 0x30;
		init_key[6] = keystr[6] - 0x30;
		init_key[7] = keystr[7] - 0x30;
		init_key[8] = keystr[8] - 0x30;
		init_key[9] = keystr[9] - 0x30;
	}

	else
	{
		printf("Initial key needs to be 10 bits only!\n");
		return 6;
	}

	int i;
	for(i = 0; i < 10; i++)
	{
		if((init_key[i] > 1 ))
		{
			printf("Initial key needs to be 0s or 1s only, as in binary\n");
			return 7;
		}
	}

	//convert vector to bit array and check if 0 or 1
	if(strlen(vectorstr) == 8)
	{
	 	init_vector[0] = vectorstr[0] - 0x30;
		init_vector[1] = vectorstr[1] - 0x30;
		init_vector[2] = vectorstr[2] - 0x30;
		init_vector[3] = vectorstr[3] - 0x30;
		init_vector[4] = vectorstr[4] - 0x30;
		init_vector[5] = vectorstr[5] - 0x30;
		init_vector[6] = vectorstr[6] - 0x30;
		init_vector[7] = vectorstr[7] - 0x30;
	}
	else
	{
		printf("Initial vector needs to be 8 bits only!\n");
		return 4;
	}

	for(i = 0; i < 8; i++)
	{
		if(init_vector[i] > 1)
		{
			printf("Initial vector needs to be 0s or 1s only, as in binary\n");
			return 5;
		}
	}


	FILE *in = fopen(infilename, "rb");
												//Error checking if file not found
	if(in == NULL)
	{
		perror("Could not open original_file!\n");
		exit(-1);
	}

	FILE *out = fopen(outfilename, "wb+");
												//Error check if cannot create file
	if(out == NULL)
	{
		perror("Could not create result_file!\n");
		exit(-1);	
	}

	keyGen(init_key);							//generate keys for algorithms

	if(!decryp)
	{
		encrypt(in, out, init_vector);
		printf("Encrypted %s as %s\n", infilename, outfilename);
	}

	else
	{
		decrypt(in, out, init_vector);
		printf("Dencrypted %s as %s\n", infilename, outfilename);
	}

	fclose(in);
	fclose(out);

	return 1;
}
Esempio n. 9
0
File: abe.c Progetto: TzuPingWu/ECC
int main(int argc, char *argv[]){
	//build the pairing function	
	pairing_t pairing;
	if(argc < 2){
		fprintf(stderr,"Wrong input arguments!\n");		
		fprintf(stderr,"Please input <./abe><supersinuglar> or <./abe><ordinary>\n");
	}else{
	if(!strcmp(argv[1],"ordinary")){
		setupOrdinaryPairing(&pairing);
		printf("Use ordinary curve...\n");
	}else if(!strcmp(argv[1],"supersingular")){
		setupSingularPairing(&pairing);//setup pairing first
		printf("Use supersingular curve...\n");
	}else{
		fprintf(stderr,"Wrong input arguments!");		
		fprintf(stderr,"Please input <./abe><sinuglar> or <./abe><ordinary>\n");
	}
	}
	//end of building the pairing funcion
	//construct a CP-ABE scheme
	//Pre-computation -> read the file of users
	float difftime= 0.0;
	int i,j,k = 0;//the index of the following loop
	clock_t tStart,tEnd;
	FILE *fTime = fopen("timeTate.txt","w+");
	int loopNum = 100;
	while(LOOP && loopNum){
	
	tStart = clock();
	MSP msp;//the monotone spanning program	
	mspSetup(&msp);
	int rows = msp.rows;
	FILE *fUser = fopen("user.file","r");//the pointer to read the user files
	int userNo = 0;//the number of users
	int* attrNo;//the number of attributes
	char **userName;
	char **attribute;
	fscanf(fUser,"%d\n",&userNo);
	attrNo = (int *)malloc(sizeof(int)*userNo);
	userName = (char **)malloc(sizeof(char *)*userNo);
	attribute = (char **)malloc(sizeof(char *)*userNo);
	for( i = 0 ; i < userNo ; i++){
		userName[i] = (char *)malloc(sizeof(char)*100);
		fscanf(fUser,"%s\n",userName[i]);
		fscanf(fUser,"%d\n",&attrNo[i]);
		attribute[i] = (char *)malloc(sizeof(char)*attrNo[i]);
		j = 0;//initialize the index of j
		k = attrNo[i];
		while(k != 0){
			fscanf(fUser,"%c\n",&attribute[i][j]);
			j++;
			k--;
		}
	}
	//1. Setup	
	setup(rows,&pairing,&msp);//the first step to set up the public key and master key
	//2. KeyGen
	for( i = 0; i<userNo;i++){
		keyGen(pairing,attrNo[i],attribute[i],userName[i]);//genereate the private key according to user's attribute
	}
	element_t message;//the plaintext message;
	element_init_GT(message,pairing);
	element_random(message);
	element_printf("M1 = %B\n",message);
	//3.Encrypt
	encrypt(message,pairing,&msp);
	//4.Decrypt
	decrypt(pairing,&msp,message,attrNo[1],attribute[1],userName[1]);	
	tEnd = clock();	
	//5.Time calculation presents
	difftime = (float)(tEnd-tStart)/CLOCKS_PER_SEC;
	printf("The cost time of tate pairing: %fs\n",difftime);
	fprintf(fTime,"%f\r\n",difftime);
	
	loopNum--;
	}//end of while-loop
	fclose(fTime);
	return 0;
}