开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
开源项目 > 程序开发 > 编程语言/脚本语言 &&
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
13 Star 69 Fork 22

johnsonyl/cpps

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
标签 (4)
master
v1.0.3-beta
v1.0.3
v1.0.2
v1.0.0
cpps
/
libs
/
rsa
/
rsa.cpp
cpps
/
libs
/
rsa
/
rsa.cpp
rsa.cpp 13.33 KB
一键复制 编辑 原始数据 按行查看 历史
johnsonyl 提交于 2024年01月13日 14:13 +08:00 . 优化cpps运行速度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

#include <cpps/cpps.h>
#include <iostream>
#include <string>
#include <fstream>
#include <openssl/err.h>
#include <openssl/ossl_typ.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/x509v3.h>
#include <openssl/ssl.h>
#include <openssl/pkcs12.h>
#include <openssl/pkcs7.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include <openssl/conf.h>
#include <openssl/conf_api.h>
#define ALLOW_SSL_ERROR_PRINTF (0)
#if ALLOW_SSL_ERROR_PRINTF
SSL_load_error_strings();
#endif
#if ALLOW_SSL_ERROR_PRINTF
ERR_print_errors_fp(stderr);
#endif
#if ALLOW_SSL_ERROR_PRINTF
ERR_free_strings();
#endif
using namespace cpps;
using namespace std;
// 公钥解密
std::string rsa_pub_decrypt(std::string cipherText, std::string pubKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char*)pubKey.c_str(), -1);
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
BIO_free_all(keybio);
return std::string("");
}
size_t len = (size_t)RSA_size(rsa);
char* decryptedText = (char*)malloc(len + 1);
memset(decryptedText, 0, len + 1);
// 解密函数
int ret = RSA_public_decrypt((int32)cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa,(size_t) RSA_PKCS1_PADDING);
if (ret >= 0)
strRet.assign(decryptedText, ret);
else {
ERR_load_crypto_strings();
char err[512];
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "%d,Error decrypting message: %s\n", (int32)cipherText.size(),err);
printf("err : %d, %s", ret, decryptedText);
}
// 释放内存
free(decryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
std::string rsa_pub_encrypt(std::string clearText, std::string pubKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -1);
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
size_t len = (size_t)RSA_size(rsa);
//int len = 1028;
char* encryptedText = (char*)malloc(len + 1);
memset(encryptedText, 0, len + 1);
// 加密函数
int ret = RSA_public_encrypt((int32)clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= 0)
strRet.assign(encryptedText, ret);
else
printf("err : %d, %s", ret, encryptedText);
// 释放内存
free(encryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
std::string rsa_pub_encrypt_no_padding(std::string clearText, std::string pubKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -1);
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
size_t len = (size_t)RSA_size(rsa);
//int len = 1028;
char* encryptedText = (char*)malloc(len + 1);
memset(encryptedText, 0, len + 1);
char* clearText_2 = (char*)malloc(len * 2);
memset(clearText_2, 0, len * 2);
memcpy(clearText_2, clearText.c_str(), clearText.size());
// 加密函数
int ret = RSA_public_encrypt((int32)len, (const unsigned char*)clearText_2, (unsigned char*)encryptedText, rsa, RSA_NO_PADDING);
if (ret >= 0)
strRet.assign(encryptedText, ret);
else
printf("err : %d, %s", ret, encryptedText);
// 释放内存
free(encryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
std::string rsa_pri_encrypt(std::string clearText, std::string priKey)
{
std::string strRet;
RSA* rsa = RSA_new();
BIO* keybio;
keybio = BIO_new_mem_buf((unsigned char*)priKey.c_str(), -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
size_t len = (size_t)RSA_size(rsa);
char* encryptedText = (char*)malloc(len + 1);
memset(encryptedText, 0, len + 1);
// 解密函数
int ret = RSA_private_encrypt((int32)clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= 0)
strRet.assign(encryptedText, ret);
// 释放内存
free(encryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
// 私钥解密
std::string rsa_pri_decrypt(std::string cipherText, std::string priKey)
{
std::string strRet;
RSA* rsa = RSA_new();
BIO* keybio;
keybio = BIO_new_mem_buf((unsigned char*)priKey.c_str(), -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
size_t len = (size_t)RSA_size(rsa);
char* decryptedText = (char*)malloc(len + 1);
memset(decryptedText, 0, len + 1);
// 解密函数
int ret = RSA_private_decrypt((int32)cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= 0)
strRet.assign(decryptedText, ret);
// 释放内存
free(decryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
std::string rsa_pri_to_pkcs8(std::string priKey)
{
std::string strRet;
RSA* rsa = RSA_new();
BIO* keybio;
keybio = BIO_new_mem_buf((unsigned char*)priKey.c_str(), -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
BIO* out;
EVP_PKEY* evp_pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(evp_pkey, rsa);
if (!(out = BIO_new(BIO_s_mem())))
{
return "";
}
PEM_write_bio_PKCS8PrivateKey(out, evp_pkey,NULL,NULL,0,NULL,NULL);
size_t pub_len = BIO_pending(out);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(out, pub_key, (int32)pub_len);
strRet.append(pub_key, pub_len);
BIO_free_all(out);
free(pub_key);
EVP_PKEY_free(evp_pkey);
return strRet;
}
std::string rsa_pri_to_pkcs1(std::string priKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char*)priKey.c_str(), -1);
EVP_PKEY* evp_pkey = NULL;
PEM_read_bio_PrivateKey(keybio, &evp_pkey, NULL, NULL);
if (evp_pkey == NULL) {
return "";
}
RSA* rsa = (RSA*)EVP_PKEY_get0_RSA(evp_pkey);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
BIO* out;
if (!(out = BIO_new(BIO_s_mem())))
{
return "";
}
PEM_write_bio_RSAPrivateKey(out, rsa,NULL,NULL,0,NULL,NULL);
size_t pub_len = BIO_pending(out);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(out, pub_key, (int32)pub_len);
strRet.append(pub_key, pub_len);
BIO_free_all(out);
free(pub_key);
EVP_PKEY_free(evp_pkey);
return strRet;
}
std::string rsa_pub_to_pkcs8(std::string pubKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char*)pubKey.c_str(), -1);
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
BIO* out;
EVP_PKEY* evp_pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(evp_pkey, rsa);
if (!(out = BIO_new(BIO_s_mem())))
{
return "";
}
PEM_write_bio_PUBKEY(out, evp_pkey);
size_t pub_len = BIO_pending(out);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(out, pub_key, (int32)pub_len);
strRet.append(pub_key, pub_len);
BIO_free_all(out);
free(pub_key);
EVP_PKEY_free(evp_pkey);
return strRet;
}
std::string rsa_pub_to_pkcs1(std::string pubKey)
{
std::string strRet;
BIO* keybio = BIO_new_mem_buf((unsigned char*)pubKey.c_str(), -1);
EVP_PKEY* evp_pkey = NULL;
PEM_read_bio_PUBKEY(keybio, &evp_pkey, NULL, NULL);
if (evp_pkey == NULL) {
return "";
}
RSA* rsa = (RSA*)EVP_PKEY_get0_RSA(evp_pkey);
if (!rsa)
{
#ifdef _DEBUG
ERR_load_crypto_strings();
char errBuf[512];
ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
std::cout << "load key failed[" << errBuf << "]" << std::endl;
#endif
BIO_free_all(keybio);
return std::string("");
}
BIO* out;
if (!(out = BIO_new(BIO_s_mem())))
{
return "";
}
PEM_write_bio_RSAPublicKey(out, rsa);
size_t pub_len = BIO_pending(out);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(out, pub_key, (int32)pub_len);
strRet.append(pub_key, pub_len);
BIO_free_all(out);
free(pub_key);
EVP_PKEY_free(evp_pkey);
return strRet;
}
cpps::tuple generateRSAKey(C*c,object oKEY_LENGTH)
{
cpps_integer KEY_LENGTH = 1024;
if (oKEY_LENGTH.isint()) KEY_LENGTH = oKEY_LENGTH.toint();
// 公私密钥对
size_t pri_len;
size_t pub_len;
char* pri_key = NULL;
char* pub_key = NULL;
// 生成密钥对
RSA* keypair = RSA_new();
BIGNUM* bne = BN_new();
BN_set_word(bne, RSA_F4);
RSA_generate_key_ex(keypair,(int32)KEY_LENGTH, bne, NULL);
BIO* pri = BIO_new(BIO_s_mem());
BIO* pub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, keypair);
// 获取长度
pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);
// 密钥对读取到字符串
pri_key = (char*)malloc(pri_len + 1);
pub_key = (char*)malloc(pub_len + 1);
BIO_read(pri, pri_key, (int32)pri_len);
BIO_read(pub, pub_key, (int32)pub_len);
pri_key[pri_len] = '0円';
pub_key[pub_len] = '0円';
cpps::tuple ret(object::create(c, pri_key), object::create(c, pub_key));
// 内存释放
RSA_free(keypair);
BIO_free_all(pub);
BIO_free_all(pri);
BN_free(bne);
free(pri_key);
free(pub_key);
return ret;
}
std::string rsa_make_pub(std::string module, std::string exp)
{
std::string strRet;
BIGNUM* n = BN_bin2bn((const unsigned char* )module.c_str(),(int) module.size(), NULL);
BIGNUM* e = BN_bin2bn((const unsigned char* )exp.c_str(),(int)exp.size(), NULL);
if (n && e)
{
EVP_PKEY* pRsaKey = EVP_PKEY_new();
RSA* rsa = RSA_new();
RSA_set0_key(rsa, n, e, NULL);
EVP_PKEY_assign_RSA(pRsaKey, rsa);
BIO* out;
if (!(out = BIO_new(BIO_s_mem())))
{
return "";
}
PEM_write_bio_PUBKEY(out, pRsaKey);
size_t pub_len = BIO_pending(out);
char* pub_key = (char*)malloc(pub_len + 1);
BIO_read(out, pub_key, (int32)pub_len);
strRet.append(pub_key, pub_len);
BIO_free_all(out);
free(pub_key);
EVP_PKEY_free(pRsaKey);
}
else {
if (n) BN_free(n);
if (e) BN_free(e);
}
return strRet;
}
cpps_export_void cpps_attach(cpps::C* c)
{
cpps::cpps_init_cpps_class(c);
cpps::_module(c, "rsa")[
def("pri_decrypt", rsa_pri_decrypt),
def("pri_encrypt", rsa_pri_encrypt),
def("pub_encrypt", rsa_pub_encrypt),
def("pub_encrypt_no_padding", rsa_pub_encrypt_no_padding),
def("pub_decrypt", rsa_pub_decrypt),
def("pub_to_pkcs8", rsa_pub_to_pkcs8),
def("pri_to_pkcs8", rsa_pri_to_pkcs8),
def("pri_to_pkcs1", rsa_pri_to_pkcs1),
def("pub_to_pkcs1", rsa_pub_to_pkcs1),
def("rsa_make_pub", rsa_make_pub),
def_inside("generate_rsakey", generateRSAKey)
];
}
cpps_export_void cpps_detach(cpps::C * c)
{
cpps::_unmodule(c, "rsa");
}
cpps_export_finish
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

CPPS是一种轻量级的嵌入式脚本语言,其语法类似于C++。它具有当前主流语言的许多特性,包括协程、面向对象、lambda、闭包、泛型变量、自定义模块支持、GC垃圾收集和跨平台。CPPS将程序解释为字节码,通过内置语法解析在虚拟机中运行
取消

发行版 (3)

全部

开源评估指数源自 OSS-Compass 评估体系,评估体系围绕以下三个维度对项目展开评估:

1. 开源生态

  • 生产力:来评估开源项目输出软件制品和开源价值的能力。
  • 创新力:用于评估开源软件及其生态系统的多样化程度。
  • 稳健性:用于评估开源项目面对多变的发展环境,抵御内外干扰并自我恢复的能力。

2. 协作、人、软件

  • 协作:代表了开源开发行为中协作的程度和深度。
  • 人:观察开源项目核心人员在开源项目中的影响力,并通过第三方视角考察用户和开发者对开源项目的评价。
  • 软件:从开源项目对外输出的制品评估其价值最终落脚点。也是开源评估最"古老"的主流方向之一"开源软件" 的具体表现。

3. 评估模型

    基于"开源生态"与"协作、人、软件"的维度,找到与该目标直接或间接相关的可量化指标,对开源项目健康与生态进行量化评估,最终形成开源评估指数。

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /