0
ElGamal 알고리즘을 사용하여 텍스트 문서를 암호화하는 방법을 알고 싶습니까? 이 알고리즘을 사용하여 정수의 암호화를 받았습니다. 예를 들어 도와주세요ElGamal 암호화를 사용하여 텍스트를 암호화하는 방법
ElGamal 알고리즘을 사용하여 텍스트 문서를 암호화하는 방법을 알고 싶습니까? 이 알고리즘을 사용하여 정수의 암호화를 받았습니다. 예를 들어 도와주세요ElGamal 암호화를 사용하여 텍스트를 암호화하는 방법
다음은 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);
}