不同程序中的加密和解密
Encryption and Decryption in Different Program
我正试图在Ubuntu 12.04下使用Crypto++实现RSA算法我设法在一个程序中实现了加密和解密。有什么办法可以把加密和解密分开吗?我想要的是,当我调用加密部分时,它会创建一个密文作为输出,然后当我调用解密部分时,他会将加密中的密文作为输入,如果我先调用解密部分,那么它会创建错误消息。
这是加密和解密的代码:
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::hex;
#include <string>
using std::string;
#include "rsa.h"
using CryptoPP::RSA;
#include "integer.h"
using CryptoPP::Integer;
#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;
int main(int argc, char** argv)
{
// Pseudo Random Number Generator
AutoSeededRandomPool rng;
///////////////////////////////////////
// Generate Parameters
CryptoPP::InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 3072);
///////////////////////////////////////
// Generated Parameters
const Integer& n = params.GetModulus();
const Integer& p = params.GetPrime1();
const Integer& q = params.GetPrime2();
const Integer& d = params.GetPrivateExponent();
const Integer& e = params.GetPublicExponent();
cout << endl;
///////////////////////////////////////
// Create Keys
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
/////////////////////////////////////////////////////////
string message, recovered;
Integer m, c, r;
cout << endl;
cout << "RSA Algorithm" << endl;
cout << "message: " ;
std::cin >> message;
// Treat the message as a big endian array
m = Integer((const byte *)message.data(), message.size());
cout << "plaintext: " << hex << m << endl << endl;
cout << "ENCRYPTION" << endl;
// Encrypt
c = publicKey.ApplyFunction(m);
cout << "cipherthext: " << hex << c << endl << endl;
cout << "DECRYPTION" << endl;
// Decrypt
r = privateKey.CalculateInverse(rng, c);
cout << "plaintext: " << hex << r << endl;
// Round trip the message
size_t req = r.MinEncodedSize();
recovered.resize(req);
r.Encode((byte *)recovered.data(), recovered.size());
cout << "recovered: " << recovered << endl;
return 0;
}
我感谢你的帮助。非常感谢。
您可以选择
- 编写两个程序,每个程序都有自己的主程序
- 使用您发送到main的
argv
来告诉它您希望它做什么。
对于备选方案2,这里还有另一个问题。本质上,在检查了arg计数argc
并记住argv[0]是您的程序之后,您可以说
if(strcmp(argv[1], "ENCRYPTION")==0)
{
//... do ENCRYPTION
}...
相关文章:
- POCO C++加密/解密
- 异或字符串加密/解密输出错误
- 简单仿射密码加密解密
- QT:AES-256-CBC 根据 PHP 代码在C++中加密/解密
- AES加密/解密接收所有明文
- 牡丹AES CBC PKCS7加密解密
- 加密/解密字节数组加密++
- 使用EVP_*进行加密/解密
- 使用 rsa 和 Windows 加密 API 加密/解密字符串和文件
- 使用 <wincrypt.h 加密解密期间丢失字符>
- RSA 加密/解密 Polarssl C++.
- 简单C++加密 - 解密库
- 在Qt中使用博坦加密大文件时如何获得加密/解密进度
- AES-128加密/解密
- C++RSA使用XML密钥进行加密/解密
- 无法使用英特尔 AES-NI 示例库以 32 字节块大小加密/解密
- 程序集asm x86加密/解密程序
- Mupdf加密解密问题
- 如何使用RSA/AES加密/解密较大的文件
- AES CBC 加密/解密仅解密前 16 个字节