我正在嘗試在 C 程序中使用 RSA 加密數(shù)據(jù),在服務(wù)器上使用 POST 方法發(fā)送數(shù)據(jù),然后在 PHP 中解密。對于這兩個程序,我都使用 OpenSSL 來加密/解密數(shù)據(jù)。在 PHP 中解密期間出現(xiàn)錯誤“錯誤:0407109F:rsa 例程:RSA_padding_check_PKCS1_type_2:pkcs 解碼錯誤” ,并且openssl_private_decrypt函數(shù)沒有返回任何數(shù)據(jù)。這是我的 C 程序:RSA *createRSA(unsigned char *key, int public){ RSA *rsa = RSA_new(); BIO *keybio; keybio = BIO_new_mem_buf(key, -1); if (keybio == NULL) return 0; if (public) rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); else rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL); return rsa;}char *rsa_encode(char *json){ char *data; RSA *rsa; int encrypted_length = 0; char pub[] = "MY_PUBLIC_KEY"; //RSA public key of size 8192 rsa = createRSA((unsigned char *)pub, 1); if (!(data = malloc(RSA_size(rsa)))) exit(0); encrypted_length = RSA_public_encrypt((int)strlen(json), (unsigned char *)json, (unsigned char *)data, rsa, RSA_PKCS1_PADDING); RSA_free(rsa); if (encrypted_length > 0) return data; return NULL;}char *bin2hex(const unsigned char *key, int size){ char *hex; int i = 0; if (!(hex = malloc((size * 2 + 1) * sizeof(char)))) handleError(); while (i < size) { sprintf((char *)(hex + (i * 2)),"%02x", key[i]); i++; } hex[i * 2] = '\0'; return hex;}這是我的 PHP 程序:$hexa = $_POST["data"];$data = hexToStr($hexa);$privateKey = openssl_get_privatekey("file://private.pem");$decrypted = "";$ret = openssl_private_decrypt($data, $decrypted, $privateKey, OPENSSL_PKCS1_PADDING);echo openssl_error_string() . "\n";我不明白錯誤在哪里,對于兩個程序我都使用OPENSSL_PKCS1_PADDING那么為什么 OpenSSL 錯誤告訴我錯誤在我的填充上?
OpenSSL RSA 解密失敗
函數(shù)式編程
2023-06-24 18:10:55