Hexadecimal numbers in C

My task is to implement the SEAL2. 0 algorithm.

In short, the input from the file is a 160-character word, it is divided into 5 hexadecimal numbers of 32 characters each. Then there are various manipulations with these hexadecimal numbers: cyclic shift, bitwise logical multiplication, bitwise logical addition, etc.

I know that C supports bitwise operations, hexadecimal numbers, and can work with them. That is, you can do this:

char a,b,c;
a=0x2f;
b=0x1c;
c=a&b;// поразрядное "И"

Question, is it possible to somehow use the built-in bitwise operations for large hexadecimal numbers of 32 characters? In what format, then, should we store such large numbers so that we can work with them?

Author: Timofei Bondarev, 2015-01-10

2 answers

If we are talking about the SEAL stream cryptographic algorithm, then the TC means 32-bit numbers

Obviously, the TS does not understand that from the point of view of the processor, a 16-decimal or 10-decimal number is just a representation of for the convenience of a person and nothing more. The processor operates with bits anyway.

Accordingly, depending on the compiler/architecture used, you need to choose a method for storing 32-bit numbers. Approximately so:

Windows(IA-32): unsigned long int

Windows (Intel 64): unsigned long int

Windows (IA-64): unsigned long int

Linux (IA-32): unsigned long int

Linux (Intel 64): unsigned short int

Linux (IA-64): unsigned short int

Mac OS X (IA-32): unsigned long int

Mac OS X (Intel 64): unsigned short int

Then you can safely perform bit manipulations with the necessary types.

P.S. Never implement cryptographic primitives yourself - never! However, the vehicle is apparently not a decree :) - maybe this is his coursework or thesis?

 3
Author: Barmaley, 2015-01-12 13:51:06

Don't hit me right away I probably didn't understand the question well, but I'll still post my code

#include <iostream>
#include <bitset>
#include <string>

using namespace std;

int main(){
    // эмуляция файла
    string file("dsfdskfjksdjfjsdfkldjsfjsdfjsdfdidfsdhfjsdjfdsfdshfdhfgggfdgfdgdfgdfgdfgdfgdfrterghjghjhjhjghjhgjghjghfbvgrfhythgfhgfhgfhgfhtrhytrhgfhgfhhythjgngnjghjghjhgjdff");
    bitset<32> b1, b2, b3, b4, b5, result;

    for (size_t i = 0; i <= file.size() - 5; i += 5){
        b1 = file[i];
        b2 = file[i + 1];
        b3 = file[i + 2];
        b4 = file[i + 3];
        b5 = file[i + 4];

        cout << "1: " << b1 << "\thex: " << hex << b1.to_ulong() << dec << "\t\tdec: " << dec << b1.to_ulong() << endl;
        cout << "2: " << b2 << "\thex: " << hex << b2.to_ulong() << dec << "\t\tdec: " << dec << b2.to_ulong() << endl;
        cout << "3: " << b3 << "\thex: " << hex << b3.to_ulong() << dec << "\t\tdec: " << dec << b3.to_ulong() << endl;
        cout << "4: " << b4 << "\thex: " << hex << b4.to_ulong() << dec << "\t\tdec: " << dec << b4.to_ulong() << endl;
        cout << "5: " << b5 << "\thex: " << hex << b5.to_ulong() << dec << "\t\tdec: " << dec << b5.to_ulong() << endl;
        cout << "----" << endl;

        result = b1 | b2 | b3 | b4 | b5;
        cout << "example1 ----> 1 | 2 | 3 | 4 | 5 = " << result << endl;
        result = b1 & b2 & b3 & b4 & b5;
        cout << "example2 ----> 1 & 2 & 3 & 4 & 5 = " << result << endl;

        cout << "-------------------------------------------------------------------" << endl;

    }

    return 0;
}
 1
Author: perfect, 2015-01-12 14:08:02