Xor by two hexadeicmal values ​​stored as string in C ++

I have two hexadecimal values ​​that are stored as a string:

string md5OfPath("a77e0a3e517e0a9d44c3a823d96ca83c");
string guidValue("c3b491e559ac4f6a81e527e971b903ed");

I want to perform an XOR operation for both values ​​in C ++. Can you suggest me how I can perform the xor operation.

+5
source share
6 answers

I think straight loop should do the trick:

static inline unsigned int value(char c)
{
    if (c >= '0' && c <= '9') { return c - '0';      }
    if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
    if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
    return -1;
}

std::string str_xor(std::string const & s1, std::string const & s2)
{
    assert(s1.length() == s2.length());

    static char const alphabet[] = "0123456789abcdef";

    std::string result;
    result.reserve(s1.length());

    for (std::size_t i = 0; i != s1.length(); ++i)
    { 
        unsigned int v = value(s1[i]) ^ value(s2[i]);

        assert(v < sizeof alphabet);

        result.push_back(alphabet[v]);
    }

    return result;
}
+5
source

Iterate over the characters of the strings in parallel and perform the operation XORcharacter by character, adding characters to the result string as you go through them.

Oh operator XOR ^.

You may have a map for characters in hexadecimal values:

`0` -> 0x0
`1` -> 0x1
...
`a` -> 0xa

And apply the operator ^to the values, not to the characters themselves.

+2
source

ints . XOR , , ints, .

4 ; 8 , 32- int (.. ).

0

++ std::transform. , , , xor:

#include <functional>

template <typename T>
struct Xor : std::binary_function<T, T, T> {
    T operator() (T a, T b) {
        return a ^ b;
    }
};

, xors :

#include <cassert>
#include <functional>
#include <iterator>
#include <string>

static std::string xorStrings( const std::string &s1,
                               const std::string &s2 )
{
    std::assert( s1.size() == s2.size() );

    std::string result;
    result.reserve( s1.size() );

    std::transform( s1.begin(), s1.end(),
                    s2.begin(),
                    std::back_inserter( result ),
                    Xor<std::string::value_type>() );

    return result;
}
0

You can use the following function (xorTwoHexStrings (string str1, string str2))

#include<sstream>

int hexCharToInt(char a){
    if(a>='0' && a<='9')
        return(a-48);
    else if(a>='A' && a<='Z')
        return(a-55);
    else
        return(a-87);
}

string xorTwoHexStrings(string str1, string str2){
    std::stringstream XORString;
    for(int i=0;i<str2.length();i++){
        XORString << hex << (hexCharToInt(str1[i])^hexCharToInt(str2[i]));
    }
    return XORString.str();
}
0
source
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

const char* quads[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };

const char * chrTObin(unsigned char c) {
  if(c >= '0' && c <= '9') return quads[     c - '0'];
  if(c >= 'A' && c <= 'F') return quads[10 + c - 'A'];
  if(c >= 'a' && c <= 'f') return quads[10 + c - 'a'];
  return NULL;
  //return -1;
}

char* xorHash(char* chrXOR1, char* chrXOR2) {

    int x;
    int xPos;

    int intXOR1 = strlen(chrXOR1);


    char strBin1[4];
    char strBin2[4];
    char strBin[8];
    char strXORED[4];
    char newXOR[128];

    strcpy(newXOR, "");

    for(x = 0; x < intXOR1; x++) {

        strcpy(strBin, "");
        strcat(strBin, chrTObin(chrXOR1[x]));
        strcat(strBin, chrTObin(chrXOR2[x]));

        strcpy(strXORED, "");

        if(strlen(strBin) == 8) {
            for(xPos = 0; xPos < 4; xPos++) {
                if(strBin[xPos] == strBin[xPos+4]) {
                    strcat(strXORED, "0");
                } else {
                    strcat(strXORED, "1");
                }
            }
        }

        if(strcmp(strXORED, "0000") == 0) {
            strcat(newXOR, "0");
        } else if(strcmp(strXORED, "0001") == 0) {
            strcat(newXOR, "1");
        } else if(strcmp(strXORED, "0010") == 0) {
            strcat(newXOR, "2");
        } else if(strcmp(strXORED, "0011") == 0) {
            strcat(newXOR, "3");
        } else if(strcmp(strXORED, "0100") == 0) {
            strcat(newXOR, "4");
        } else if(strcmp(strXORED, "0101") == 0) {
            strcat(newXOR, "5");
        } else if(strcmp(strXORED, "0110") == 0) {
            strcat(newXOR, "6");
        } else if(strcmp(strXORED, "0111") == 0) {
            strcat(newXOR, "7");
        } else if(strcmp(strXORED, "1000") == 0) {
            strcat(newXOR, "8");
        } else if(strcmp(strXORED, "1001") == 0) {
            strcat(newXOR, "9");
        } else if(strcmp(strXORED, "1010") == 0) {
            strcat(newXOR, "A");
        } else if(strcmp(strXORED, "1011") == 0) {
            strcat(newXOR, "B");
        } else if(strcmp(strXORED, "1100") == 0) {
            strcat(newXOR, "C");
        } else if(strcmp(strXORED, "1101") == 0) {
            strcat(newXOR, "D");
        } else if(strcmp(strXORED, "1110") == 0) {
            strcat(newXOR, "E");
        } else if(strcmp(strXORED, "1111") == 0) {
            strcat(newXOR, "F");
        }

    }

    return newXOR;

}

int main(int argc, char* argv[]) {

    if(argc != 3){
        printf("Usage:\n");
        printf("xor HEX1 HEX2\n");
        return 0;
    }

    if(strlen(argv[1]) == strlen(argv[2])) {

        char oneXOR[128];
        char twoXOR[128];
        char newXOR[128];

        strcpy(oneXOR, argv[1]);
        strcpy(twoXOR, argv[2]);
        strcpy(newXOR, "");

        printf("XOR: %s %s\n", oneXOR, twoXOR);
        strcpy(newXOR, xorHash(oneXOR, twoXOR));
        printf("RESULT: %s\n", newXOR);

    }

    return 0;
}
0
source

All Articles