如何将RSA C 的字符串值解密
How to do I decrypt back to string value for RSA c++
我使用RSA算法很难解密,我从解密函数的结果中获得的一切都是数值,而不是用户输入的实际字符字符串值。p>这是我的代码
#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<<"nn 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
实际值3752432432
->如何将其转换为字母格式。
代码中的第一个问题是未定义的行为:
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 标准可以保证元素在此处存储。这意味着您可以在需要时获得原始数组。
您可以指定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 << "bb]";
}
return out;
}
我假设您将vector<int>
用于加密值。在这种情况下,上面的操作员将输出值而不是指针地址0x7fff5fbfedd0
。
实际值 375 243 2432->如何将其转换为字母格式
您无需在解密值中添加任何96
常数。解密后,您应该获得原始值。之所以没有发生的原因是int
值溢出。重新检查您的计算和结果值,例如您需要将c_d_value
类型更改为std::vector<unsigned long long> c_d_value
。
相关文章:
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- C++ - 从文件解密字符串
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- 异或字符串加密/解密输出错误
- AES/CFB密码模式从字符串失败中解密
- 上和小写字符串的加密和解密,基本密码C
- 使用 rsa 和 Windows 加密 API 加密/解密字符串和文件
- 加密和解密C++和德尔福中的一些字符串
- 如何将RSA C 的字符串值解密
- OpenSSL AES_CBC-256解密的字符串并不总是正确的
- 解密AES字符串
- 解密字符串末尾的 NULL 终止符需要什么?
- 无法解密这些字符串
- 使用数组解密字符串
- 字符串未使用 <openssl/aes>正确解密
- CryptDecrypt在解密字符串的末尾返回随机字符
- 在android NDK jni中加密和解密utf8字符串
- 发送c# AES加密字符串到CyptoPP c++程序解密
- 使用c++、Openssl和aes加密和解密字符串
- 在c++中发出加密字符串,在PHP中发出解密