Exemplo n.º 1
0
int G2::spill(char *& bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*2*bytes_per_big;
	Big x,y;

	if (mtable==NULL) return 0;

	bytes=new char[len];

	for (i=j=0;i<n;i++)
	{
		mtable[i].get(x,y);		
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		x=from_binary(bytes_per_big,&bytes[j]);

		j+=bytes_per_big;
		to_binary(y,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}
	delete [] mtable; 
	mtable=NULL;
	return len;
}
Exemplo n.º 2
0
int PFC::spill(G2& w,char *& bytes)
{
	int i,j,len,m;
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	
	Big a,b,n;
	Big X=*x;
	if (w.ptable==NULL) return 0;

	if (X<0) n=-(6*X+2);
    else n=6*X+2;

	m=2*(bits(n)+ham(n));
	len=m*2*bytes_per_big;

	bytes=new char[len];
	for (i=j=0;i<m;i++)
	{
		w.ptable[i].get(a,b);
		to_binary(a,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary(b,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;

	}

	delete [] w.ptable; 
	w.ptable=NULL;
	return len;
}
Exemplo n.º 3
0
int PFC::spill(G2& w,char *& bytes)
{
	int i,j,len,m;
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	
	ZZn a,b,c;
	Big X=*x;
	if (w.ptable==NULL) return 0;

	m=2*(bits(X)-2+ham(X));
	len=m*3*bytes_per_big;

	bytes=new char[len];
	for (i=j=0;i<m;i++)
	{
		w.ptable[i].get(a,b,c);
		to_binary((Big)a,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)b,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)c,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}

	delete [] w.ptable; 
	w.ptable=NULL;
	return len;
}
Exemplo n.º 4
0
int main()
{
	string_t * strs;
	string_t sum;
	int j;
	
	make_zero_str(&sum, 0, STR_LENS);
	to_binary(&sum);
	
	strs = buffer_to_strs(strs_buffer, NUM_STRS, STR_LENS);
	for(j = 0; j < NUM_STRS; j++)
	{
		to_binary(&strs[j]);
	}
	
	for(j = 0; j < NUM_STRS; j++)
	{
		add_bin_strs(&sum, &strs[j]);
	}
	
	to_ascii(&sum);
	fwrite(sum.str, 1, sum.size, stdout);
	printf("\n");
	printf("First 10 digits of the sum: ");
	fwrite(sum.str, 1, 10, stdout);
	printf("\n");
	
	free(sum.str);
	for(j = 0; j < NUM_STRS; j++)
	{
		free(strs[j].str);
	}
	free(strs);
	return 0;
}
Exemplo n.º 5
0
//check if a date is valid
static
int 
is_valid(struct time* date){
	uchar month  = to_binary(date->month);
	uchar day_of_month  = to_binary(date->day_of_month);
	uchar hours  = to_binary(date->hours);
	uchar minutes  = to_binary(date->minutes);
	uchar year = to_binary(date->year);

	if(month<0 || month>12)
		return 0;
	if(day_of_month < 0)
		return 0;
	if(hours<0 || hours>24)
		return 0;
	if(minutes<0 || minutes>60)
		return 0;
	switch(month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
				if(day_of_month>31)
					return 0;
				break;
		case 4:
		case 6:
		case 9:
		case 11:
				if(day_of_month>30)
					return 0;
				break;
		case 2:
				{
					int days_on_f=28;
					
					if ((year%4 == 0 && year % 100 != 0) || year % 400 == 0)
						days_on_f=29;
					if(day_of_month > days_on_f)
						return 0;
					break;
				}
	}
	return 1;
}
Exemplo n.º 6
0
/*
Main method for program date. use it like: 
	_ "date" for consult current date
	_ "date MMDDhhmm for set month(MM), day(DD), hh (hours) and minute(mm)"
*/
int 
main(int argc,char *argv[])
{
	int fd;
	struct time time;

	if(argc == 1){
		fd=open("rtc",O_RDWR);
		read(fd,&time,1);
		close(fd);
		printf(1,"%s %s %d %d:%d:%d\n",get_day(time.day_of_week), get_month(time.month), to_binary(time.day_of_month),to_binary(time.hours),to_binary(time.minutes),to_binary(time.seconds));
	}else{ 
			if(argc == 2){
				fd=open("rtc",O_RDWR);
				read(fd,&time,1);																
				if (parse_time(argv[1],&time) == 0){												
					if(is_valid(&time))
						write(fd,&time,1);
					else
						printf(1,"The date isn't valid.\n");
					close(fd);						
				}else					
					printf(1,err_msg);
			}else
				printf(1,err_msg);				
	}
	exit();
}
Exemplo n.º 7
0
//sha3salt needs to be figured out CUDA_DEBUG
static void *salt(char *ciphertext)
{
#ifdef CUDA_DEBUG
	printf("salt(%s)\n", ciphertext);
#endif
	static crypt_sha3_salt ret;
	uint8_t i, *pos = (uint8_t *) ciphertext, *end;
	char *p,*dest = ret.salt;
	if (strncmp(ciphertext, sha3_salt_prefix, strlen(sha3_salt_prefix)) == 0) {
		pos += strlen(sha3_salt_prefix);
		ret.prefix = '1';
	}
	//XXX other salt prefix needs to be null?/removed?
	/*if (strncmp(ciphertext, apr1_salt_prefix,
		strlen(apr1_salt_prefix)) == 0) {
		pos += strlen(apr1_salt_prefix);
		ret.prefix = 'a';
	}*/
	end = pos;
	for (i = 0; i < 8 && *end != '$'; i++, end++);
	while (pos != end)
		*dest++ = *pos++;
	ret.length = i;
	p = strrchr(ciphertext, '$') + 1;
	to_binary(p,(char*) ret.hash);
#ifdef CUDA_DEBUG
	puts("salted:");
	uint32_t *t=ret.hash;
	for(i=0;i<4;i++)
	  printf("%08x ",t[i]);
	puts("");
#endif
	return (void *) &ret;
}
Exemplo n.º 8
0
Arquivo: tenToTwo.cpp Projeto: wjzmm/c
void to_binary(unsigned long n){
	if(n > 1){
		to_binary(n / 2);
	}
	printf("%d", n % 2);

	return ;
}
Exemplo n.º 9
0
static void *binary(char *ciphertext)
{
	static char b[BINARY_SIZE];
	memset(b, 0, BINARY_SIZE);
	char *p = strrchr(ciphertext, '$') + 1;
	to_binary(p, b);
	return (void *) b;
}
Exemplo n.º 10
0
string AES::hexstr_to_binstr (string cad)
{
    //cada caracter lo tngo q pasar a binario
    stringstream aux;
    for (unsigned i=0; i<(MAX*2);i++)
       aux<<to_binary(to_dec(cad[i]),4).get_s();
    return aux.str();
}
Exemplo n.º 11
0
void to_binary (unsigned long n)	/* recursion function */
{
	int r;
	r = n % 2;
	if (n >= 2)
		to_binary(n / 2);
	putchar ('0' + r);
	return;
}
Exemplo n.º 12
0
string bigToRawBytes(Big x)
{
	char c[MAX_LEN+1];
	memset(c, 0, MAX_LEN);
	int size = to_binary(x, MAX_LEN, c, FALSE);
	string bytes(c, size);
	stringstream ss;
	ss << bytes << "\0";
	return ss.str();
}
Exemplo n.º 13
0
int main(){
	int sum, i;
	
	for(sum = 0, i=0; i < LIMIT ; i++){
		if(isPalindromic(int_to_vector(i)) && isPalindromic(to_binary(i)) )
			sum += i;
	}
	std::cout << "Answer: " << sum << std::endl;
	return 0;
}
Exemplo n.º 14
0
/*递归函数*/ 
void to_binary(unsigned long n){
	int r;
	r = n % 2;
	if(n >= 2){
		printf("n = %d r = %d\n",n,r);
		to_binary(n/2);
	}
	putchar('0'+r);
	
	return;
}
Exemplo n.º 15
0
void to_binary(unsigned long n)
{
    int r;
    
    r = n % 2;
    if (n > 2)
	to_binary(n / 2);
    putchar(r ? '1' : '0');
    
    return;
}
Exemplo n.º 16
0
int GT::spill(char *& bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*6*bytes_per_big;
	ZZn2 a,b,c;
	Big x,y;

	if (etable==NULL) return 0;

	bytes=new char[len];
	for (i=j=0;i<n;i++)
	{
		etable[i].get(a,b,c);
		a.get(x,y);
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary(y,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		b.get(x,y);
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary(y,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		c.get(x,y);
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary(y,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}
	delete [] etable; 
	etable=NULL;
	return len;
}
Exemplo n.º 17
0
int G2::spill(char *& bytes)
{
	int i,j,n=(1<<WINDOW_SIZE);
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*6*bytes_per_big;
	ZZn3 x,y;
	ZZn a,b,c;

	if (mtable==NULL) return 0;

	bytes=new char[len];
	for (i=j=0;i<n;i++)
	{
		mtable[i].get(x,y);
		x.get(a,b,c);
		to_binary((Big)a,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)b,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)c,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		y.get(a,b,c);
		to_binary((Big)a,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)b,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
		to_binary((Big)c,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}
	delete [] mtable; 
	mtable=NULL;
	return len;
}
Exemplo n.º 18
0
int main()
{
    int number;
    printf("pls input the number you wanna transform:\n");
    scanf("%d", &number);
    printf("the number after transform:\n");
    printf("%d", to_binary(number));
//    用这个会多个1出来,原因不明;
//    to_binary(number);
    printf("\n");

    return 0;
}
Exemplo n.º 19
0
int main(void){
	unsigned long number;
	printf("Enter an integer (q or quit):\n");
	while(scanf("%ul",&number) == 1){
		printf("Binary equivalent\n: ");
		to_binary(number);
		putchar('\n');
		printf("Enter an integer (q or quit): \n");
	}
	
	printf("Done.\n");
	
	return 0;
}
Exemplo n.º 20
0
int to_binary(int a)
{
    int r;
    r = a % 2;
//    printf("\n%d\n", r);
    if (a >= 2)
    {
        to_binary(a/2);
//        printf("ssss" );
    }
//    return r;
//    printf("%d", r);
//    putchar(r+'0');
}
Exemplo n.º 21
0
// serialize helper methods for going from Big to bytes and back
string bigToBytes(Big x)
{
	int len = MAX_LEN;
	char c[len+1];
	memset(c, 0, len);
	int size = to_binary(x, len, c, FALSE);
	string bytes(c, size);
//	printf("bigToBytes before => ");
//	_printf_buffer_as_hex((uint8_t *) bytes.c_str(), size);
	stringstream ss;
	ss << size << ":" << bytes << "\0";
//	printf("bigToBytes after => ");
//	_printf_buffer_as_hex((uint8_t *) ss.str().c_str(), ss.str().size());
	return ss.str();
}
Exemplo n.º 22
0
int main(int argc, char *argv[])
{
    unsigned long number;
    
    printf("Enter an integer (q to quit): \n");
    while (scanf("%lu", &number) == 1) {
	printf("Binary equivalent: ");
	to_binary(number);
	putchar('\n');
	printf("Enter an integer (q to quit): \n");
    }
    printf("Done.\n");
    
    return 0;
}
string AdaptiveHuffman::get_code(char sym)
{
	tempcode = "";

	get_code_by_tree(root, string(1, sym), "");

	string result = tempcode;
	
	if (tempcode == "")
	{
		get_code_by_tree(root, "NYT", "");
		result = tempcode;
		result += to_binary(sym);
	}

	return result;
}
Exemplo n.º 24
0
Arquivo: t18.c Projeto: ssenk/shinsoft
int main(int argc, char *argv[])
{
    char  c = -1;
    short s = -1;
    int   i = -1;
    long  l = -1;
    float f = -1.0f;
    double       d = -1.0;
    long double  q = -1.0;

    double md = 1.0e16;
    double md2;
    double d201 = 2.01;

    for(double db = 0; db < 100; db++)
    {
	double d = db / 100 * 100;
	    printf("%.16f = %.16f %.16f\n", db, d, fabs(db-d));
    }

    if (md == md+1)
    {
	md2 = md+1;
        printf("%.16f(1.0e16)가 %.16f + 1와 같다\n", md, md);
	dump_binary(&md,  sizeof(md));
	dump_binary(&md2, sizeof(md2));
    }

    printf("d201 = %.2f => (int)(d201*100) = %d\n", d201, (int)(d201 * 100));

    dump_binary(&c, sizeof(c));
    dump_binary(&s, sizeof(s));
    dump_binary(&i, sizeof(i));
    dump_binary(&l, sizeof(l));
    dump_binary(&f, sizeof(f));
    dump_binary(&d, sizeof(d));
    dump_binary(&q, sizeof(q));

    printf("%2d ==> [%s] \n", sizeof(c), to_binary(&c, sizeof(c)));
    printf("%2d ==> [%s] \n", sizeof(s), to_binary(&s, sizeof(s)));
    printf("%2d ==> [%s] \n", sizeof(i), to_binary(&i, sizeof(i)));
    printf("%2d ==> [%s] \n", sizeof(l), to_binary(&l, sizeof(l)));
    printf("%2d ==> [%s] \n", sizeof(f), to_binary(&f, sizeof(f)));
    printf("%2d ==> [%s] \n", sizeof(d), to_binary(&d, sizeof(d)));
    printf("%2d ==> [%s] \n", sizeof(q), to_binary(&q, sizeof(q)));

    printfloat(f);
}
Exemplo n.º 25
0
Arquivo: Time.cpp Projeto: epatel/Cosa
time_t::operator clock_t()
{
  clock_t res;
  to_binary();
  res = year * (SECONDS_PER_DAY * 365);
  for (uint8_t y = 0; y < year; y++) 
    if (is_leap(y)) 
      res +=  SECONDS_PER_DAY;
  for (uint8_t m = 1; m < month; m++) {
    uint8_t days = pgm_read_byte(&days_in[m]);
    if (is_leap(year) && (m == 2)) days += 1;
    res += SECONDS_PER_DAY * days;
  }
  res += (date - 1) * SECONDS_PER_DAY;
  res += hours * SECONDS_PER_HOUR;
  res += minutes * SECONDS_PER_MINUTE;
  res += seconds;
  return (res);
}
Exemplo n.º 26
0
int PFC::spill(G1& w,char *& bytes)
{
	int i,j,n=2*(bits(*ord-1)-2+ham(*ord));
	int bytes_per_big=(MIRACL/8)*(get_mip()->nib-1);
	int len=n*bytes_per_big;
	Big x;
	if (w.ptable==NULL) return 0;

	bytes=new char[len];
	for (i=j=0;i<n;i++)
	{
		x=w.ptable[i];
		to_binary(x,bytes_per_big,&bytes[j],TRUE);
		j+=bytes_per_big;
	}

	delete [] w.ptable; 
	w.ptable=NULL;
	return len;
}
Exemplo n.º 27
0
int main()
try {
    cout << "Enter input file name: ";
    string iname;
    cin >> iname;
    to_binary(iname);

    cout << "Enter output file name: ";
    string oname;
    cin >> oname;
    from_binary(oname);

    cout << "Done!\n";
}
catch (exception& e) {
    cerr << "exception: " << e.what() << endl;
}
catch (...) {
    cerr << "exception\n";
}
Exemplo n.º 28
0
//Obtain a month name from a day number in decimal version
static
char* 
get_month(uchar month)
{
	month= to_binary(month);
	switch(month){
		case 1: return "January";
		case 2: return "February";
		case 3: return "March";
		case 4: return "April";
		case 5: return "May";
		case 6: return "June";
		case 7: return "July";
		case 8: return "Agoust";
		case 9: return "September";
		case 10: return "October";
		case 11: return "November";
		case 12: return "December";
		default: return "Error";
	}
}
void main()
{
    int a,b,c,d,w,n,o ;
    int x[32], y[32], z[32] ;
    
    o = 0 ;
    w = 9 ; /* word size in bits */
    /* note that x, y, and z must be at least as large as w and may be larger */
    
    b = 1 ; /* add one to each */
    for( c=250;c<260;c++ )
    {
        if ( c > 255 )  /* exceeds 8 bits */
            d = c-512 ;
        else
            d =  c ;
        printf( "%d %d ", d,b ) ;
        to_binary( d,w,x,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        to_binary( b,w,y,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",b ) ;
        adder( x,y,z,&o,w ) ;
        from_binary( z,w,&a ) ;
        printf( "%d ", a ) ;
        if ( o )
            printf( "Overflow in adder! " ) ;
        printf( "\n" ) ;
    }
    
    b = -1 ; /* add -1 to each */
    for( c=250;c<260;c++ )
    {
        if ( c > 255 )
            d = c-512 ;
        else
            d =  c ;
        printf( "%d %d ", d,b ) ;
        to_binary( d,w,x,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        to_binary( b,w,y,&o ) ;
        if ( o )
            printf( "%d Overflow in to_binary! ",d ) ;
        adder( x,y,z,&o,w ) ;
        from_binary( z,w,&a ) ;
        printf( "%d ", a ) ;
        if ( o )
            printf( "Overflow in adder! " ) ;
        printf( "\n" ) ;
    }
    
    
    for( w=8; w<13; w=w+4 )          /* word sizes 8 and 12 bits */
        for( a=99; a >-100; a=a-99 )  /* 99, 0, -99 */
            for( b=4; b>-5; b=b-4 )    /* 4, 0, -4 */
            {
                to_binary( a,w,x,&o ) ;
                if ( o )
                    printf( "%d Overflow in to_binary! ",a ) ;
                to_binary( b,w,y,&o ) ;
                if ( o )
                    printf( "%d Overflow in to_binary! ",b ) ;
                mult( x,y,z,&o,w ) ;
                printf( "%d %d ", a,b ) ;
                from_binary( z,w,&n ) ;
                printf( "%d ", n ) ;
                if ( o )
                    printf( "Overflow in multiplier! " ) ;
                printf( "\n" ) ;
            }
}
Exemplo n.º 30
0
PlaintextMessage EncryptedMessage::decrypt(const G2& P, const G2& Ppub, G1 D, PFC *pfc) {
    G2 uCalc;
    G2 U = (*autData).getU();
    Big ud_hash = (*pfc).hash_to_aes_key((*pfc).pairing(U,D));
    Big ses_key;
    Big r;
    int nbOfRecipients = (*autData).getNbOfRecipients();

    Big W;
    Big V = (*autData).getV();
    Big rho, rho_hash;
    vector <Big> ws = (*autData).getEncryptedRecipientKeys();
    char P_text[Clen];
    bool integrity = false;

    time_t begin_time = clock();

    int i = 0;
    while(U != uCalc && i < nbOfRecipients){
        // rho = V XOR Hash(e(D,U))
        W=ws.at(i);
        rho = lxor(W, ud_hash);

        // M = W XOR Hash(rho)
        (*pfc).start_hash();
        (*pfc).add_to_hash(rho);
        rho_hash = (*pfc).finish_hash_to_group();
        ses_key = lxor(V, rho_hash);

        // r = Hash(rho,M)
        (*pfc).start_hash();
        (*pfc).add_to_hash(rho);
        (*pfc).add_to_hash(ses_key);
        r = (*pfc).finish_hash_to_group();
        uCalc = (*pfc).mult(P,r);
        i++;
    }
    cout << "ses_key is " << endl << ses_key << endl;

    /*************************************************
    *       AES GCM part of the decryption step      *
    **************************************************/
    to_binary(ses_key, HASH_LEN, sessionKey, TRUE);
    char k1[HASH_LEN/2];
    char iv[HASH_LEN/2];
    char Tdec[TAG_LEN];
    memset(P_text, 0, Clen+1);

    getIV(iv);
    getK1(k1);

    int Alen = (*autData).getLength();
    char A[Alen];
    (*autData).encodeTo(A);
    gcm g;
    gcm_init(&g, HASH_LEN/2, k1, HASH_LEN/2, iv);
    gcm_add_header(&g, A, Alen);
    gcm_add_cipher(&g, GCM_DECRYPTING, P_text, Clen, C);
    gcm_finish(&g, Tdec);

    integrity = true;
    for (int j = 0; j < TAG_LEN; j++) {
        if(Tdec[j] != T[j]) {
            integrity = false;
        }
    }

    if(integrity == false) {
        cout << "Received tag T does not correspond to decrypted T. There are some integrity issues here." << endl;
    } else {
        cout << "Successful integrity check!" << endl;
    }

    message = (string)P_text;

    return PlaintextMessage(message);
}