ELGamal公钥密码算法代码实现C++

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int pow_mod(int a, int b, int p); //pow and mod运算 --核心  a的b次方mod p 
void encryption(int m, int pub_key, int p, int g, int* c1, int* c2); //加密算法 
//m--密文  pub--公钥  p--素数 g--本原元 c1、c2 --密文 
int decryption(int c1, int c2, int pub_key, int p, int g); // 解密算法
bool is_prime(int p); //判断是否为素数
 
//main函数测试 
int main() {
    int p;//素数
    int g ; //本元根 
    cout << "Please input g:";
	cin >> g; 
	cout << endl; 

    do {
        cout << "Please enter a prime number: ";
        cin>>p;
    } while (!is_prime(p));
    cout << endl;
    cout << "Enter the private key of user A: "; //A的私钥 
    int key_A;
    cin>>key_A;
    cout << endl;
    int pub; // A的公钥 
    pub = pow_mod(g, key_A, p);
    cout << "the public key of user A: "<<pub<<endl;
    cout << endl; 
    cout << "Input plaintext(smaller than "<< p<<"): ";
    int m; // 明文 
    cin>>m;
    cout << endl; 
    int c1, c2; // 密文 
    encryption(m, pub, p, g, &c1, &c2);
    cout << "The ciphertext encrypted with the public key is:" << endl;
	cout << "c1= " << c1 << "    " << "c2= " <<c2 << endl;;
	cout << endl;
    int m_ = decryption(c1, c2, key_A, p, g);
    cout << "Plaintext decrypted with private key:"<< m_ << endl;

    return 0;
}

//加密算法
void encryption(int m, int pub_key, int p, int g, int* c1, int* c2) {
    int k; //随机数
	cout << "Please input a random data: ";
	cin >> k; 
    *c1 = pow_mod(g, k, p);
    *c2 = m * pow_mod(pub_key, k, p) % p;
}
//解密算法
int decryption(int c1, int c2, int pub_key, int p, int g) {
    int m;
    int c1_ = pow_mod(c1, p - 2, p);
    m = c2 * pow_mod(c1_, pub_key, p) % p;
    return m;
}
//判断是否为素数
bool is_prime(int p) {
    int i;
    for (i = 2; i <= sqrt(p); i++) {
        if (p % i == 0)
            return false;
    }
    return true;
}

//pow and mod运算 --核心 
int pow_mod(int a, int b, int p) {
    int ans = 1;
    int tmp = a % p;
    while (b) {
        if (b & 1)
            ans = ans * tmp % p;
        b >>= 1;
        tmp = tmp * tmp % p;
    }
    return ans % p;
}
  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面给出一个使用 Crypto++ 库实现 ElGamal 公钥加密算法的示例代码,供参考: ```cpp #include <iostream> #include <cryptopp/elgamal.h> #include <cryptopp/osrng.h> using namespace CryptoPP; using namespace std; int main() { // 定义 ElGamal 密钥 AutoSeededRandomPool rng; // 随机数生成器 ElGamalKeys::PrivateKey privateKey; ElGamalKeys::PublicKey publicKey; privateKey.GenerateRandomWithKeySize(rng, 256); publicKey.AssignFrom(privateKey); // 输出公钥和私钥 cout << "Private key: " << endl; cout << "x: " << privateKey.GetPrivateExponent() << endl; cout << "p: " << privateKey.GetGroupParameters().GetModulus() << endl; cout << "Public key: " << endl; cout << "y: " << publicKey.GetPublicElement() << endl; cout << "p: " << publicKey.GetGroupParameters().GetModulus() << endl; // 加密和解密 string message = "Hello, world!"; ElGamal::Encryptor encryptor(publicKey); ElGamal::Decryptor decryptor(privateKey); SecByteBlock ciphertext(encryptor.CiphertextLength(message.size())); encryptor.Encrypt(rng, (const byte*)message.data(), message.size(), ciphertext); SecByteBlock plaintext(encryptor.MaxPlaintextLength(ciphertext.size())); size_t decryptedSize = decryptor.Decrypt(rng, ciphertext, ciphertext.size(), plaintext); plaintext.resize(decryptedSize); // 输出加密结果和解密结果 cout << "Message: " << message << endl; cout << "Ciphertext: " << (const char*)ciphertext.data() << endl; cout << "Plaintext: " << (const char*)plaintext.data() << endl; return 0; } ``` 上述代码实现了使用 256 位素数进行 ElGamal 公钥加密的功能。其中,AutoSeededRandomPool 类用于生成随机数,ElGamalKeys::PrivateKey 和 ElGamalKeys::PublicKey 分别表示私钥和公钥ElGamal::Encryptor 和 ElGamal::Decryptor 分别用于加密和解密。具体使用方法可以参考 Crypto++ 的官方文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值