用java的RSA 公钥和私钥到远程文件,然后进行加解密
时间: 2024-02-17 17:02:33 浏览: 63
首先,您需要生成RSA公钥和私钥。可以使用Java中的`KeyPairGenerator`类来生成公钥和私钥。以下是一个示例代码:
```java
import java.security.*;
public class RSAKeyGenerator {
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("Private Key: " + privateKey);
System.out.println("Public Key: " + publicKey);
}
}
```
生成的公钥和私钥将打印在控制台上。
接下来,您需要将公钥和私钥保存到文件中。可以使用Java中的`KeyStore`类来保存公钥和私钥。以下是一个示例代码:
```java
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
public class RSAKeyStore {
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null, null);
keyStore.setKeyEntry("mykey", privateKey, "mypass".toCharArray(), new Certificate[] { });
keyStore.setCertificateEntry("mycert", new TestCertificate(publicKey));
FileOutputStream fos = new FileOutputStream("mykeystore.jks");
keyStore.store(fos, "mystorepass".toCharArray());
fos.close();
}
}
class TestCertificate extends Certificate {
private PublicKey publicKey;
public TestCertificate(PublicKey publicKey) {
super("X.509");
this.publicKey = publicKey;
}
@Override
public PublicKey getPublicKey() {
return publicKey;
}
@Override
public byte[] getEncoded() {
return new byte[0];
}
@Override
public void verify(PublicKey key) { }
@Override
public void verify(PublicKey key, String sigProvider) { }
@Override
public String toString() {
return "TestCertificate";
}
}
```
这个示例代码将生成一个Java密钥库,并将私钥和公钥保存为键值对。然后将密钥库保存到文件中。
现在,您可以使用公钥加密文件,然后使用私钥解密文件。以下是示例代码:
```java
import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.*;
public class RSAEncryption {
public static void main(String[] args) throws Exception {
// 读取密钥库文件
String keyStoreFile = "mykeystore.jks";
String keyStorePass = "mystorepass";
String keyAlias = "mykey";
String keyPass = "mypass";
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(keyStoreFile), keyStorePass.toCharArray());
// 获取私钥
PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, keyPass.toCharArray());
// 加密文件
String inputFile = "plaintext.txt";
String encryptedFile = "encrypted.txt";
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyStore.getCertificate("mycert").getPublicKey());
FileInputStream fis = new FileInputStream(inputFile);
FileOutputStream fos = new FileOutputStream(encryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
byte[] encrypted = cipher.update(buffer, 0, len);
if (encrypted != null) {
fos.write(encrypted);
}
}
byte[] encrypted = cipher.doFinal();
if (encrypted != null) {
fos.write(encrypted);
}
fis.close();
fos.flush();
fos.close();
// 解密文件
String decryptedFile = "decrypted.txt";
cipher.init(Cipher.DECRYPT_MODE, privateKey);
fis = new FileInputStream(encryptedFile);
fos = new FileOutputStream(decryptedFile);
buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
byte[] decrypted = cipher.update(buffer, 0, len);
if (decrypted != null) {
fos.write(decrypted);
}
}
byte[] decrypted = cipher.doFinal();
if (decrypted != null) {
fos.write(decrypted);
}
fis.close();
fos.flush();
fos.close();
}
}
```
这个示例代码将使用公钥加密`plaintext.txt`文件,并将加密后的文件保存为`encrypted.txt`。然后,使用私钥解密`encrypted.txt`文件,并将解密后的文件保存为`decrypted.txt`。
阅读全文