首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ElGamal加密/解密图像文件

使用ElGamal加密/解密图像文件
EN

Stack Overflow用户
提问于 2016-04-21 19:47:07
回答 1查看 1K关注 0票数 0

我试图用ElGamal在C++中加密和解密图像文件。它必须使用ElGamal加密。我想同时保存加密文件和恢复文件。我在加密/解密部分使用Crypto++库。这是我到目前为止所拥有的。

代码语言:javascript
复制
AutoSeededRandomPool prng;

ElGamal::Decryptor decryptor;
decryptor.AccessKey().GenerateRandomWithKeySize(prng, 2048);
const ElGamalKeys::PrivateKey& privateKey = decryptor.AccessKey();

ElGamal::Encryptor encryptor(decryptor);
const PublicKey& publicKey = encryptor.AccessKey();

string ofilename = "test.bmp";
string efilename = "test.enc";
string rfilename = "test-recovered.bmp";

FileSource fs1(ofilename.c_str(), true, encryptor.CreateEncryptionFilter(encryptor.Encrypt, new FileSink(efilename.c_str())));

FileSource fs2(efilename.c_str(), true, decryptor.CreateDecryptionFilter(decryptor.Decrypt, new FileSink(rfilename.c_str())));

我被困在加密和解密部分了。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2016-04-25 07:42:47

您的问题是,您试图使用非对称密码系统对任意大的数据进行加密。非对称密码系统不能对长度大于模数大小的数据进行加密。(见为什么我的ElGamal实现不能用于长文本字符串?)

这个限制的典型解决方法是使用标准对称算法(例如AES)对输入执行对称转换,然后使用非对称公钥对对称密钥进行加密。然后,解密反转操作,首先解密对称密钥,然后使用对称密钥解密加密的内容。

Crypto++ ElGamal对象提供SymmetricEncrypt和SymmetricDecrypt。这些函数将在对称密钥下加密和解密任意长度的文本,然后对ElGamal公钥下的对称密钥进行加密。(见Crypto++ Wiki - ElGamal)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36779113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档