util.cpp

来自「tea 加密算法 qq使用的就是这个加密算法来 解决登陆问题」· C++ 代码 · 共 59 行

CPP
59
字号
#include "util.h"
#include <vector>

using namespace std;

char intToHexChar(int x) {
	static const char HEX[16] = {
		'0', '1', '2', '3',
		'4', '5', '6', '7',
		'8', '9', 'A', 'B',
        'C', 'D', 'E', 'F'
    };
    return HEX[x];
}

int hexCharToInt(char hex) {
    hex = toupper(hex);
    if (isdigit(hex))
        return (hex - '0');
    if (isalpha(hex))
        return (hex - 'A' + 10);
    return 0;
}

string bytesToHexString(const byte *in, size_t size) {
	string str;
    for (size_t i = 0; i < size; i++) {
        int t = in[i];
        int a = t / 16;
        int b = t % 16;
        str.append(1, intToHexChar(a));
        str.append(1, intToHexChar(b));
        if (i != size - 1)
			str.append(1, ' ');
	}
    return str;
}

size_t hexStringToBytes(const string &str, byte *out) {
	vector<string> vec;
    string::size_type currPos = 0, prevPos = 0;
    while ((currPos = str.find(' ', prevPos)) != string::npos) {
        string b(str.substr(prevPos, currPos - prevPos));
        vec.push_back(b);
        prevPos = currPos + 1;
    }
    if (prevPos < str.size()) {
        string b(str.substr(prevPos));
        vec.push_back(b);
    }
    typedef vector<string>::size_type sz_type;
    sz_type size = vec.size();
	for (sz_type i = 0; i < size; i++) {
		int a = hexCharToInt(vec[i][0]);
		int b = hexCharToInt(vec[i][1]);
		out[i] = a * 16 + b;
	}
	return size;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?