-
Jan Kožusznik authored066602ac
KeyPairReader.java 1.23 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 KeyPairReader {
private KeyFactory factory;
public KeyPairReader() throws NoSuchAlgorithmException {
factory = KeyFactory.getInstance("rsa");
}
public KeyPair readKeys(String fileWithKeys) throws IOException,
InvalidKeySpecException
{
//open file
//Read first line-> modulus
//...
//create RSAPublicKeySpec
try (BufferedReader br = Files.newBufferedReader(Paths.get(fileWithKeys))) {
BigInteger modulus = new BigInteger(br.readLine());
BigInteger publicExponent = new BigInteger(br.readLine());
BigInteger privateExponent = new BigInteger(br.readLine());
RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus, publicExponent);
RSAPrivateKeySpec pri = new RSAPrivateKeySpec(modulus, privateExponent);
return new KeyPair(factory.generatePublic(pub), factory.generatePrivate(
pri));
}
}
}