RSA 密钥对生成器
在浏览器中即时生成 RSA 公钥与私钥对。支持 512–8192 位密钥长度,PEM 格式(PKCS#8 / X.509)。兼容 OpenSSL、SSH、JWT 与 SSL/TLS。无需登录,密钥不会离开浏览器。
Default: 2048 bits
PEM 密钥验证
粘贴公钥或私钥的 PEM,检查是否有效。
使用方法
关于此工具
当您需要用于开发、SSH、TLS或JWT实验的公钥和私钥时,可以使用此RSA密钥对生成器。日常测试可选择2048位密钥长度,如需更强的密钥对可选择4096位,然后导出适用于OpenSSL风格工作流的PEM格式(PKCS#8私钥和X.509公钥)。 生成过程完全在您的浏览器中运行。密钥在您的设备上创建,不会上传到Handy Dev Tools的服务器——即使是在非生产环境中使用,这一点在处理私密信息时也很重要。该工具免费使用,无需账号。 除非您遵循自己的安全密钥管理流程,否则请将生成的密钥视为开发用凭据。实际测试时建议使用2048位或更高长度,并注意不要将私钥提交到源代码管理或共享聊天中。
使用方法
1. When you open the page, a key pair is automatically generated with the default (2048 bits), and the public and private keys are displayed. 2. To change key length: Enter a value that is a multiple of 8 between 512 and 8192 bits in the key length field, then click "Generate". 3. To generate a new key pair with the same key length: Click the "Regenerate" button. 4. Copy keys: Click the copy button in each key's display area to copy to the clipboard. 5. Download keys: Click the download button in each key's display area to download as a PEM file (public key: public_key.pem, private key: private_key.pem).
选项
Key length: Must be between 512 and 8192 bits and a multiple of 8. Default is 2048 bits. Use the up/down buttons in the number input to increment/decrement by 8. Invalid values (out of range or not a multiple of 8) show an error message and disable the "Generate" and "Regenerate" buttons.
使用场景
• Generating key pairs for SSH connections (public key authentication setup) • Creating private keys for SSL/TLS certificates • Generating key pairs for JWT signing • Creating key pairs for encrypted communication • Temporary key pair generation for development and testing • Generating key pairs for code signing
代码示例
1// Generate key pair
2const keyPair = await window.crypto.subtle.generateKey(
3 {
4 name: 'RSA-OAEP',
5 modulusLength: 2048,
6 publicExponent: new Uint8Array([1, 0, 1]), // 65537
7 hash: 'SHA-256',
8 },
9 true, // extractable
10 ['encrypt', 'decrypt']
11);1from cryptography.hazmat.primitives.asymmetric import rsa
2from cryptography.hazmat.primitives import serialization
3
4# Generate key pair
5private_key = rsa.generate_private_key(
6 public_exponent=65537,
7 key_size=2048
8)
9public_key = private_key.public_key()
10
11# Export in PEM format
12private_pem = private_key.private_bytes(
13 encoding=serialization.Encoding.PEM,
14 format=serialization.PrivateFormat.PKCS8,
15 encryption_algorithm=serialization.NoEncryption()
16)
17public_pem = public_key.public_bytes(
18 encoding=serialization.Encoding.PEM,
19 format=serialization.PublicFormat.SubjectPublicKeyInfo
20)1# Generate 2048-bit RSA private key
2openssl genrsa -out private_key.pem 2048
3
4# Extract public key
5openssl rsa -in private_key.pem -pubout -out public_key.pem1# PEM 公钥 → OpenSSH 单行格式(authorized_keys)
2ssh-keygen -f public_key.pem -i -m PKCS8 > openssh_public.pub
3
4# 校验私钥
5openssl rsa -in private_key.pem -check -noout
6
7# 由私钥生成 CSR(非交互可加 -subj)
8openssl req -new -key private_key.pem -out csr.pem1$config = [
2 'private_key_bits' => 2048,
3 'private_key_type' => OPENSSL_KEYTYPE_RSA,
4];
5$key = openssl_pkey_new($config);
6openssl_pkey_export($key, $privatePem);
7$details = openssl_pkey_get_details($key);
8$publicPem = $details['key'];
9
10// openssl_pkey_export may emit PKCS#1 (BEGIN RSA PRIVATE KEY)
11// depending on OpenSSL/PHP. This tool exports PKCS#8 (BEGIN PRIVATE KEY).1// .env — store file paths, never paste PEM contents
2// JWT_ALGO=RS256
3// JWT_PRIVATE_KEY=file:///var/www/certs/private_key.pem
4// JWT_PUBLIC_KEY=file:///var/www/certs/public_key.pem
5
6// Laravel Passport
7// php artisan passport:keys
8// Or copy this tool's PEM to storage/oauth-private.key
9// and storage/oauth-public.keyHow it works
RSA key pair generation prioritizes the Web Crypto API. If Web Crypto API is unavailable or PEM conversion fails, it falls back to the node-forge library. Keys are PEM: private keys in PKCS#8 (-----BEGIN PRIVATE KEY-----), public keys as X.509 SubjectPublicKeyInfo (-----BEGIN PUBLIC KEY-----). Larger key lengths take longer to generate, but a 2048-bit key pair typically completes within 5 seconds. All processing is done in the browser; keys are never sent to any server.
隐私与数据处理
All key generation is done in the browser; generated public and private keys are never sent to any server. Keys exist only in browser memory and are not automatically saved. Private keys are sensitive — manage them carefully and clear them from memory (e.g., close the browser) when done. Do not paste private keys into FAQ answers, chat, or source code. For production, prefer generating keys in a secure, isolated environment when possible.
常见问题
- Q: 什么是RSA密钥对?
- A: RSA密钥对由一个公钥和与之匹配的私钥组成,用于加密、签名、SSH、TLS和JWT。公钥可以公开分享,而私钥必须保密。
- Q: 应该使用哪种密钥长度?
- A: 一般开发和测试请使用2048位。如需更强的安全性,建议使用4096位。除了旧系统实验外,请避免使用512位等非常小的密钥长度。
- Q: 我的密钥会被上传到服务器吗?
- A: 不会。生成过程在您的浏览器中运行。密钥不会发送到Handy Dev Tools的服务器。
- Q: 可以导出哪些格式?
- A: 输出适用于OpenSSL风格工作流的PEM格式(PKCS#8私钥 / X.509公钥)。可以复制或下载以供本地使用。
- Q: 这是免费的吗?
- A: 是的,无需注册。
- Q: 如何在 PHP 中生成相同的密钥对?
- A: 使用 openssl_pkey_new(),指定 OPENSSL_KEYTYPE_RSA 和 2048 等密钥长度,然后用 openssl_pkey_export() 导出私钥、用 openssl_pkey_get_details() 获取公钥。请参见本页的 PHP 示例。openssl_pkey_export() 可能因 OpenSSL/PHP 版本输出 PKCS#1(BEGIN RSA PRIVATE KEY),而本工具导出 PKCS#8(BEGIN PRIVATE KEY)。两者都被广泛接受。
- Q: 这些密钥可以直接用于 Laravel JWT 或 Passport 吗?
- A: 开发环境可以。本工具导出 PKCS#8 私钥和 X.509(SPKI)公钥,Laravel Passport 以及 RS256 JWT 库(如 tymon/jwt-auth)均可使用。将 JWT_PRIVATE_KEY / JWT_PUBLIC_KEY 指向 PEM 文件,或复制到 storage/oauth-private.key 与 storage/oauth-public.key。请勿提交私钥;除非有自己的密钥管理流程,否则将浏览器生成的密钥视为开发凭据。
