Skip to content

Instantly share code, notes, and snippets.

@kyle-go
Last active December 14, 2015 00:59
Show Gist options
  • Save kyle-go/5002534 to your computer and use it in GitHub Desktop.
Save kyle-go/5002534 to your computer and use it in GitHub Desktop.
simple encrypt
/*!
simple encrypt
*/
#include <string>
#include <assert.h>
std::string encrypt(const std::string &src, bool encrypt)
{
assert(!src.empty());
std::string ret;
if (encrypt) {
for (size_t i = 0; i < src.length(); ++i) {
char tmp[4] = {0};
sprintf_s(tmp, "%.2x", (unsigned char)src[i] ^ 0xe9);
ret += tmp;
}
} else {
assert (src.length()%2 == 0);
for (size_t i = 0; i < src.length(); i+=2) {
char tmp[4] = {0};
tmp[0] = src[i];
tmp[1] = src[i+1];
assert ((tmp[0] <= '9' && tmp[0] >='0' || tmp[0] <= 'f' && tmp[0] >= 'a')
&& (tmp[1] <= '9' && tmp[1] >='0' || tmp[1] <= 'f' && tmp[1] >= 'a'));
unsigned int c;
sscanf_s(tmp, "%2X", &c);
ret += (char)((unsigned char)c ^ 0xe9);
}
}
return ret;
}
int main()
{
std::string src = "test abvd 1000__ --abc 你妹的。。。";
std::string dst = encrypt(src, true);
printf("src = %s\n", src.c_str());
printf("dst = %s\n", dst.c_str());
printf("src = %s\n", encrypt(dst, false).c_str());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment