当前位置: 首页 > news >正文

一个app下载网站广州搜索排名优化

一个app下载网站,广州搜索排名优化,网站开发需要哪些职位,苏州做网站推广的公司一、【原理简介】非对称加密 非对称加密,也被称为公钥加密,其中使用一对相关的密钥:一个公钥和一个私钥。公钥用于加密数据,私钥用于解密数据。公钥可以公开分享,而私钥必须保密。 密钥生成: 当一个用户或设备希望使用…

一、【原理简介】非对称加密

非对称加密,也被称为公钥加密,其中使用一对相关的密钥:一个公钥和一个私钥。公钥用于加密数据,私钥用于解密数据。公钥可以公开分享,而私钥必须保密。

  1. 密钥生成:

    • 当一个用户或设备希望使用非对称加密时,要生成一对密钥:一个公钥和一个私钥。这两个密钥是数学上相关的,但从公钥中计算出私钥在计算上是不可行的。
  2. 加密过程:

    • 发送方使用接收方的公钥对消息进行加密。只有持有与那个公钥相对应的私钥的人(在这种情况下是接收方)才能解密这个消息。
  3. 解密过程:

    • 接收方使用其私钥对接收到的加密消息进行解密,以恢复原始消息。
  4. 安全性:

    • 即使攻击者知道公钥并且他们拦截了加密的消息,但由于他们没有私钥,所以他们不能解密那个消息。这提供了保密性。
    • 由于私钥不是公开的,因此无法用它来伪造加密的消息。
  5. 数字签名:

    • 除了保密性外,非对称加密还可以用于数字签名。发送方使用其私钥对消息的哈希进行加密,产生一个数字签名。
    • 接收方可以使用发送方的公钥来验证签名,确保消息是从发送方发出的,并且没有被修改。
  6. 主要算法:

    • RSA、ElGamal、ECC(椭圆曲线加密)是非对称加密的主要算法。
  7. 性能问题:

    • 与对称加密相比,非对称加密通常计算上更加昂贵,所以它在处理大量数据时可能不太实用。在实际场景中,经常使用非对称加密来交换一个对称密钥(如AES密钥),然后使用对称加密来加密实际的数据。
  8. 公钥基础设施(PKI):

    • PKI是一个结合硬件、软件、策略和标准,以实现公钥加密和数字签名服务的体系结构。它包括证书颁发机构(CA),负责颁发和验证数字证书。

总结:非对称加密使用一对密钥来确保数据的安全性和完整性。公钥用于加密,而私钥用于解密或签名。


二、【代码】生成秘钥对文件 和 读取秘钥对加密、加密

  • example 1 生成秘钥对
#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>/* compile : gcc createRSA_KeyPair.c -I./include -L./lib -lcrypto -Wl,-rpath=./lib  */
int main() {int ret = 0;RSA *r = NULL;BIGNUM *bne = NULL;int bits = 2048;unsigned long e = RSA_F4;// 1. 生成 RSA 密钥对bne = BN_new();ret = BN_set_word(bne, e);if(ret != 1) {goto free_all;}r = RSA_new();ret = RSA_generate_key_ex(r, bits, bne, NULL);if(ret != 1) {goto free_all;}// 2. 保存私钥到 PEM 文件FILE *fp = fopen("private_key.pem", "wb");if(fp == NULL) {perror("Unable to open private key file for writing");goto free_all;}PEM_write_RSAPrivateKey(fp, r, NULL, NULL, 0, NULL, NULL);fclose(fp);// 3. 保存公钥到 PEM 文件fp = fopen("public_key.pem", "wb");if(fp == NULL) {perror("Unable to open public key file for writing");goto free_all;}PEM_write_RSA_PUBKEY(fp, r);fclose(fp);free_all:RSA_free(r);BN_free(bne);return 0;
}
  • example 2 使用秘钥对加密、解密
#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>/* compile : gcc keyFromFile.c -I./include -L./lib -lcrypto -Wl,-rpath=./lib  */
int main() {// 加载公钥FILE* pubKeyFile = fopen("public_key.pem", "rb");RSA* rsaPublicKey = PEM_read_RSA_PUBKEY(pubKeyFile, NULL, NULL, NULL);fclose(pubKeyFile);// 加载私钥FILE* privKeyFile = fopen("private_key.pem", "rb");RSA* rsaPrivateKey = PEM_read_RSAPrivateKey(privKeyFile, NULL, NULL, NULL);fclose(privKeyFile);const char* plainText = "Hello, OpenSSL!";char encrypted[4098] = {};char decrypted[4098] = {};// 使用公钥加密int encryptedLength = RSA_public_encrypt(strlen(plainText), (unsigned char*)plainText,(unsigned char*)encrypted, rsaPublicKey, RSA_PKCS1_OAEP_PADDING);if (encryptedLength == -1) {printf("Public Encrypt failed \n");return 0;}// 使用私钥解密int decryptedLength = RSA_private_decrypt(encryptedLength, (unsigned char*)encrypted,(unsigned char*)decrypted, rsaPrivateKey, RSA_PKCS1_OAEP_PADDING);if (decryptedLength == -1) {printf("Private Decrypt failed \n");return 0;}decrypted[decryptedLength] = '\0';printf("Original: %s\n", plainText);printf("Decrypted: %s\n", decrypted);RSA_free(rsaPublicKey);RSA_free(rsaPrivateKey);return 0;
}

三、【代码】代码内生成秘钥对并加密解密

  • c风格
#include <stdio.h>
#include <string.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>/* compile : gcc RSA.c -I./include -L./lib -lcrypto -Wl,-rpath=./lib */
int main() {// 1. create keyint ret = 0;RSA *rsa = NULL;BIGNUM *bne = NULL;int bits = 2048;unsigned long e = RSA_F4;bne = BN_new();ret = BN_set_word(bne, e);if(ret != 1) {goto free_all;}rsa = RSA_new();ret = RSA_generate_key_ex(rsa, bits, bne, NULL);if(ret != 1) {goto free_all;}// 2. encryptoconst char* plainText = "Hello, OpenSSL!";char encrypted[4098] = {};int encryptedLength = RSA_public_encrypt(strlen(plainText) + 1, (unsigned char*)plainText,(unsigned char*)encrypted, rsa, RSA_PKCS1_OAEP_PADDING);if(encryptedLength == -1) {printf("Public Encrypt failed \n");goto free_all;}// 3. decryptochar decrypted[4098] = {};int decryptedLength = RSA_private_decrypt(encryptedLength, (unsigned char*)encrypted,(unsigned char*)decrypted, rsa, RSA_PKCS1_OAEP_PADDING);if(decryptedLength == -1) {printf("Private Decrypt failed \n");goto free_all;}decrypted[decryptedLength] = '\0';printf("Original: %s\n", plainText);printf("Decrypted: %s\n", decrypted);free_all:RSA_free(rsa);BN_free(bne);return 0;
}
  • C++风格
#include <iostream>
#include <vector>
#include <stdexcept>extern "C"{
#include <openssl/pem.h>
#include <openssl/rsa.h>
}class RSAWrapper {
public:RSAWrapper() {rsa = RSA_new();bne = BN_new();BN_set_word(bne, RSA_F4);RSA_generate_key_ex(rsa, 2048, bne, nullptr);}~RSAWrapper() {RSA_free(rsa);BN_free(bne);}std::vector<unsigned char> encrypt(const std::string &plainText) {std::vector<unsigned char> encrypted(RSA_size(rsa));int encryptLength = RSA_public_encrypt(plainText.size(),reinterpret_cast<const unsigned char*>(plainText.c_str()),encrypted.data(),rsa,RSA_PKCS1_OAEP_PADDING);if (encryptLength == -1) {throw std::runtime_error("Error during encryption");}return encrypted;}std::string decrypt(const std::vector<unsigned char> &cipherText) {std::vector<unsigned char> decrypted(RSA_size(rsa));int decryptLength = RSA_private_decrypt(cipherText.size(),cipherText.data(),decrypted.data(),rsa,RSA_PKCS1_OAEP_PADDING);if (decryptLength == -1) {throw std::runtime_error("Error during decryption");}return std::string(reinterpret_cast<char*>(decrypted.data()));}private:RSA *rsa;BIGNUM *bne;
};/* compile : g++ -std=c++11  RSA.cpp -I./include -L./lib -lcrypto -Wl,-rpath=./lib */
int main() {try {RSAWrapper rsaWrapper;std::string plainText = "Hello, OpenSSL C++!";auto encrypted = rsaWrapper.encrypt(plainText);auto decrypted = rsaWrapper.decrypt(encrypted);std::cout << "Original Text: " << plainText << std::endl;std::cout << "Decrypted Text: " << decrypted << std::endl;} catch (const std::exception &e) {std::cerr << "Error: " << e.what() << std::endl;}return 0;
}
http://www.hkea.cn/news/112776/

相关文章:

  • 精准营销数据杭州排名优化软件
  • 中卫网站建站设计seo学习论坛
  • wordpress初始登录seo排名赚app靠谱吗
  • 软件外包保密协议seo相关岗位
  • 后台网站开发文档下载班级优化大师app
  • 辛集城乡建设管理局网站网络营销网络推广
  • 阿里云部署一个自己做的网站吗电商网站搭建
  • 免费汽车租赁网站模板网站域名解析ip查询
  • 企业解决方案官网国内seo排名分析主要针对百度
  • 变态版手游石景山区百科seo
  • 阿里云控制台登录入口seo矩阵培训
  • wordpress苗木模板网站搜索排优化怎么做
  • 网站图片引导页怎么做重庆seo招聘
  • 如何做属于自己的领券网站郑州百度网站优化排名
  • 建设银行益阳市分行桃江支行网站公司页面设计
  • vps 网站上传网站seo优化是什么意思
  • wordpress cos腾讯云seo网站优化收藏
  • 鹤岗商城网站建设免费域名申请
  • 江苏三个地方疫情严重抖音视频排名优化
  • 竞价排名广告东莞关键词排名快速优化
  • 做视频网站要什么格式好网络营销公司怎么注册
  • 企业专业网站建设快速网站搭建
  • 武威建设网站的网站google谷歌搜索
  • 长沙公司做网站多少钱推广平台怎么做
  • 现在大家做电商网站用什么源码营销策略都有哪些
  • 可以做试卷的网站英语怎么说seo关键词排名优化系统源码
  • 网站怎么设置支付功能企业网站的主要类型有
  • 成都圣都装饰装修公司北京搜索优化排名公司
  • 境外建设网站贴吧互联网域名注册查询
  • 广州建站工作室淘客推广怎么做