如何在c++;? 我用C++学习密码学,所以我用非常简单的XOR开始,我想打开文件0x1.txt,用3个键的XOR加密它,并创建一个新文件名为0x2.txt,并把加密的数据放入其中,并解密它,并将其内容放在0x3.txt:

如何在c++;? 我用C++学习密码学,所以我用非常简单的XOR开始,我想打开文件0x1.txt,用3个键的XOR加密它,并创建一个新文件名为0x2.txt,并把加密的数据放入其中,并解密它,并将其内容放在0x3.txt:,c++,encryption,xor,C++,Encryption,Xor,encrpyt->0x1.txt-->将加密数据放入0x2.txt;解密0x2.txt--> 将取消键入的数据放入0x3.txt 这是我的代码: 加密代码: LPVOID Crypt(HANDLE hFile, DWORD dwFileSize) { // allocate buffer for file contents LPVOID lpFileBytes = malloc(dwFileSize); // read the file into the buffer

encrpyt->0x1.txt-->将加密数据放入0x2.txt;解密0x2.txt--> 将取消键入的数据放入0x3.txt

这是我的代码:

加密代码:

LPVOID Crypt(HANDLE hFile, DWORD dwFileSize) {
    // allocate buffer for file contents
    LPVOID lpFileBytes = malloc(dwFileSize);
    // read the file into the buffer
    ReadFile(hFile, lpFileBytes, dwFileSize, NULL, NULL);

    // apply XOR encryption
    int i;
    char key[3] = {'*', '~', '#'};
    for (i = 0; i < dwFileSize; i++) {
        *((LPBYTE)lpFileBytes + i) ^= key[i % sizeof(key)];
    }

    return lpFileBytes;
}
然后将加密的数据放入0x2.txt:

HANDLE hCryptedFile = CreateFile("0x2.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

        // write to crypted file
        WriteFile(hCryptedFile, lpFileBytes, dwFileSize, NULL, NULL);
现在我想解密我制作的0x2.txt文件的内容:

HANDLE hFile = CreateFile("0x2.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        // get file size
        DWORD dwFileSize = GetFileSize(hFile, NULL);

        // decrypt and obtain decrypted bytes
        LPVOID lpFileBytes = Crypt(hFile, dwFileSize);
        CloseHandle(hFile);
创建文件0x3.txt:

HANDLE hTempFile = CreateFile("0x3.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        // write to temporary file
        WriteFile(hTempFile, lpFileBytes, dwFileSize, NULL, NULL);
        // clean up
        CloseHandle(hTempFile);
        free(lpFileBytes);
但是文件,它加密更多,而不是解密!。那么问题出在哪里呢? 这是我的全部代码:

https://pastebin.com/6WZX5J1K

我认为你应该先测试你的加密/解密,而不需要所有的文件

小心未经测试的代码

void Encrypt( std::string& txt) {
  // apply XOR encryption
  int i;
  char key[3] = {'*', '~', '#'};
  for (i = 0; i < txt.length(); i++) {
    txt[i] ^= key[i % sizeof(key)];
  }
}

bool Test() {
  std::string org { "123" };
  std::string encrypted = org;
  Encrypt(encrypted, encrypted.length());
  std::string decrypted = encrypted;
  Encrypt(decrypted, decrypted.length());
  // std::cout << org << " " << encrypted << " " << decrypted << std::endl;
  return org == decrypted;
}
void加密(std::string&txt){
//应用异或加密
int i;
字符键[3]={'*','~','#'};
对于(i=0;i//std::非常感谢,我已经测试了我的encrypt,它工作了,我通过看到0x2.txt的内容来验证这一点,它是加密的。但是在0x3.txt中解密的整个问题是,它包含加密的数据,而不是解密的。这是我的完整代码:,注释不是用于扩展讨论的;这段对话已经结束。
void Encrypt( std::string& txt) {
  // apply XOR encryption
  int i;
  char key[3] = {'*', '~', '#'};
  for (i = 0; i < txt.length(); i++) {
    txt[i] ^= key[i % sizeof(key)];
  }
}

bool Test() {
  std::string org { "123" };
  std::string encrypted = org;
  Encrypt(encrypted, encrypted.length());
  std::string decrypted = encrypted;
  Encrypt(decrypted, decrypted.length());
  // std::cout << org << " " << encrypted << " " << decrypted << std::endl;
  return org == decrypted;
}