Java examples for Security:DSA
Transform a BigInteger RSA public key into a RSAPublicKey object
/*/*from w w w . j a v a2 s . c o m*/ * Copyright (c) 2014 Berner Fachhochschule, Switzerland. * Bern University of Applied Sciences, Engineering and Information Technology, * Research Institute for Security in the Information Society, E-Voting Group, * Biel, Switzerland. * * Project UniBoard. * * Distributable under GPL license. * See terms of license at gnu.org. */ import java.math.BigInteger; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAPublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.DSAPrivateKeySpec; import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; import java.util.logging.Level; import java.util.logging.Logger; public class Main{ private static final Logger logger = Logger.getLogger(PostHelper.class .getName()); /** * Transform a BigInteger RSA public key into a RSAPublicKey object * * @param modulus value of RSA modulus * @param e value of public key * @return the corresponding RSAPublicKey object, null if an error occured * @throws InvalidKeySpecException if values do not represent a valid public key */ public static RSAPublicKey createRSAPublicKey(BigInteger modulus, BigInteger e) throws InvalidKeySpecException { RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, e); KeyFactory keyFactory; try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException ex) { logger.log(Level.SEVERE, "Invalid key algorithm given.", ex); return null; } return (RSAPublicKey) keyFactory.generatePublic(spec); } }