rsa java 加解密文件代码
时间: 2023-08-01 14:14:11 浏览: 121
下面是一个简单的RSA加解密文件的Java代码示例:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class RSADemo {
private static final int KEY_SIZE = 2048; // 密钥长度
private static final String PUBLIC_KEY_FILE = "rsa_public_key.pem"; // 公钥文件路径
private static final String PRIVATE_KEY_FILE = "rsa_private_key.pem"; // 私钥文件路径
public static void main(String[] args) throws Exception {
// 生成公私钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
// 保存公私钥到文件
saveKeyToFile(PUBLIC_KEY_FILE, publicKey.getEncoded());
saveKeyToFile(PRIVATE_KEY_FILE, privateKey.getEncoded());
// 读取公私钥
PublicKey publicKeyFromFile = (PublicKey) readKeyFromFile(PUBLIC_KEY_FILE, "RSA", true);
PrivateKey privateKeyFromFile = (PrivateKey) readKeyFromFile(PRIVATE_KEY_FILE, "RSA", false);
// 加密文件
String plaintextFile = "plaintext.txt";
String ciphertextFile = "ciphertext.txt";
encryptFile(plaintextFile, ciphertextFile, publicKeyFromFile);
// 解密文件
String decryptedFile = "decrypted.txt";
decryptFile(ciphertextFile, decryptedFile, privateKeyFromFile);
}
/** 加密文件 */
private static void encryptFile(String plaintextFile, String ciphertextFile, Key publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] buffer = new byte[(KEY_SIZE / 8) - 11]; // 计算分块大小,需要减去11
int len;
FileInputStream fis = new FileInputStream(new File(plaintextFile));
FileOutputStream fos = new FileOutputStream(new File(ciphertextFile));
while ((len = fis.read(buffer)) != -1) {
byte[] encrypted = cipher.doFinal(buffer, 0, len);
fos.write(encrypted);
}
fis.close();
fos.close();
}
/** 解密文件 */
private static void decryptFile(String ciphertextFile, String decryptedFile, Key privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] buffer = new byte[KEY_SIZE / 8]; // 解密时不需要分块
int len;
FileInputStream fis = new FileInputStream(new File(ciphertextFile));
FileOutputStream fos = new FileOutputStream(new File(decryptedFile));
while ((len = fis.read(buffer)) != -1) {
byte[] decrypted = cipher.doFinal(buffer, 0, len);
fos.write(decrypted);
}
fis.close();
fos.close();
}
/** 将公私钥保存到文件 */
private static void saveKeyToFile(String fileName, byte[] keyBytes) throws Exception {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(keyBytes);
fos.close();
}
/** 从文件中读取公私钥 */
private static Key readKeyFromFile(String fileName, String algorithm, boolean isPublicKey) throws Exception {
byte[] keyBytes = new byte[KEY_SIZE / 8];
FileInputStream fis = new FileInputStream(fileName);
fis.read(keyBytes);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
if (isPublicKey) {
return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes));
} else {
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
}
}
}
```
需要注意的几点:
- 生成密钥对时使用的算法是"RSA",密钥长度为2048位。
- 加解密时使用的算法是"RSA/ECB/PKCS1Padding",即RSA加密模式为ECB,填充方式为PKCS1Padding。
- 加密时需要分块,每次加密的数据量应该小于密钥长度减去11(即2048位密钥每块加密长度为245字节),解密时不需要分块,直接解密即可。
- 密钥保存到文件时可以使用Base64编码,这样方便查看和传输,但是在实际加解密时需要先解码再使用。
阅读全文