rsa 相关问题

RSA是一种通用的公钥算法,可用于加密和签名。它是Internet上使用的大多数安全协议的核心组件,包括SSL / TLS协议套件。有关IBM Rational Software Architect的问题,请使用rational-rsa标记。

Mbed-TLS:OAEP 的就地加密/解密似乎不起作用

当使用单独的缓冲区时,RSA 加密/解密在 Mbed-TLS 中工作,其中

回答 1 投票 0

RSA 加密-解密:BadPaddingException:数据必须以零开头

对于一个被问了很多次的问题,我很抱歉向你询问你的技能。 我有一个关于 RSA 加密的问题。 我已经检查过有关此问题的其他主题,但我没有找到任何有用的答案...

回答 2 投票 0

MbedTLS:mbedtls_rsa_rsaes_oaep_encrypt/decrypt 支持就地加密/解密吗? (应该可以,但是好像没用?)

C 程序中带有 RSA 的 IN MbedTls 在使用单独的缓冲区时可以工作(对于纯文本、密文和解密文本,即纯文本和解密文本的内容是相同的...

回答 1 投票 0

使用 PHP 加密和解密文件

我想从上传文件中解密扩展名为 .txt .docx .xlsx 和 .pdf 的文件。 加密.php 我想从上传文件中解密扩展名为 .txt .docx .xlsx 和 .pdf 的文件。 加密.php <?php include "koneksi.php"; function generateKeyPairFromFileName(){ $config = array( "default_md" => "sha512", "private_key_bits" => 512, "private_key_type" => OPENSSL_KEYTYPE_RSA, ); $keypair = openssl_pkey_new($config); if (!$keypair) { die("Failed to generate key pair"); } openssl_pkey_export($keypair, $privateKey, null, $config); $publicKey = openssl_pkey_get_details($keypair); $publicKey = $publicKey['key']; return array( 'publicKey' => $publicKey, 'privateKey' => $privateKey ); } function savePublicKeyToLocal($publicKey, $fileName) { $publicKeyPath = $fileName . '_public_key.pem'; // Ganti dengan lokasi penyimpanan kunci publik Anda file_put_contents($publicKeyPath, $publicKey); return $publicKeyPath; } // Fungsi untuk menyimpan kunci pribadi ke file lokal function savePrivateKeyToLocal($privateKey, $fileName) { $privateKeyPath = $fileName . '_private_key.pem'; // Ganti dengan lokasi penyimpanan kunci pribadi Anda file_put_contents($privateKeyPath, $privateKey); return $privateKeyPath; } // Fungsi untuk mengenkripsi file menggunakan AES function encryptFile($data, $key) { // Buat vektor inisialisasi (IV) acak $iv = openssl_random_pseudo_bytes(16); // Enkripsi data menggunakan AES-256-CBC dengan kunci dan IV yang diberikan $encryptedData = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); // Base64 encode hasil enkripsi $encryptedDataBase64 = base64_encode($encryptedData); // Simpan atau kirim data terenkripsi ke tempat penyimpanan atau penerima // echo "Data terenkripsi: " . $encryptedDataBase64; return $encryptedDataBase64; } if(isset($_POST['generate'])){ $uploadedFileName = $_FILES['file']['name']; $filetmp = $_FILES['file']['tmp_name']; // Membaca isi file $fileContent = file_get_contents($filetmp); $KeyPair = generateKeyPairFromFileName(); $publicKeyPath = savePublicKeyToLocal($KeyPair['publicKey'], $uploadedFileName); $publicKeyPath = savePrivateKeyToLocal($KeyPair['privateKey'], $uploadedFileName); $encryptedFileContent = encryptFile($fileContent, $KeyPair['publicKey']); // echo $encryptedFileContent; $query = "INSERT INTO upload (nama_file, filedata) VALUES ('$uploadedFileName', '$encryptedFileContent')"; if (mysqli_query($conn, $query)) { echo "File berhasil diunggah dan dienkripsi."; } else { echo "Gagal mengunggah dan menyimpan file ke database: " . mysqli_error($conn); } } ?> <form action="" method="post" enctype="multipart/form-data"> Pilih file untuk diunggah (PDF, DOCX, XLSX, TXT, maks 15 MB): <input type="file" name="file"> <button name="generate" type="submit">Generate</button> </form> 我将私钥和公钥保存在本地。上面的脚本用于在保存到数据库之前加密数据。 ecnrypt.php <?php error_reporting(E_ALL); ini_set('display_errors', 1); include "koneksi.php"; // Fungsi dekripsi function decryptFile($data, $key) { // Decode data terenkripsi dari base64 $encryptedData = base64_decode($data); // Ambil IV dari data terenkripsi $iv = openssl_random_pseudo_bytes(16); $decryptedData = openssl_decrypt($encryptedData, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); // Kembalikan data yang telah didekripsi return $decryptedData; } if(isset($_GET['nama_file'])){ // Nama file $fileName = $_GET['nama_file']; // Membaca kunci privat try { $privateKeyPath = $fileName . '_private_key.pem'; if (!file_exists($privateKeyPath)) { throw new Exception("File kunci privat tidak ditemukan: " . $privateKeyPath); } $privateKey = file_get_contents($privateKeyPath); } catch (Exception $e) { echo "Gagal membaca kunci privat: " . $e->getMessage(); exit; } // Mengambil data file terenkripsi dari database $query = "SELECT filedata FROM upload WHERE nama_file = '$fileName'"; $result = mysqli_query($conn, $query); if ($result && mysqli_num_rows($result) > 0) { $row = mysqli_fetch_assoc($result); // Dekripsi data $decryptedData = decryptFile($row['filedata'], $privateKey); // Tampilkan data yang telah didekripsi echo $decryptedData; } else { echo "File tidak ditemukan!"; } } 我要解密的文件是空的。我的意思是在解密的数据上返回空。有人有同样的问题吗? 期待包含解密数据的文件。 我检查了我的 PHP 脚本,它似乎有效地结合了 RSA 和 AES 加密来进行文件上传。 加密.php <?php error_reporting(E_ALL); ini_set('display_errors', 1); include "koneksi.php"; // Enkripsi if(isset($_POST['generate'])){ // Mendapatkan informasi file yang diunggah $uploadedFileName = $_FILES['file']['name']; $uploadedFileSize = $_FILES['file']['size']; $uploadedFileType = strtolower(pathinfo($uploadedFileName, PATHINFO_EXTENSION)); $sql = "SELECT nama_file FROM upload WHERE nama_file='$uploadedFileName'"; $query = mysqli_query($conn, $sql); $rows = mysqli_num_rows($query); if( $rows > 0) { return header('location:./home.php?message=error'); } // Batasi jenis file yang diperbolehkan dan ukuran file $allowedTypes = array('txt', 'docx', 'pdf', 'xlsx'); $maxFileSize = 15 * 1024 * 1024; // 15 MB dalam bytes if (in_array($uploadedFileType, $allowedTypes) && $uploadedFileSize <= $maxFileSize) { // Menghasilkan pasangan kunci RSA $rsaKey = openssl_pkey_new(array( 'private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA )); // Mendapatkan kunci privat openssl_pkey_export($rsaKey, $privateKey); // Mendapatkan kunci publik $publicKey = openssl_pkey_get_details($rsaKey)['key']; // Menyimpan kunci dengan awalan nama file yang diunggah $privateKeyFileName = pathinfo($uploadedFileName, PATHINFO_FILENAME) . "_private_key.pem"; $publicKeyFileName = pathinfo($uploadedFileName, PATHINFO_FILENAME) . "_public_key.pem"; // echo $privateKeyFileName; file_put_contents('./keys/'. $privateKeyFileName, $privateKey); file_put_contents('./keys/'. $publicKeyFileName, $publicKey); // Menghasilkan kunci AES acak $aesKey = openssl_random_pseudo_bytes(16); // Mengenkripsi kunci AES menggunakan kunci publik RSA openssl_public_encrypt($aesKey, $encAesKey, $publicKey); // Unggah file $uploadedFile = $_FILES['file']['tmp_name']; // Mengenkripsi data menggunakan AES $iv = openssl_random_pseudo_bytes(16); $ciphertext = openssl_encrypt(file_get_contents($uploadedFile), 'aes-128-cbc', $aesKey, 0, $iv); // Menyimpan data terenkripsi ke sebuah variabel $encryptedFileContent = $iv . $ciphertext; // Menyimpan kunci AES terenkripsi ke sebuah file dengan awalan nama file yang diunggah $encryptedAesKeyFileName = pathinfo($uploadedFileName, PATHINFO_FILENAME) . "_encrypted_aes_key.bin"; file_put_contents('./keys/'. $encryptedAesKeyFileName, $encAesKey); // Memasukkan data file terenkripsi ke dalam database $query = "INSERT INTO upload (nama_file, filedata, mimetype) VALUES ('$uploadedFileName', '$encryptedFileContent', '$uploadedFileType')"; if (mysqli_query($conn, $query)) { echo "File berhasil diunggah dan dienkripsi."; header('location:./home.php?message=success'); } else { echo "Gagal mengunggah dan menyimpan file ke database: " . mysqli_error($conn); } } else { echo "Jenis file yang diunggah tidak diperbolehkan atau ukuran file melebihi batas maksimum."; } } ?> 解密.php <?php error_reporting(E_ALL); ini_set('display_errors', 1); include "koneksi.php"; if(isset($_GET['dekripsi'])) { $id = $_GET['dekripsi']; $query = "SELECT nama_file, filedata FROM upload WHERE id_file='$id'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); $uploadedFileName = $row['nama_file']; // Load RSA private key $privateKeyFileName = pathinfo($uploadedFileName, PATHINFO_FILENAME) . "_private_key.pem"; $privateKey = file_get_contents('./keys/'. $privateKeyFileName); // Decrypt AES key using RSA private key $encryptedAesKeyFileName = pathinfo($uploadedFileName, PATHINFO_FILENAME) . "_encrypted_aes_key.bin"; $encAesKey = file_get_contents('./keys/'. $encryptedAesKeyFileName); openssl_private_decrypt($encAesKey, $decAesKey, $privateKey); // Load encrypted data from the database (Assuming you have a function to retrieve data from the database) $encryptedData = $row['filedata']; // Decrypt data using AES $iv = substr($encryptedData, 0, 16); $ciphertext = substr($encryptedData, 16); $plaintext = openssl_decrypt($ciphertext, 'aes-128-cbc', $decAesKey, 0, $iv); // Prepare file for download header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($uploadedFileName)); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . strlen($plaintext)); ob_clean(); flush(); echo $plaintext; // exit; } else { echo "File name not provided."; } ?>

回答 1 投票 0

在磁盘上保存和加载加密/RSA私钥

我正在使用 crypto/rsa,并试图找到一种正确保存和加载密钥的方法。是否有正确的方法从 rsa.PrivateKey 创建 [] 字节。如果是这样,有没有办法对 rsa 正确执行此操作。

回答 4 投票 0

c# 如何将 JWK 转换为 RsaSecurityKey

我正在尝试根据公钥验证签名,并且我使用 RSACryptoproviders 等实现了这一点。 我设置公钥(以PEM格式导入密钥,读取rsa参数,然后

回答 2 投票 0

使用 Ansible 自动化 RSA SecurID

我已经尝试自动化安装和配置 RSA Securid 一段时间了。这是我一直在使用的参考: https://www.turbogeek.co.uk/how-to-install-rsa-authenticatio...

回答 1 投票 0

Erlang - 导入 GPG 公钥

我正在尝试在 Erlang 中做一些与公钥相关的事情,它们要求我跟踪公钥。根据此页面,我应该能够使用 file:read_file/1 和

回答 2 投票 0

无法在 iOS 中使用 SecKeyCreateWithData 加载 RSA-PSS 公钥

我为 rsa-pss-sha512 签名算法生成的密钥存在问题:我无法使用 SecKeyCreateWithData() 将其加载到我的 iOS 代码中。 这是我创建它的方法: % openssl genpkey -

回答 1 投票 0

如何使用 openssl 生成 x509 RSA-PSS 证书,其预告片字段包含值 0xBC 满足 RFC8017

使用 Windows 版本的 openssl(3.2.1),我们希望生成具有以下条件的证书: 哈希方法:SHA-256 掩码生成功能:具有 SHA-256 的 MGF1 盐的长度:32字节 特拉...

回答 1 投票 0

使用 C 的 1024 位 RSA 算法

我正在做一个用C语言的小项目,它是一个RSA文本加密。 该代码可以工作,但问题是如果我选择一个大密钥,它就不起作用。 我认为问题是由于...

c rsa
回答 2 投票 0

如何使用crypto.subtle加密/解密正确解密RSA-OAEP加密字符串?

我尝试使用 RSA 密钥对来加密和解密文件,并出于测试目的使用字符串。 它似乎正在正确生成密钥对(尽管有一个轻微的危险信号是 h...

回答 1 投票 0

是否可以在 Linux 中通过一系列命令通过 SSH 从一个终端链接到另一个终端? [已关闭]

我正在尝试设置一个别名,以从本地盒子进入我们的主服务器,然后进入内部盒子。我正在设置 RSA 密钥来加快速度,但这真的很好......

回答 1 投票 0

Golang 的 RSA 签名与 Java 的不匹配

我正在尝试使用 PKCS #1v15 标准创建 RSA 签名。数据首先使用 MD5 进行哈希处理,签名算法为 SHA256。但由于某种原因,两个输出不同并且......

回答 1 投票 0

当 M^e mod(N) = 0 (C++) 时,RSA 加密就会被破坏。怎么解决?

目前正在尝试在 C++ 中实现 RSA 加密,但遇到了加密消息的问题。 如果我们取 p = 2、q = 7,则 N = 14 且 phi = 6。e 被迫为 5,d 可以是一个变体...

回答 1 投票 0

无法将私钥转换为字符串

我想将私钥转换为字符串,以便用户可以将其存储在硬状态中。 我尝试使用 Base64 转换私钥,但它给我错误,指出 privateKey.getEncoded() 为 nul...

回答 1 投票 0

使用 Java 在 BouncyCastle 中加载以前存储的密钥失败

我正在使用 Bouncy Castle 用 Java 生成 RSA 密钥。我将此密钥存储到 PKCS1 文件中,并希望将该文件加载到程序的另一部分回到密钥文件对象。 我的密钥存储是

回答 1 投票 0

带有 RS256 的 JWT 不接受私钥

我正在尝试使用“jsonwebtoken”通过 RS256 创建 JWT。我已经使用以下命令创建了密钥: ssh-keygen -t rsa -b 4096 -m PEM -f 私钥的输出看起来...

回答 1 投票 0

从java PublicKey获取模数和publicExponent

大家好,我得到的公钥如下 OpenSSLRSAPublicKey{模数=

回答 1 投票 0

无法让PHP代码像C#一样对数据进行签名

我有以下 C# 代码,运行良好。我需要在 php 中创建相同的函数,该函数将返回与 C# 完全相同的签名许可证。我正在使用 phpseclib v1.0.22 并用于...

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.