Java examples for Security:DSA
Transform a BigInteger DSA private key into a DSAPrivateKey object
/*//from w w w . j ava2s. 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 DSA private key into a DSAPrivateKey object * * @param p value of prime p * @param q value of prime q * @param g value of generator g * @param x value of private key x * @return the corresponding DSAPrivateKey object, null if an error occured * @throws InvalidKeySpecException if values do not represent a valid private key */ public static DSAPrivateKey createDSAPrivateKey(BigInteger p, BigInteger q, BigInteger g, BigInteger x) throws InvalidKeySpecException { DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(x, p, q, g); KeyFactory keyFactory; try { keyFactory = KeyFactory.getInstance("DSA"); } catch (NoSuchAlgorithmException ex) { logger.log(Level.SEVERE, "Invalid key algorithm given.", ex); return null; } return (DSAPrivateKey) keyFactory.generatePrivate(keySpec); } }