Skip to content

Commit

Permalink
RSA padding changed to PKCS1 OAEP for compatibility with other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
leventkaragol committed Jul 2, 2024
1 parent 0fa5136 commit b62becb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void encryptWithRSA()

void decryptWithRSA()
{
auto encryptedText = "RVq6qJqexZGmxQtnCEwgIJqj7rxcAdnlQUqQ+qwWzc0y/VlX/EJ+/HuuMPrFbvDcgJnfVDz7WE2pHcJWR0lLR++UWxzLT8PRqBCpI0fX7ymqsaPCn2w/Q826FplfzlIpt0LZdkrkaSa/V/HMcQOPJ4JONuWxNaUpggA0KKOHdkH8A2RcpSnc4NskwPt84b9TU/mEwHlvyIzpvSF3/AiaGbSH5wEIe5iniBBhE7bUlkadvGu6jv8wiGqSeD/fWzZM7gbwmlxqxSzKFIJOpNwxya5+h7mHNGMmmZVVfGunrm6P0l7Qfz/cGf7Rca2SOgFosBWCr7NpjjM/AaK4PYVZwg==";
auto encryptedText = "dHKwJR4tepg8XwuUo9Kr1hFN3BjMVo9AAxGMCFpLHjvRJRwUojnH0vexouobLlG4hfWo/c4/61Q31/RfayascjdDJHFtPL7TjQHsMO+tg/cMCqHhmvXoUi9lmZs576PP3jibOe5g+YnU6FuGnnkgY3oljFQG50wdUM0lOrsV+zcucqE8G6APpjuqrJo1lvfcIVt2q6l99UF6RijkhX1IMifBjZIC13p0qLaaYfvT2KTOPZ2UWqTrQa2kSfsVJCdXK0aYfyrspRbs2eY1H5SW+0GV1YLtVNDQVg4djfSRaXEX+Ym/WtonNsXQyxbYZmINDaTzutb1KYq8upAI67Ax2g==";

auto privateKey = "-----BEGIN PRIVATE KEY-----\n"
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDAgBJHSAjCLOC/\n"
Expand Down
12 changes: 11 additions & 1 deletion src/libcpp-crypto.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Easy-to-use, symmetric (AES-256) and asymmetric (RSA) encryption and also hash (SHA-256) library for C++ (17+)
version 1.3.0
version 1.3.1
https://github.com/leventkaragol/libcpp-crypto
If you encounter any issues, please submit a ticket at https://github.com/leventkaragol/libcpp-crypto/issues
Expand Down Expand Up @@ -614,6 +614,11 @@ namespace lklibs
throw CryptoException("Failed to initialize encryption operation");
}

if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) <= 0)
{
throw CryptoException("Failed to set RSA padding");
}

size_t outlen;

if (EVP_PKEY_encrypt(ctx.get(), nullptr, &outlen, reinterpret_cast<const unsigned char*>(plaintext.data()), plaintext.size()) <= 0)
Expand Down Expand Up @@ -671,6 +676,11 @@ namespace lklibs
throw CryptoException("Failed to initialize decryption operation");
}

if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) <= 0)
{
throw CryptoException("Failed to set RSA padding");
}

size_t outlen;

if (EVP_PKEY_decrypt(ctx.get(), nullptr, &outlen, reinterpret_cast<const unsigned char*>(encryptedText.data()), encryptedText.size()) <= 0)
Expand Down

0 comments on commit b62becb

Please sign in to comment.