Я пытаюсь определить, предлагает ли крипто -библиотека C# «Знак» и « подтвердить и восстановить », как это делают библиотеки OpenSSL C. Позвольте мне объяснить немного дальше. < /P>
библиотеки OpenSSL 3.4.1 C предлагают «знак» и «проверить и восстановить» операции, которые: < /p>
«Знак» - подписывает данные (AES Key и IV в моем случае) и включает подписанные данные в файле подписи. Эта операция выполняется с использованием закрытого ключа RSA. < /P>
< /li>
«Проверьте и восстанавливает» - проверяет подпись и восстанавливает подписание данных. Я могу подписать ключ AES и IV, затем проверить и восстановить их позже для использования. Поддерживает ли Crypto Crypto Library?/*******************************************************************************
* *
* Name - AesKeyIv::Sign *
* *
* Desc - This function signs the aes key and iv. The signature is written *
* to member variable 'sig', and can be accessed via member function *
* 'GetSig'. *
* *
* Inputs - signKey - key to be used in signing *
* *
* Outputs - none *
* *
* Returns - PASS or FAIL *
* *
* Change history: *
* *
*******************************************************************************/
Status AesKeyIv::Sign(EVP_PKEY* signKey) {
// validate inputs
assert(signKey != NULL);
// allocate public key algorithm context
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(signKey, NULL);
if (ctx == NULL) {
PROCESS_ERROR("Failed to allocate public key algorithm context.");
return FAIL;
}
// initialize ctx for signing
if (EVP_PKEY_sign_init(ctx) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to initialize ctx for signing.");
return FAIL;
}
// set padding type to rsa pkcs1
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to set padding type to rsa pkcs1.");
return FAIL;
}
size_t sigLen = 0; // signature length
unsigned char tmpBuf[KEY_LEN + IV_LEN]; // temporary buffer
// pack key and iv into temporary buffer
memcpy(tmpBuf, key, KEY_LEN);
memcpy(tmpBuf + KEY_LEN, iv, IV_LEN);
// get signature length
if (EVP_PKEY_sign(ctx, NULL, &sigLen, tmpBuf, sizeof(tmpBuf)) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to get signature length.");
return FAIL;
}
// verify signature length
if (sigLen != SIG_LEN) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Invalid signature length.");
return FAIL;
}
// get aes key and iv signature
if (EVP_PKEY_sign(ctx, sig, &sigLen, tmpBuf, sizeof(tmpBuf)) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to get aes key and iv signature.");
return FAIL;
}
// free public key algorithm context
EVP_PKEY_CTX_free(ctx);
// return pass status
return PASS;
}
/*******************************************************************************
* *
* Name - AesKeyIv::VerifyAndRecover *
* *
* Desc - This function verifies the aes key and iv signature stored in *
* member variable 'sig', and recovers the key and iv, which are *
* written to member variables 'key' and 'iv', respectively. They can *
* be accessed via member functions 'GetKey' and 'GetIv', *
* respectively. *
* *
* Inputs - verifyKey - key to be used in verifying *
* *
* Outputs - none *
* *
* Returns - PASS or FAIL *
* *
* Change history: *
* *
*******************************************************************************/
Status AesKeyIv::VerifyAndRecover(EVP_PKEY* verifyKey) {
// validate inputs
assert(verifyKey != NULL);
// allocate public key algorithm context
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(verifyKey, NULL);
if (ctx == NULL) {
PROCESS_ERROR("Failed to allocate public key algorithm context.");
return FAIL;
}
// initialize ctx for signing
if (EVP_PKEY_verify_recover_init(ctx) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to initialize ctx for signing.");
return FAIL;
}
// set padding type to rsa pkcs1
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to set padding type to rsa pkcs1.");
return FAIL;
}
size_t maxAesKeyIvLen = 0; // max aes key and iv length
// get max aes key and iv length (this is the max length, *not* the actual length,
// which will be determined in the next call to this function, further down below)
if (EVP_PKEY_verify_recover(ctx, NULL, &maxAesKeyIvLen, sig, SIG_LEN) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to get max aes key and iv length.");
return FAIL;
}
// verify max aes key and iv length
if (maxAesKeyIvLen != SIG_LEN) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Invalid max aes key and iv length.");
return FAIL;
}
unsigned char tmpBuf[KEY_LEN + IV_LEN]; // temporary buffer
size_t actualAesKeyIvLen = 0; // actual aes key and iv length
// verify and recover aes key and iv, and their actual (combined) length
if (EVP_PKEY_verify_recover(ctx, tmpBuf, &actualAesKeyIvLen, sig, SIG_LEN) != OPENSSL_PASS) {
EVP_PKEY_CTX_free(ctx);
PROCESS_ERROR("Failed to verify and recover aes key and iv.");
return FAIL;
}
// free pkey context
EVP_PKEY_CTX_free(ctx);
// verify actual aes key and iv length
if (actualAesKeyIvLen != (KEY_LEN + IV_LEN)) {
PROCESS_ERROR("Invalid actual aes key and iv length.");
return FAIL;
}
// set key and iv member variables
SetKey(tmpBuf);
SetIv(tmpBuf + KEY_LEN);
// return pass status
return PASS;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... ke-openssl
Предлагает ли крипто -библиотека C# опцию «Проверить и восстановить», такую как библиотека OpenSSL C? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как добавить опцию с помощью CLIC CLI и правильно передать опцию в функции?
Anonymous » » в форуме Python - 0 Ответы
- 7 Просмотры
-
Последнее сообщение Anonymous
-