-
Jan Kožusznik authoreda1901f53
KeyPairLoader.java 1.08 KiB
package java2.koz01.lab13;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
public class KeyPairLoader {
private KeyFactory kf;
public KeyPairLoader() throws NoSuchAlgorithmException {
kf = KeyFactory.getInstance("RSA");
}
public KeyPair load(String file) throws IOException, InvalidKeySpecException {
try (BufferedReader br = Files.newBufferedReader(Paths.get(file))) {
BigInteger modulus = new BigInteger(br.readLine());
BigInteger pubExp = new BigInteger(br.readLine());
BigInteger privExp = new BigInteger(br.readLine());
RSAPrivateKeySpec priv = new RSAPrivateKeySpec(modulus, privExp);
RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus, pubExp);
return new KeyPair(kf.generatePublic(pub), kf.generatePrivate(priv));
}
}
}