2017-04-16 26 views
-2

私はC++の初心者です。私はXOR暗号化プログラムで作業していました。自分のプログラムに手動でコードを入力すると、私のプログラムは正常に動作します。この問題は、暗号化されたフレーズを復号しようとしたときに発生し、プログラムはフレーズの最初の単語のみを返します。XORの文字列を入力/出力する際のC++の問題

例: 、入力された文字列を暗号化する:Hello Worldの

キー:Q

暗号化された文字列:V

解読する

、入力された文字列:V

キー:Q

出力:こんにちは

それは私に最初の言葉を返すだけで、私は入力と出力を取っている方法と関係があると思います。

ありがとうございました!

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

using namespace std; 
/*Encrypt/decrypt program that takes in a string and encrypts it using a single byte xor encryption. Program can also decrypt a given string with or without a key.*/ 

string XOR(string input, char key){ 
    string XORString = input; 
    string newXOR = ""; 
    for(int i = 0; i < input.length();i++){ 
     newXOR += XORString[i]^(int(key+i))%255; 
    } 
    return newXOR; 
} 

int main() 
{ 
    /*char key = 'q'; 
    std::cout << XOR("hello world",key); 
    return 0; 
    The lines above are for manual testing if needed*/ 
    bool b = true; 
    while(b){ 
     int x; 
     cout<<"Enter 1 for Encryption, 2 for Decryption, 3 for Decryption without key or 4 to end program."<<endl; 
     cin>>x; 
     if(x==1){ 
      string phrase1 = ""; 
      cout<< "Enter a phrase to encrypt: "; 
      cin.ignore(); 
      getline(std::cin,phrase1); 
      cout<<"Enter a key to encrypt with: "; 
      char key1; 
      cin>> key1; 
      std::cout << XOR(phrase1,key1)<< endl; 
     } 
     else if(x==2){ 
      string phrase = ""; 
      cout<< "Enter a phrase to decrypt: "; 
      cin.ignore(); 
      std::getline(std::cin,phrase); 
      cout<<"Enter a key to decrypt with: "; 
      char key2; 
      cin>>key2; 
      std::cout << XOR(phrase,key2)<<endl; 
     } 
     else if(x==3){ 
      char keys [] = {'!','"','#','$','%','&','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[',']','^','_','`','a','b','c','d','e','f','g','h','I','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~'}; 
      int end = 92; 
      int count = 0; 
      string s; 
      cout<< "Enter phrase: "; 
      cin>>s; 
      for(int i = 0; i < 92;i++){ 
       count++; 
       cout<<count<<" possible combination out of " << end << " using " << keys[i]<< ":"<<XOR(s,keys[i])<<endl; 
      } 
     } 
     else if(x==4){ 
      cout<<"Have a nice day!"; 
      b = false; 
     } 
     else{ 
      cout<<"You entered an invalid number"; 
      break; 
     } 
    } 
    return 0; 

これは、問題のコードのようになります。

  string phrase1 = ""; 
      cout<< "Enter a phrase to encrypt: "; 
      cin.ignore(); 
      getline(std::cin,phrase1); 
+0

'^'はhiを持っていますか'%'より優先順位が高いですか?あなたとあなたの読者は、括弧を付けて苦痛を軽減しましょう。 –

+0

これは恐らく文字とのXORの問題です。文字が等しい場合は、C/C++で文字列の終わりを意味するヌル文字(制御文字)を生成します。 XORはバイナリです。結果のバイナリアジャイルを処理するかエンコードするか*モジュラ追加のような別の暗号化方式を使用する必要があります –

答えて

0

私は問題はこれを試して、あなたの文字列XORString機能であると思う:

//change this 
newXOR += XORString[i]^(int(key+i))%255; 
//to 
newXOR = XORString[i]; 

私は、これは「私はドン問題を解決を願って演算子の排他的ORについて多くのことを知っていますが、文字列関数の問題を引き起こしていると思います。

+0

それが機能しない場合は、別の方法で解決できるかどうかを教えてください。 – Tyger

関連する問題