2016-11-22 32 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 - >我怎麼轉換這封信格式。在代碼

+0

「'int numeric_value_msg [Message.length()]; //動態數組分配」這就是你的想法。這實際上是大多數編譯器的編譯器錯誤。 – user4581301

+0

@ user4581301它沒有任何區別.. :( – KoushKilla

+0

代碼和輸出重新格式化,使其更清晰。 – Nikita

回答

0

第一個問題是未定義行爲在:

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

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

Message總是空時numerical_value_msg陣列創建的。之後Message得到它的值,for循環訪問內存超出numerical_value_msg中的數組邊界。我建議用vector<int>替換int[]類型。 C++標準guarantees表示元素連續存儲在那裏。這意味着您可以在需要時獲取原始數組。

你可以指定operator<<將數據打印到cout

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

實際值\ 375 \ 243 \ 2432 - >我怎麼轉換這封信格式

你不需要任何96不斷添加到解密值。解密後,您應該獲得原始值。它沒有發生的原因是int值溢出。重新檢查你的計算和結果值,例如您需要將c_d_value類型更改爲std::vector<unsigned long long> c_d_value