-1

Ubuntu 12.04 で Crypto++ を使用して RSA アルゴリズムを実装しようとしています。1 つのプログラムで暗号化と復号化の両方を実装することができました。暗号化と復号化を分離する方法はありますか? 私が望むのは、暗号化部分を呼び出すと、出力として暗号文が作成され、復号化部分を呼び出すと、暗号化から暗号文が入力として取得され、最初に復号化部分を呼び出すと、エラーメッセージが作成されます。

これは、暗号化と復号化のコードです。

#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;
}

助けていただければ幸いです。ありがとうございました。

4

1 に答える 1

0

次のいずれかを実行できます

  1. それぞれ独自の main を持つ 2 つのプログラムを作成します。
  2. メインに送信するを使用しargvて、何をしたいかを伝えます。

オプション 2 については、ここで別の質問があります。要するに、引数の数を確認し、argcargv[0] が自分のプログラムであることを思い出した後、次のように言うことができます。

if(strcmp(argv[1], "ENCRYPTION")==0)
{
//... do ENCRYPTION
}...
于 2013-07-24T15:45:34.670 に答える