-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.cpp
More file actions
110 lines (87 loc) · 3.38 KB
/
RSA.cpp
File metadata and controls
110 lines (87 loc) · 3.38 KB
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
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <iostream>
#include <string>
#include "RSA.hpp"
//コンパイラオプション
//g++ -o RSA.exe RSA.cpp -I"C:\Program Files\OpenSSL-Win64\include" -L"C:\Program Files\OpenSSL-Win64\lib\VC\x64\MTd" -lssl -lcrypto -Wno-deprecated-declarations
// キーペアの生成
RSA* RSAClass::createRSAKeyPair() {
int keyLength = 2048;
unsigned long e = RSA_F4; // 公開指数(通常はRSA_F4)
RSA* rsa = RSA_generate_key(keyLength, e, NULL, NULL);
if (rsa == NULL) {
std::cerr << "鍵の生成に失敗しました" << std::endl;
return NULL;
}
return rsa;
}
// 公開鍵をPEM形式で取得
std::string RSAClass::getPublicKey(RSA* rsa) {
BIO* bio = BIO_new(BIO_s_mem());
PEM_write_bio_RSA_PUBKEY(bio, rsa);
size_t pubKeyLen = BIO_pending(bio);
char* pubKey = new char[pubKeyLen + 1];
BIO_read(bio, pubKey, pubKeyLen);
pubKey[pubKeyLen] = '\0';
std::string publicKey(pubKey);
delete[] pubKey;
BIO_free_all(bio);
return publicKey;
}
// 秘密鍵をPEM形式で取得
std::string RSAClass::getPrivateKey(RSA* rsa) {
BIO* bio = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
size_t privKeyLen = BIO_pending(bio);
char* privKey = new char[privKeyLen + 1];
BIO_read(bio, privKey, privKeyLen);
privKey[privKeyLen] = '\0';
std::string privateKey(privKey);
delete[] privKey;
BIO_free_all(bio);
return privateKey;
}
// メッセージの暗号化
std::string RSAClass::encryptMessage(RSA* rsa, const std::string& message) {
size_t rsaLen = RSA_size(rsa);
unsigned char* encryptedMessage = new unsigned char[rsaLen];
int result = RSA_public_encrypt(message.length(),
reinterpret_cast<const unsigned char*>(message.c_str()),
encryptedMessage,
rsa,
RSA_PKCS1_PADDING);
if (result == -1) {
char* err = new char[130];
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
std::cerr << "暗号化に失敗しました " << err << std::endl;
delete[] err;
return "";
}
std::string encryptedString(reinterpret_cast<char*>(encryptedMessage), result);
delete[] encryptedMessage;
return encryptedString;
}
// メッセージの復号
std::string RSAClass::decryptMessage(RSA* rsa, const std::string& encryptedMessage) {
size_t rsaLen = RSA_size(rsa);
unsigned char* decryptedMessage = new unsigned char[rsaLen];
int result = RSA_private_decrypt(encryptedMessage.length(),
reinterpret_cast<const unsigned char*>(encryptedMessage.c_str()),
decryptedMessage,
rsa,
RSA_PKCS1_PADDING);
if (result == -1) {
char* err = new char[130];
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
std::cerr << "復号に失敗しました" << err << std::endl;
delete[] err;
return "";
}
std::string decryptedString(reinterpret_cast<char*>(decryptedMessage), result);
delete[] decryptedMessage;
return decryptedString;
}