排版若有错请看原文
原文
RSA
import sun.security.rsa.RSAPrivateCrtKeyImpl;
import sun.security.rsa.RSAPublicKeyImpl;
import javax.crypto.Cipher;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.*;
import java.util.Base64;
public class RSAUtil {
private static final String ALGORITHM = "RSA";
public static KeyPair genKeyPair(int keyLength) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(keyLength);
return keyPairGenerator.generateKeyPair();
}
public static void saveKey(Key key, String path) throws IOException {
Path keyPath = Paths.get(path);
Files.write(keyPath, Base64.getEncoder().encode(key.getEncoded()));
}
public static PublicKey readPublicKey(String path) throws Exception {
Path keyPath = Paths.get(path);
byte[] keyBytes = Files.readAllBytes(keyPath);
return new RSAPublicKeyImpl(Base64.getDecoder().decode(keyBytes));
}
public static PrivateKey readPrivateKey(String path) throws Exception {
Path keyPath = Paths.get(path);
byte[] keyBytes = Files.readAllBytes(keyPath);
return RSAPrivateCrtKeyImpl.newKey(Base64.getDecoder().decode(keyBytes));
}
public