【问题标题】:How encrypt a string with XOR without include strange characters?如何使用 XOR 加密字符串而不包含奇怪的字符?
【发布时间】:2020-03-14 19:26:29
【问题描述】:

以下代码可以正常工作,但我不希望在加密字符串中包含奇怪的字符,例如 '\x03'。如何做到这一点?

string XOR_Encryption(string toBeEncrypted, string sKey)
{
    string sEncrypted(toBeEncrypted);
    unsigned int iKey(sKey.length()), iIn(toBeEncrypted.length()), x(0);
    for (unsigned int i = 0; i < iIn; i++)
    {
        sEncrypted[i] = toBeEncrypted[i] ^ sKey[x];
        if (++x == iKey) { x = 0; }
    }
    return sEncrypted;
}

用法:

    string message = "gbpi";
    string Key("Q4s4R4t");

    string encrypted_message = XOR_Encryption(message, Key);
    cout << "encoded: " << encrypted_message << endl;

    string decryptedMessage = XOR_Encryption(encrypted_message, Key);
    cout << "decoded: " << decryptedMessage << endl;

【问题讨论】:

  • 对输出的十六进制或base64进行编码。请注意,您必须在解密之前进行解码。
  • @kelalaka,谢谢。工作。
  • 请写一个包含完整代码和一些解释的答案。我会投票...
  • 作为替代方案,您可以尝试使用Vigenere 加密,它总是产生字母字符(或带有简单扩展名的字母数字)。优点是密文与明文长度相同,不像 Hex 或 Base64。
  • @kelalaka, "Please write an answer that should contain full code and with some explanations. I'll upvote..." - 完成。

标签: c++ visual-studio encryption cryptography xor


【解决方案1】:

参考@kelalakasuggestion,这里有一个使用Hex编码的解决方案:

#include "stdafx.h"

#include <stdio.h>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include <iostream>

string XOR_Encryption(string toBeEncrypted, string sKey)
{
    string sEncrypted(toBeEncrypted);
    unsigned int iKey(sKey.length()), iIn(toBeEncrypted.length()), x(0);
    for (unsigned int i = 0; i < iIn; i++)
    {
        sEncrypted[i] = toBeEncrypted[i] ^ sKey[x];
        if (++x == iKey) { x = 0; }
    }
    return sEncrypted;
}

void stream2hex(const string str, string& hexstr, bool capital = false)
{
    hexstr.resize(str.size() * 2);
    const size_t a = capital ? 'A' - 1 : 'a' - 1;

    for (size_t i = 0, c = str[0] & 0xFF; i < hexstr.size(); c = str[i / 2] & 0xFF)
    {
        hexstr[i++] = c > 0x9F ? (c / 16 - 9) | a : c / 16 | '0';
        hexstr[i++] = (c & 0xF) > 9 ? (c % 16 - 9) | a : c % 16 | '0';
    }
}

void hex2stream(const string hexstr, string& str)
{
    str.resize((hexstr.size() + 1) / 2);

    for (size_t i = 0, j = 0; i < str.size(); i++, j++)
    {
        str[i] = (hexstr[j] & '@' ? hexstr[j] + 9 : hexstr[j]) << 4, j++;
        str[i] |= (hexstr[j] & '@' ? hexstr[j] + 9 : hexstr[j]) & 0xF;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    string text = "text";
    string Key("password");

    string encrypted_message = XOR_Encryption(text, Key);
    stream2hex(encrypted_message, encrypted_message, true);
    cout << "encoded: " << encrypted_message << endl;

    hex2stream(encrypted_message, encrypted_message);
    string decryptedMessage = XOR_Encryption(encrypted_message, Key);
    cout << "decoded: " << decryptedMessage << endl;

   getch();

   return 0;
}

【讨论】:

  • 我做到了。记得至少为函数写一些解释吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多