List of usage examples for org.bouncycastle.crypto.params AsymmetricKeyParameter isPrivate
public boolean isPrivate()
From source file:edu.wisc.doit.tcrypt.AbstractPublicKeyDecrypter.java
License:Apache License
/** * Create an encrypter and decrypter using the specified public and private keys * /*w ww. ja va 2 s. c om*/ * @param publicKeyParam public key * @param privateKeyParam private key */ public AbstractPublicKeyDecrypter(AsymmetricKeyParameter publicKeyParam, AsymmetricKeyParameter privateKeyParam) { super(publicKeyParam); if (!privateKeyParam.isPrivate()) { throw new IllegalArgumentException("Private key parameter must be private"); } this.privateKeyParam = privateKeyParam; }
From source file:org.cryptacular.adapter.Converter.java
License:Open Source License
/** * Produces a {@link PrivateKey} from a BC private key type. * * @param bcKey BC private key./*from w ww. j a v a2 s . c o m*/ * * @return JCE private key. */ public static PrivateKey convertPrivateKey(final AsymmetricKeyParameter bcKey) { if (!bcKey.isPrivate()) { throw new IllegalArgumentException("AsymmetricKeyParameter is not a private key: " + bcKey); } final PrivateKey key; if (bcKey instanceof DSAPrivateKeyParameters) { key = new WrappedDSAPrivateKey((DSAPrivateKeyParameters) bcKey); } else if (bcKey instanceof ECPrivateKeyParameters) { key = new WrappedECPrivateKey((ECPrivateKeyParameters) bcKey); } else if (bcKey instanceof RSAPrivateCrtKeyParameters) { key = new WrappedRSAPrivateCrtKey((RSAPrivateCrtKeyParameters) bcKey); } else { throw new IllegalArgumentException("Unsupported private key " + bcKey); } return key; }
From source file:org.cryptacular.adapter.Converter.java
License:Open Source License
/** * Produces a {@link PublicKey} from a BC public key type. * * @param bcKey BC public key./*w w w. ja va 2 s.c o m*/ * * @return JCE public key. */ public static PublicKey convertPublicKey(final AsymmetricKeyParameter bcKey) { if (bcKey.isPrivate()) { throw new IllegalArgumentException("AsymmetricKeyParameter is not a public key: " + bcKey); } final PublicKey key; if (bcKey instanceof DSAPublicKeyParameters) { key = new WrappedDSAPublicKey((DSAPublicKeyParameters) bcKey); } else if (bcKey instanceof ECPublicKeyParameters) { key = new WrappedECPublicKey((ECPublicKeyParameters) bcKey); } else if (bcKey instanceof RSAKeyParameters) { key = new WrappedRSAPublicKey((RSAKeyParameters) bcKey); } else { throw new IllegalArgumentException("Unsupported public key " + bcKey); } return key; }
From source file:org.restcomm.sbc.media.dtls.TlsUtils.java
License:Open Source License
static AsymmetricKeyParameter loadPrivateKeyResource(String resource) throws IOException { PemObject pem = loadPemResource(resource); if (LOGGER.isTraceEnabled()) { LOGGER.trace("load Private key =" + resource); LOGGER.trace("type =" + pem.getType()); }/*from w w w . ja va 2s .c o m*/ if (pem.getType().endsWith("RSA PRIVATE KEY")) { RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent()); return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(), rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(), rsa.getExponent2(), rsa.getCoefficient()); } if (pem.getType().endsWith("PRIVATE KEY")) { AsymmetricKeyParameter pKey = PrivateKeyFactory.createKey(pem.getContent()); if (LOGGER.isTraceEnabled()) { LOGGER.trace("key =" + pKey.isPrivate()); LOGGER.trace(pKey.toString()); } return pKey; } throw new IllegalArgumentException("'resource' doesn't specify a valid private key"); }
From source file:org.xipki.commons.security.pkcs12.DSAPlainDigestSigner.java
License:Open Source License
@Override public void init(final boolean forSigning, final CipherParameters parameters) { this.forSigning = forSigning; AsymmetricKeyParameter param; if (parameters instanceof ParametersWithRandom) { param = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters(); } else {/*from w w w .j ava2s . c om*/ param = (AsymmetricKeyParameter) parameters; } ParamUtil.requireNonNull("param", param); if (param instanceof ECPublicKeyParameters) { keyBitLen = ((ECPublicKeyParameters) param).getParameters().getCurve().getFieldSize(); } else if (param instanceof ECPrivateKeyParameters) { keyBitLen = ((ECPrivateKeyParameters) param).getParameters().getCurve().getFieldSize(); } else if (param instanceof DSAPublicKeyParameters) { keyBitLen = ((DSAPublicKeyParameters) param).getParameters().getQ().bitLength(); } else if (param instanceof DSAPrivateKeyParameters) { keyBitLen = ((DSAPrivateKeyParameters) param).getParameters().getQ().bitLength(); } else { throw new IllegalArgumentException("unknown parameters: " + param.getClass().getName()); } if (forSigning && !param.isPrivate()) { throw new IllegalArgumentException("Signing Requires Private Key."); } if (!forSigning && param.isPrivate()) { throw new IllegalArgumentException("Verification Requires Public Key."); } reset(); dsaSigner.init(forSigning, parameters); }
From source file:org.xipki.security.bcext.DSAPlainDigestSigner.java
License:Open Source License
public void init(boolean forSigning, CipherParameters parameters) { this.forSigning = forSigning; AsymmetricKeyParameter k; if (parameters instanceof ParametersWithRandom) { k = (AsymmetricKeyParameter) ((ParametersWithRandom) parameters).getParameters(); } else {//ww w . j ava 2 s. co m k = (AsymmetricKeyParameter) parameters; } ParamChecker.assertNotNull("k", k); if (k instanceof ECPublicKeyParameters) { keyBitLen = ((ECPublicKeyParameters) k).getParameters().getCurve().getFieldSize(); } else if (k instanceof ECPrivateKeyParameters) { keyBitLen = ((ECPrivateKeyParameters) k).getParameters().getCurve().getFieldSize(); } else if (k instanceof DSAPublicKeyParameters) { keyBitLen = ((DSAPublicKeyParameters) k).getParameters().getQ().bitLength(); } else if (k instanceof DSAPrivateKeyParameters) { keyBitLen = ((DSAPrivateKeyParameters) k).getParameters().getQ().bitLength(); } else { throw new IllegalArgumentException("unknown parameters: " + k.getClass().getName()); } if (forSigning && !k.isPrivate()) { throw new IllegalArgumentException("Signing Requires Private Key."); } if (!forSigning && k.isPrivate()) { throw new IllegalArgumentException("Verification Requires Public Key."); } reset(); dsaSigner.init(forSigning, parameters); }