Example usage for org.bouncycastle.jcajce.provider.asymmetric.rsa BCRSAPublicKey getPublicExponent

List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.rsa BCRSAPublicKey getPublicExponent

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.asymmetric.rsa BCRSAPublicKey getPublicExponent.

Prototype

public BigInteger getPublicExponent() 

Source Link

Document

return the public exponent.

Usage

From source file:org.nfc.eclipse.ndef.signature.SignatureVerifier.java

License:Open Source License

public Boolean verify(CertificateFormat certificateFormat, byte[] certificateBytes, SignatureType signatureType,
        byte[] signatureBytes, byte[] coveredBytes) throws CertificateException, NoSuchProviderException {

    if (Security.getProvider("BC") == null) {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }//from w ww . j av a2s. c  om

    Certificate certificate = null;
    if (certificateFormat == CertificateFormat.X_509) {
        java.security.cert.CertificateFactory cf = java.security.cert.CertificateFactory.getInstance("X.509",
                "BC");

        certificate = cf.generateCertificate(new ByteArrayInputStream(certificateBytes));
    }

    if (signatureType == SignatureType.RSASSA_PKCS1_v1_5_WITH_SHA_1) {

        BCRSAPublicKey key = (BCRSAPublicKey) certificate.getPublicKey();

        RSAKeyParameters pubParameters = new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

        SHA1Digest digest = new SHA1Digest();

        RSADigestSigner rsaDigestSigner = new RSADigestSigner(digest);
        rsaDigestSigner.init(false, pubParameters);
        rsaDigestSigner.update(coveredBytes, 0, coveredBytes.length);

        return rsaDigestSigner.verifySignature(signatureBytes);
    } else if (signatureType == SignatureType.RSASSA_PSS_SHA_1) {
        BCRSAPublicKey key = (BCRSAPublicKey) certificate.getPublicKey();

        RSAKeyParameters pubParameters = new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

        AsymmetricBlockCipher rsaEngine = new RSABlindedEngine();
        rsaEngine.init(false, pubParameters);

        SHA1Digest digest = new SHA1Digest();

        PSSSigner signer = new PSSSigner(rsaEngine, digest, digest.getDigestSize());
        signer.init(true, pubParameters);
        signer.update(coveredBytes, 0, coveredBytes.length);

        return signer.verifySignature(signatureBytes);
    } else if (signatureType == SignatureType.ECDSA) {

        // http://en.wikipedia.org/wiki/Elliptic_Curve_DSA
        // http://stackoverflow.com/questions/11339788/tutorial-of-ecdsa-algorithm-to-sign-a-string
        // http://www.bouncycastle.org/wiki/display/JA1/Elliptic+Curve+Key+Pair+Generation+and+Key+Factories
        // http://java2s.com/Open-Source/Java/Security/Bouncy-Castle/org/bouncycastle/crypto/test/ECTest.java.htm

        /*
        BCRSAPublicKey key = (BCRSAPublicKey) certificate.getPublicKey();
                
          RSAKeyParameters pubParameters = new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());
                
           org.bouncycastle.crypto.signers.ECDSASigner signer = new org.bouncycastle.crypto.signers.ECDSASigner();
           signer.init(false, pubParameters);
                
          SHA1Digest digest = new SHA1Digest();
           digest.update(coveredBytes, 0, coveredBytes.length);
                
           return signer.verifySignature(signatureBytes);
           */
    } else if (signatureType == SignatureType.DSA) {

        ASN1InputStream aIn = new ASN1InputStream(signatureBytes);
        ASN1Primitive o;
        try {
            o = aIn.readObject();

            ASN1Sequence asn1Sequence = (ASN1Sequence) o;

            BigInteger r = DERInteger.getInstance(asn1Sequence.getObjectAt(0)).getValue();
            BigInteger s = DERInteger.getInstance(asn1Sequence.getObjectAt(1)).getValue();

            BCDSAPublicKey key = (BCDSAPublicKey) certificate.getPublicKey();

            // DSA Domain parameters
            DSAParams params = key.getParams();
            if (params == null) {
                return Boolean.FALSE;
            }

            DSAParameters parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());

            DSASigner signer = new DSASigner();
            signer.init(false, new DSAPublicKeyParameters(key.getY(), parameters));

            SHA1Digest digest = new SHA1Digest();
            digest.update(coveredBytes, 0, coveredBytes.length);
            byte[] message = new byte[digest.getDigestSize()];
            digest.doFinal(message, 0);

            return signer.verifySignature(message, r, s);
        } catch (IOException e) {
            return Boolean.FALSE;
        }
    }

    return null;

}

From source file:org.nfc.eclipse.ndef.signature.SignatureVerifier.java

License:Open Source License

public boolean verifyRSASSA_PKCS1_v1_5_WITH_SHA_1(X509Certificate certificate, byte[] signature,
        byte[] covered) {
    BCRSAPublicKey key = (BCRSAPublicKey) certificate.getPublicKey();

    RSAKeyParameters pubParameters = new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent());

    SHA1Digest digest = new SHA1Digest();

    RSADigestSigner rsaDigestSigner = new RSADigestSigner(digest);
    rsaDigestSigner.init(false, pubParameters);
    rsaDigestSigner.update(covered, 0, covered.length);

    return rsaDigestSigner.verifySignature(signature);
}

From source file:org.xdi.oxauth.model.crypto.Certificate.java

License:MIT License

public PublicKey getPublicKey() {
    PublicKey publicKey = null;//from w  w  w.ja v a  2  s  .c  o m

    if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCRSAPublicKey) {
        BCRSAPublicKey jcersaPublicKey = (BCRSAPublicKey) x509Certificate.getPublicKey();

        publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(), jcersaPublicKey.getPublicExponent());
    } else if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCECPublicKey) {
        BCECPublicKey jceecPublicKey = (BCECPublicKey) x509Certificate.getPublicKey();

        publicKey = new ECDSAPublicKey(signatureAlgorithm, jceecPublicKey.getQ().getX().toBigInteger(),
                jceecPublicKey.getQ().getY().toBigInteger());
    }

    return publicKey;
}

From source file:org.xdi.oxauth.model.crypto.Certificate.java

License:MIT License

public RSAPublicKey getRsaPublicKey() {
    RSAPublicKey rsaPublicKey = null;

    if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCRSAPublicKey) {
        BCRSAPublicKey publicKey = (BCRSAPublicKey) x509Certificate.getPublicKey();

        rsaPublicKey = new RSAPublicKey(publicKey.getModulus(), publicKey.getPublicExponent());
    }/*  w w  w .  j a v a 2 s . c o m*/

    return rsaPublicKey;
}

From source file:org.xdi.oxauth.model.crypto.signature.RSAKeyFactory.java

License:MIT License

@Deprecated
public RSAKeyFactory(SignatureAlgorithm signatureAlgorithm, String dnName)
        throws InvalidParameterException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException,
        InvalidKeyException, CertificateEncodingException {
    if (signatureAlgorithm == null) {
        throw new InvalidParameterException("The signature algorithm cannot be null");
    }/*  www.  j  a v  a2  s . c om*/

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(2048, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();

    BCRSAPrivateCrtKey jcersaPrivateCrtKey = (BCRSAPrivateCrtKey) keyPair.getPrivate();
    BCRSAPublicKey jcersaPublicKey = (BCRSAPublicKey) keyPair.getPublic();

    rsaPrivateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(),
            jcersaPrivateCrtKey.getPrivateExponent());

    rsaPublicKey = new RSAPublicKey(jcersaPublicKey.getModulus(), jcersaPublicKey.getPublicExponent());

    if (StringUtils.isNotBlank(dnName)) {
        // Create certificate
        GregorianCalendar startDate = new GregorianCalendar(); // time from which certificate is valid
        GregorianCalendar expiryDate = new GregorianCalendar(); // time after which certificate is not valid
        expiryDate.add(Calendar.YEAR, 1);
        BigInteger serialNumber = new BigInteger(1024, new Random()); // serial number for certificate

        X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
        X500Principal principal = new X500Principal(dnName);

        certGen.setSerialNumber(serialNumber);
        certGen.setIssuerDN(principal);
        certGen.setNotBefore(startDate.getTime());
        certGen.setNotAfter(expiryDate.getTime());
        certGen.setSubjectDN(principal); // note: same as issuer
        certGen.setPublicKey(keyPair.getPublic());
        certGen.setSignatureAlgorithm(signatureAlgorithm.getAlgorithm());

        X509Certificate x509Certificate = certGen.generate(jcersaPrivateCrtKey, "BC");
        certificate = new Certificate(signatureAlgorithm, x509Certificate);
    }
}