Projekt je dostupný zadarmo na stiahnutie!
XTIME C Calculator Decimal Hexadecimal Multiplycation Bit

Funkcia xtime() v jazyku C

  • Funkcia xtime() je v AES algoritmoch násobenie 02 v desiatkovej sústave. Funkciu je možné použiť aj na hexadecimálny či binárny tvar vstupu
  • Program pracuje so štandardným vstupom používateľa, ktorý vie vstupy zadať a program mu vráti výsledok. Na základe použitia daného programu zadáva hexadecimálny, alebo decimálny tvar jednotlivých vstupov.

  • Preklad programu cez Makefile:

    gcc dec.c -o dec.exe
    gcc hex.c -o hex.exe
       

    xtime kalkulačka s decimálnou reprezentáciou

    /*|----------------------------------------------------------|*/
    /*|XTIME DECIMAL CALCULATOR IN C LANGUAGE                    |*/
    /*|AUTHOR: MARTIN CHLEBOVEC                                  |*/
    /*|----------------------------------------------------------|*/        
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    static unsigned long xtime(unsigned long x){
    	return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
    }
    static uint8_t Multiply(uint8_t x, uint8_t y){
      	return ((y & 1) * x) ^
           ((y>>1 & 1) * xtime(x)) ^
           ((y>>2 & 1) * xtime(xtime(x))) ^
           ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
           ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))^
           ((y>>5 & 1) * xtime(xtime(xtime(xtime(xtime(x)))))) ^
           ((y>>6 & 1) * xtime(xtime(xtime(xtime(xtime(xtime(x)))))))^
           ((y>>7 & 1) * xtime(xtime(xtime(xtime(xtime(xtime(xtime(x))))))));
    }
    
    int main(){
    	int operand1, operand2;
    	uint8_t x,y;
        char buffer[100];
      	int success = 0;
      	printf("Enter numbers from range 0 - 255: \n");
      	while(success != 1) {
    		printf("Enter number 1: \n");
        		scanf("%s", buffer);
        		success = sscanf(buffer, "%d", &operand1);
        		if(operand1 >= 0 && operand1 < 256) {
          			success = 1;
        		}else{
        			success = 0;
        		}
      	}
    	success = 0;
    	while(success != 1) {
             	printf("Enter number 2: \n");
        		scanf("%s", buffer);
        		success = sscanf(buffer, "%d", &operand2);
        		if(operand2 >= 0 && operand2 < 256) {
          			success = 1;
        		}else{
        			success = 0;
        		}
      	}
    	x = operand1;
    	y = operand2;
     	uint8_t result;
    	result = Multiply( x, y );
    	printf("Xtime result: %d\n", result); 
    	return 0;
    }
            

    xtime kalkulačka s hexadecimálnou reprezentáciou

    /*|----------------------------------------------------------|*/
    /*|XTIME HEXADECIMAL CALCULATOR IN C LANGUAGE                |*/
    /*|AUTHOR: MARTIN CHLEBOVEC                                  |*/
    /*|----------------------------------------------------------|*/         
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    static unsigned long xtime(unsigned long x){
    	return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
    }
    static uint8_t Multiply(uint8_t x, uint8_t y){
      	return ((y & 1) * x) ^
           ((y>>1 & 1) * xtime(x)) ^
           ((y>>2 & 1) * xtime(xtime(x))) ^
           ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
           ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))^
           ((y>>5 & 1) * xtime(xtime(xtime(xtime(xtime(x)))))) ^
           ((y>>6 & 1) * xtime(xtime(xtime(xtime(xtime(xtime(x)))))))^
           ((y>>7 & 1) * xtime(xtime(xtime(xtime(xtime(xtime(xtime(x))))))));
    }
    
    int main(){
    	unsigned int operand1, operand2;
    	uint8_t x,y;
          	char buffer[100];
      	int success = 0;
      	printf("Enter numbers from range 00-FF: \n");
      	while(success != 1) {
    		printf("Enter number 1: \n");
        		printf("0x");
    		scanf("%s", buffer);
        		success = sscanf(buffer, "%02x", &operand1);
        		if(operand1 >= 00 && operand1 < 256) {
          			success = 1;
        		}else{
        			success = 0;
        		}
      	}
    	success = 0;
    	while(success != 1) {
             	printf("Enter number 2: \n");
        		printf("0x");
    		scanf("%s", buffer);
        		success = sscanf(buffer, "%02x", &operand2);
        		if(operand2 >= 00 && operand2 < 256) {
          			success = 1;
        		}else{
        			success = 0;
        		}
      	}
    	x = operand1;
    	y = operand2;
     	uint8_t result;
    	result = Multiply( x, y );
            printf("Xtime result: 0x%02x\n", result);
    	return 0;
    }