【问题标题】:How to encrypt text using ElGamal encryption如何使用 ElGamal 加密对文本进行加密
【发布时间】:2013-10-20 17:42:46
【问题描述】:

我只想知道如何使用 ElGamal 算法加密文本文档?我使用这个算法加密了整数。请帮忙举个例子

【问题讨论】:

    标签: encryption elgamal


    【解决方案1】:

    这是一个使用 Crypto++ 的示例。为ElGamal encryption example? 提供了答案。

    你知道你打算使用什么语言或库吗?

    #include <iostream>
    using std::cout;
    using std::cerr;
    using std::endl;
    
    #include <cryptopp/osrng.h>
    using CryptoPP::AutoSeededRandomPool;
    
    #include <cryptopp/secblock.h>
    using CryptoPP::SecByteBlock;
    
    #include <cryptopp/elgamal.h>
    using CryptoPP::ElGamal;
    using CryptoPP::ElGamalKeys;
    
    #include <cryptopp/cryptlib.h>
    using CryptoPP::DecodingResult;
    
    int main(int argc, char* argv[])
    {
        ////////////////////////////////////////////////
        // Generate keys
        AutoSeededRandomPool rng;
    
        cout << "Generating private key. This may take some time..." << endl;
    
        ElGamal::Decryptor decryptor;
        decryptor.AccessKey().GenerateRandomWithKeySize(rng, 512);
        const ElGamalKeys::PrivateKey& privateKey = decryptor.AccessKey();
    
        ElGamal::Encryptor encryptor(decryptor);
        const PublicKey& publicKey = encryptor.AccessKey();
    
        ////////////////////////////////////////////////
        // Secret to protect
        static const int SECRET_SIZE = 16;
        SecByteBlock plaintext( SECRET_SIZE );
        memset( plaintext, 'A', SECRET_SIZE );
    
        ////////////////////////////////////////////////
        // Encrypt
    
        // Now that there is a concrete object, we can validate
        assert( 0 != encryptor.FixedMaxPlaintextLength() );
        assert( plaintext.size() <= encryptor.FixedMaxPlaintextLength() );
    
        // Create cipher text space
        size_t ecl = encryptor.CiphertextLength( plaintext.size() );
        assert( 0 != ecl );
        SecByteBlock ciphertext( ecl );
    
        encryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext );
    
        ////////////////////////////////////////////////
        // Decrypt
    
        // Now that there is a concrete object, we can check sizes
        assert( 0 != decryptor.FixedCiphertextLength() );
        assert( ciphertext.size() <= decryptor.FixedCiphertextLength() );
    
        // Create recovered text space
        size_t dpl = decryptor.MaxPlaintextLength( ciphertext.size() );
        assert( 0 != dpl );
        SecByteBlock recovered( dpl );
    
        DecodingResult result = decryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered );
    
        // More sanity checks
        assert( result.isValidCoding );
        assert( result.messageLength <= decryptor.MaxPlaintextLength( ciphertext.size() ) );
    
        // At this point, we can set the size of the recovered
        //  data. Until decryption occurs (successfully), we
        //  only know its maximum size
        recovered.resize( result.messageLength );
    
        // SecByteBlock is overloaded for proper results below
        assert( plaintext == recovered );
    
        // If the assert fires, we won't get this far.
        if(plaintext == recovered)
            cout << "Recovered plain text" << endl;
        else
            cout << "Failed to recover plain text" << endl;
    
        return !(plaintext == recovered);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-21
      • 1970-01-01
      • 2022-06-17
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      相关资源
      最近更新 更多