2016-11-22 4 views
1

RSAアルゴリズムを使用して解読するのが苦労しているのは、解読関数の結果から得られたすべてが数値であり、実際の文字列値ではない入力されます。する方法私はRSAの文字列値に復号化するC++

はここ

#include <iostream> 
#include <cstdlib> 
#include <string> 
#include <ctime> 
#include <math.h> 
#include <exception> 
#include <stdexcept> 
#include <vector> 
using namespace std; 
int main(){ 
    long int v2 = (rand() % 900 + 800); 
    long int v1 = (rand() % 900 + 800); 
    for (int i=v1; i<v1+100; i++) 
    { 
     bool prime=true; 
     for (int j=2; j*j<=i; j++) 
     { 
      if (i % j == 0) 
      { 
       prime=false; 
       break; 
      } 
     } 
     if(prime){ 
      // v2 = int a[i]; 
      cout << i << " "; 
     } 
    } 
    int d; 
    int e = 3; 
    int p = 1087; 
    int q = 1091; 
    int p1 = (p-1); 
    int p2 = (q-1); 
    int phi = p1*p2; 
    int n = p*q; 
    int d_value; 
    cout<<"phi is " <<phi<<endl; 
    cout<<"n is " <<n<<endl; 

    int sk; 
    //Simple iteration to find d using forloop. E*Dmodn = 1 
    for(int i=1;i>0;i++) 
    { 
     int x=i*e; 
     d=x%n; 
     if(d==1) //E*Dmodn = 1 
     { 
      d_value=i; 
      cout<<"\n\n Private Key ::"<<d_value; 
      break; 
     } 
    } // end of forloop 

    // User Input, option starts here 
    string Message; 
    int numerical_value_msg[Message.length()]; // Dynamic array allocation 
    cout << "Please, enter your full name: "; 
    getline (cin,Message); 
    cout << Message << "!\n"; 
    for(int i = 0; i<Message.length(); i++){ // Trying to transfer string of character to numerical value 
     numerical_value_msg[i] = Message[i]; 
    } 
    cout<<numerical_value_msg<<endl; // Output of Numerical Value of Message 

    // Encryption starts here 
    int size = sizeof(numerical_value_msg); 
    int encryption_value[100]; 
    int m_e_value[100]; 
    for(int i=0; i<size;i++){ 
     m_e_value[i] = pow(numerical_value_msg[i],e); //exponent from math.h 
     encryption_value[i] = m_e_value[i]%n; 
     //cout<<"e"<<encryption_value<<endl; 
     cout<<"e"<<encryption_value<<endl; 
     } 

    //Decryption Value 
    int size2 = sizeof(encryption_value); 
    int c_d_value[100]; 
    char actual_value[Message.length()]; 

    for(int i=0; i<Message.length();i++){ 
     cout<<"DValue"<<d_value<<endl; 
     c_d_value[i] = pow(encryption_value[i],d_value); 
     actual_value[i] = (m_e_value[i]%n)+96; // + 96 to convert it to Letter value 
    } 
    cout<<"Actual value"<<actual_value<<endl; 
} 

MyOutputは、このような

Private Key ::395306 Please, enter Text TO convert: 
Hello Hello! 
0x7fff5fbfedd0 
encrytion value is 0x7fff5fbff620 
encrytion value is 0x7fff5fbff620 
encrytion value is 0x7fff5fbff620 
encrytion value is 0x7fff5fbff620 
encrytion value is 0x7fff5fbff620 

実際の値として読み込み、私のコードです\375\243\2432 - >私は手紙形式にこれを変換しますか。 numerical_value_msg配列が作成されたときに

string Message; 
int numerical_value_msg[Message.length()]; // Dynamic array allocation 

// Lines skipped 
numerical_value_msg[i] = Message[i]; 

Message常に空です:コード内

+0

"int numeric_value_msg [Message.length()]; //動的配列の割り当て"それはあなたが考えるものです。実際には、ほとんどのコンパイラではコンパイラエラーです。 – user4581301

+0

@ user4581301それはまったく違いはありません.. :( – KoushKilla

+0

コードと出力がより明確になるように再フォーマットされました。 – Nikita

答えて

0

最初の問題は、未定義の動作です。その後、Messageは値を取得し、forループアクセスメモリは配列外の境界numerical_value_msgにあります。 タイプをvector<int>に置き換えることをお勧めします。 C++標準guaranteesは、要素がそこに連続して格納されています。これは、必要に応じて生の配列を取得できることを意味します。

あなたはcoutにデータを印刷するoperator<<を指定することができます。

template <typename T> 
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) { 
    if (!v.empty()) { 
    out << '['; 
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); 
    out << "\b\b]"; 
    } 
    return out; 
} 

私はあなたが暗号化された値のためvector<int>を使用すると仮定します。この場合、上記の演算子はポインタアドレス0x7fff5fbfedd0ではなく値を出力します。 243 \ 2432 \ 375 \

実際の値は、 - >どのように私はあなたが復号化された値に任意の96定数を追加する必要はありません手紙形式

にこれを変換します。復号後、元の値を取得する必要があります。それが起こらない理由は、intの値のオーバーフローです。計算結果と結果の値を再確認します。 c_d_valueタイプをstd::vector<unsigned long long> c_d_valueに変更する必要があります。

関連する問題