C/C++ 中的简单 OpenSSL RSA 加密让我头疼

发布于 2024-08-31 15:24:54 字数 2208 浏览 3 评论 0原文

已解决:我很蠢。加密的第一个参数应该是 key.size() ,解密的第一个参数应该是 RSA_size(myKey) 。

原始问题 嘿伙计们,我在弄清楚如何做到这一点时遇到了一些麻烦。

基本上我只希望客户端和服务器能够相互发送加密消息。

这将是非常不安全的,因为我正在努力解决这一切,所以我不妨从底层开始。

到目前为止,我已经让所有密钥都工作了,但是加密/解密让我很痛苦。

首先我会说我正在使用 C++,但大多数函数都需要 C 字符串,因此无论我做什么都可能会导致问题。

请注意,在客户端,我收到以下有关解密的错误。

错误:04065072:rsa例程:RSA_EAY_PRIVATE_DECRYPT:填充检查失败

我不太明白填充是如何工作的,所以我不知道如何修复它。

这里的 Anywho 是每一侧的相关变量,后面是代码。

客户端:

RSA *myKey; // Loaded with private key
// The below will hold the decrypted message
unsigned char* decrypted = (unsigned char*) malloc(RSA_size(myKey));
/* The below holds the encrypted string received over the network. 
Originally held in a C-string but C strings never work for me and scare me 
so I put it in a C++ string */
string encrypted;

// The reinterpret_cast line was to get rid of an error message.
// Maybe the cause of one of my problems?
if(RSA_private_decrypt(sizeof(encrypted.c_str()), reinterpret_cast<const unsigned char*>(encrypted.c_str()), decrypted, myKey, RSA_PKCS1_OAEP_PADDING)==-1)
    {
        cout << "Private decryption failed" << endl;
        ERR_error_string(ERR_peek_last_error(), errBuf);
        printf("Error: %s\n", errBuf);
        free(decrypted);
        exit(1);
    }

服务器:

RSA *pkey; // Holds the client's public key
string key; // Holds a session key I want to encrypt and send
//The below will hold the encrypted message
unsigned char *encrypted = (unsigned char*)malloc(RSA_size(pkey));

// The reinterpret_cast line was to get rid of an error message.
// Maybe the cause of one of my problems?
if(RSA_public_encrypt(sizeof(key.c_str()), reinterpret_cast<const unsigned char*>(key.c_str()), encrypted, pkey, RSA_PKCS1_OAEP_PADDING)==-1)
        {
            cout << "Public encryption failed" << endl;
            ERR_error_string(ERR_peek_last_error(), errBuf);
            printf("Error: %s\n", errBuf);
            free(encrypted);
            exit(1);
        }

让我再次声明,以防万一我之前没有声明过,我知道我的代码很糟糕,但我只是想建立一个框架来理解这一点。

如果这冒犯了各位资深程序员,我很抱歉。

预先感谢你们提供的任何帮助!

SOLVED: I was dumb. First argument of encrypt should have been key.size() and first argument of decrypt should have been RSA_size(myKey).

ORIGINAL QUESTION
Hey guys, I'm having some trouble figuring out how to do this.

Basically I just want a client and server to be able to send each other encrypted messages.

This is going to be incredibly insecure because I'm trying to figure this all out so I might as well start at the ground floor.

So far I've got all the keys working but encryption/decryption is giving me hell.

I'll start by saying I am using C++ but most of these functions require C strings so whatever I'm doing may be causing problems.

Note that on the client side I receive the following error in regards to decryption.

error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed

I don't really understand how padding works so I don't know how to fix it.

Anywho here are the relevant variables on each side followed by the code.

Client:

RSA *myKey; // Loaded with private key
// The below will hold the decrypted message
unsigned char* decrypted = (unsigned char*) malloc(RSA_size(myKey));
/* The below holds the encrypted string received over the network. 
Originally held in a C-string but C strings never work for me and scare me 
so I put it in a C++ string */
string encrypted;

// The reinterpret_cast line was to get rid of an error message.
// Maybe the cause of one of my problems?
if(RSA_private_decrypt(sizeof(encrypted.c_str()), reinterpret_cast<const unsigned char*>(encrypted.c_str()), decrypted, myKey, RSA_PKCS1_OAEP_PADDING)==-1)
    {
        cout << "Private decryption failed" << endl;
        ERR_error_string(ERR_peek_last_error(), errBuf);
        printf("Error: %s\n", errBuf);
        free(decrypted);
        exit(1);
    }

Server:

RSA *pkey; // Holds the client's public key
string key; // Holds a session key I want to encrypt and send
//The below will hold the encrypted message
unsigned char *encrypted = (unsigned char*)malloc(RSA_size(pkey));

// The reinterpret_cast line was to get rid of an error message.
// Maybe the cause of one of my problems?
if(RSA_public_encrypt(sizeof(key.c_str()), reinterpret_cast<const unsigned char*>(key.c_str()), encrypted, pkey, RSA_PKCS1_OAEP_PADDING)==-1)
        {
            cout << "Public encryption failed" << endl;
            ERR_error_string(ERR_peek_last_error(), errBuf);
            printf("Error: %s\n", errBuf);
            free(encrypted);
            exit(1);
        }

Let me once again state, in case I didn't before, that I know my code sucks but I'm just trying to establish a framework for understanding this.

I'm sorry if this offends you veteran coders.

Thanks in advance for any help you guys can provide!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2024-09-07 15:24:54

也许不是唯一的问题,但是:RAS_xxxcrypt 函数的第一个参数是缓冲区的字节数。 sizeof(key.c_str()) 不会产生 key 中的字节数,它会产生 key.c_str() 结果类型的大小,即sizeof(const char*)。您可能想传递字符串中的字符数,这可以通过 size() 成员函数获得。

Maybe not the only problem but: The first argument to RAS_xxxcrypt functions is the number of bytes of the buffers. sizeof(key.c_str()) does not yield the number of bytes in key, it yields the size of the type of key.c_str()'s result type, i.e. sizeof(const char*). You probably want to pass the number of chars in the string instead, which can be obtained with the size() member function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文