五岁小站 - 免费在线工具箱

加载中...

RSA 密钥生成

RSA 加解密测试

使用生成的密钥对进行加解密测试

RSA 密钥长度对比
密钥长度 安全性 生成速度 最大加密长度 推荐用途
1024 位 不安全 117 字节 仅用于测试,不建议生产使用
2048 位 安全 较快 245 字节 推荐,适合大多数场景
3072 位 很安全 较慢 373 字节 高安全要求场景
4096 位 最安全 501 字节 极高安全要求,如CA证书
代码示例
# 生成私钥 (PKCS#1 格式)
openssl genrsa -out private_key.pem 2048

# 从私钥提取公钥
openssl rsa -in private_key.pem -pubout -out public_key.pem

# 转换为 PKCS#8 格式
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in private_key.pem -out private_key_pkcs8.pem

# 使用公钥加密
openssl rsautl -encrypt -pubin -inkey public_key.pem -in plaintext.txt -out encrypted.bin

# 使用私钥解密
openssl rsautl -decrypt -inkey private_key.pem -in encrypted.bin -out decrypted.txt
import java.security.*;
import java.security.spec.*;
import java.util.Base64;
import javax.crypto.Cipher;

// 生成密钥对
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair();

// 获取公私钥
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

// 公钥加密
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(plaintext.getBytes("UTF-8"));
String encryptedBase64 = Base64.getEncoder().encodeToString(encrypted);

// 私钥解密
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedBase64));
String result = new String(decrypted, "UTF-8");
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.backends import default_backend
import base64

# 生成密钥对
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)
public_key = private_key.public_key()

# 导出 PEM 格式
private_pem = private_key.private_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PrivateFormat.PKCS8,
    encryption_algorithm=serialization.NoEncryption()
)

# 公钥加密
encrypted = public_key.encrypt(
    plaintext.encode(),
    padding.PKCS1v15()
)

# 私钥解密
decrypted = private_key.decrypt(encrypted, padding.PKCS1v15())

# pip install cryptography
package main

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
)

// 生成密钥对
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
publicKey := &privateKey.PublicKey

// 导出私钥 PEM
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyPEM := pem.EncodeToMemory(&pem.Block{
    Type:  "RSA PRIVATE KEY",
    Bytes: privateKeyBytes,
})

// 导出公钥 PEM
publicKeyBytes, _ := x509.MarshalPKIXPublicKey(publicKey)
publicKeyPEM := pem.EncodeToMemory(&pem.Block{
    Type:  "PUBLIC KEY",
    Bytes: publicKeyBytes,
})

// 加密
encrypted, _ := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(plaintext))

// 解密
decrypted, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
const crypto = require('crypto');

// 生成密钥对
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem'
    }
});

// 公钥加密
const encrypted = crypto.publicEncrypt(
    { key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING },
    Buffer.from(plaintext)
);

// 私钥解密
const decrypted = crypto.privateDecrypt(
    { key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING },
    encrypted
);

RSA 是什么?

RSA 是一种非对称加密算法,由 Ron Rivest、Adi Shamir 和 Leonard Adleman 于 1977 年发明。RSA 使用一对密钥:公钥和私钥。公钥可以公开分享,用于加密数据;私钥必须保密,用于解密数据。

RSA 的安全性基于大数分解的数学难题,目前被广泛应用于 HTTPS、SSH、数字签名、密钥交换等场景。

公钥和私钥的区别

🔓 公钥 (Public Key)
- 可以公开分享给任何人
- 用于加密数据(只有对应的私钥能解密)
- 用于验证数字签名

🔐 私钥 (Private Key)
- 必须严格保密,不能泄露
- 用于解密公钥加密的数据
- 用于创建数字签名

密钥格式说明

📄 PKCS#1
RSA 专用格式,私钥以 "-----BEGIN RSA PRIVATE KEY-----" 开头

📄 PKCS#8
通用格式,支持多种算法。私钥以 "-----BEGIN PRIVATE KEY-----" 开头,Java 默认使用此格式

安全说明

🛡️ 本地生成
所有密钥生成都在浏览器中完成,密钥不会上传到服务器

🔑 密钥保管
请妥善保管私钥,私钥丢失将无法解密数据。建议离线存储备份

⚠️ 密钥长度
2048 位是目前推荐的最小长度,1024 位已不安全。对于长期使用建议 4096 位