# cipher **Repository Path**: alanuu/cipher ## Basic Information - **Project Name**: cipher - **Description**: 基于 Java 的 RSA 非对称加密工具,使用 JDK 原生 javax.crypto 和 java.security API 实现 RSA 密钥生成、私钥加密与公钥解密。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: test - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-20 - **Last Updated**: 2026-07-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Cipher 基于 Java 的 RSA 非对称加密工具,使用 JDK 原生 `javax.crypto` 和 `java.security` API 实现 RSA 密钥生成、私钥加密与公钥解密。 ## 功能 - **密钥生成** — 生成 512 位 RSA 密钥对,以 Base64 编码输出公钥和私钥 - **私钥加密** — 使用私钥对明文进行 RSA 加密,返回 Base64 编码的密文 - **公钥解密** — 使用公钥对 Base64 密文进行 RSA 解密,返回明文字符串 ## 技术栈 | 项目 | 说明 | | ------- | ----------------------------- | | 语言 | Java | | 构建工具 | Maven | | 依赖 | commons-codec 1.12(Base64 编解码) | ## 项目结构 ``` cipher/ ├── pom.xml └── src/main/java/com/wcf/ └── RSA.java ``` ## 快速开始 ### 环境要求 - JDK 8+ - Maven 3.x ### 编译 ```bash mvn compile ``` ### 运行 ```bash mvn exec:java -Dexec.mainClass="com.wcf.RSA" ``` 直接运行将生成一对 RSA 密钥并打印到控制台。 ## API 说明 ### `initKey()` 初始化 RSA 密钥对,生成 512 位密钥,公钥和私钥以 Base64 字符串存储在类静态字段中。 ### `priEncode(String src)` 使用私钥对明文 `src` 进行加密,返回 Base64 编码的密文字符串。 ### `pubDecode(String src)` 使用公钥对 Base64 编码的密文 `src` 进行解密,返回原始明文字符串。 ## 使用示例 ```java // 1. 生成密钥对 RSA.initKey(); // 2. 私钥加密 String cipherText = RSA.priEncode("Hello, RSA!"); // 3. 公钥解密 String plainText = RSA.pubDecode(cipherText); System.out.println(plainText); // Hello, RSA! ``` ## 注意事项 - 当前密钥长度为 **512 位**,仅适用于学习和测试,生产环境建议使用 **2048 位及以上** 的密钥长度 - 该实现使用"私钥加密、公钥解密"模式,适用于签名验证场景;如需保密通信,应使用"公钥加密、私钥解密" - 公钥和私钥存储在类静态变量中,多次调用 `initKey()` 会覆盖之前的密钥 ## 许可证 本项目仅供学习参考。