公钥与私钥、iv(初始向量)

公钥(Public Key)与私钥(Private Key)是通过加密算法得到的一个密钥对(即一个公钥和一个私钥,也就是非对称加密方式)。公钥可对会话进行加密、验证数字签名,只有使用对应的私钥才能解密会话数据,从而保证数据传输的安全性。公钥是密钥对外公开的部分,私钥则是非公开的部分,由用户自行保管。

通过加密算法得到的密钥对可以保证在世界范围内是唯一的。使用密钥对的时候,如果用其中一个密钥加密一段数据,只能使用密钥对中的另一个密钥才能解密数据。例如:用公钥加密的数据必须用对应的私钥才能解密;如果用私钥进行加密也必须使用对应的公钥才能解密,否则将无法成功解密。
二、
iv即初始向量(IV,Initialization Vector),在有线等效保密(WEP)协议中,IV是用来和密钥组合成密钥种子,作为RC4算法的输入,来产生加密字节流对数据进行加密的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于椭圆曲线的多变量公钥加密算法(ECMQV)的 C++ 实现: ```c++ #include <iostream> #include <string> #include <utility> #include <vector> #include <openssl/ec.h> #include <openssl/ecdh.h> #include <openssl/evp.h> #include <openssl/rand.h> using namespace std; // 椭圆曲线参数 const char* curve_name = "secp256k1"; // 生成椭圆曲线密钥对 pair<EC_KEY*, EC_POINT*> generate_keypair() { // 创建椭圆曲线参数 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp256k1); // 生成私钥 EC_KEY* key = EC_KEY_new(); EC_KEY_set_group(key, group); EC_KEY_generate_key(key); // 生成公钥 EC_POINT* pub_key = EC_POINT_new(group); EC_POINT_copy(pub_key, EC_KEY_get0_public_key(key)); return make_pair(key, pub_key); } // 计算共享密钥 vector<unsigned char> compute_shared_secret(EC_KEY* my_key, EC_POINT* my_pub_key, EC_KEY* other_key, EC_POINT* other_pub_key) { // 创建椭圆曲线参数 EC_GROUP* group = EC_GROUP_new_by_curve_name(NID_secp256k1); // 计算共享密钥 size_t len = ECDH_compute_key(NULL, 0, other_pub_key, my_key, NULL); vector<unsigned char> shared_secret(len); ECDH_compute_key(shared_secret.data(), shared_secret.size(), other_pub_key, my_key, NULL); return shared_secret; } // 计算哈希值 vector<unsigned char> hash_message(string message) { EVP_MD_CTX* mdctx = EVP_MD_CTX_new(); EVP_DigestInit(mdctx, EVP_sha256()); EVP_DigestUpdate(mdctx, message.c_str(), message.length()); vector<unsigned char> hash(EVP_MD_size(EVP_sha256())); unsigned int hash_len; EVP_DigestFinal(mdctx, hash.data(), &hash_len); EVP_MD_CTX_free(mdctx); return hash; } int main() { // 生成 Alice 和 Bob 的密钥对 auto alice_keypair = generate_keypair(); auto bob_keypair = generate_keypair(); // Alice 和 Bob 互相交换公钥,计算共享密钥 auto alice_shared_secret = compute_shared_secret(alice_keypair.first, alice_keypair.second, bob_keypair.first, bob_keypair.second); auto bob_shared_secret = compute_shared_secret(bob_keypair.first, bob_keypair.second, alice_keypair.first, alice_keypair.second); // 比较 Alice 和 Bob 计算出的共享密钥是否相同 if (alice_shared_secret == bob_shared_secret) { cout << "Shared secret: "; for (auto b : alice_shared_secret) { printf("%02x", b); } cout << endl; } else { cout << "Error: shared secrets do not match" << endl; } // 使用共享密钥加密和解密消息 string message = "Hello World!"; vector<unsigned char> shared_secret = alice_shared_secret; // 使用 Alice 和 Bob 之间计算出的共享密钥 vector<unsigned char> iv(16); // 初始向量 RAND_bytes(iv.data(), iv.size()); // 生成随机的初始向量 vector<unsigned char> hash = hash_message(message); // 计算消息的哈希值 vector<unsigned char> key(hash.begin(), hash.begin() + 16); // 从哈希值中提取加密密钥 vector<unsigned char> ciphertext; // 加密后的消息 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key.data(), iv.data()); int out_len; ciphertext.resize(message.length() + EVP_CIPHER_CTX_block_size(ctx)); EVP_EncryptUpdate(ctx, ciphertext.data(), &out_len, (const unsigned char*)message.c_str(), message.length()); int ciphertext_len = out_len; EVP_EncryptFinal_ex(ctx, ciphertext.data() + ciphertext_len, &out_len); ciphertext_len += out_len; ciphertext.resize(ciphertext_len); EVP_CIPHER_CTX_free(ctx); cout << "Ciphertext: "; for (auto b : ciphertext) { printf("%02x", b); } cout << endl; string decrypted_message; // 解密后的消息 ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key.data(), iv.data()); decrypted_message.resize(ciphertext.size()); EVP_DecryptUpdate(ctx, (unsigned char*)decrypted_message.c_str(), &out_len, ciphertext.data(), ciphertext_len); int decrypted_len = out_len; EVP_DecryptFinal_ex(ctx, (unsigned char*)decrypted_message.c_str() + decrypted_len, &out_len); decrypted_len += out_len; decrypted_message.resize(decrypted_len); EVP_CIPHER_CTX_free(ctx); cout << "Decrypted message: " << decrypted_message << endl; return 0; } ``` 在这个实现中,我们使用 OpenSSL 库中的 ECDSA 和 ECDH 函数来进行椭圆曲线加密和密钥交换。我们首先生成 Alice 和 Bob 的密钥对,然后它们互相交换公钥,并计算出共享密钥。接着,我们使用共享密钥加密和解密消息。在这个实现中,我们使用 AES-128-CBC 模式来加密消息,并使用 SHA-256 哈希算法计算消息的哈希值,从中提取加密密钥。需要注意的是,我们需要生成一个随机的初始向量,以增加加密的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值