Example usage for java.security.interfaces RSAPublicKey getModulus

List of usage examples for java.security.interfaces RSAPublicKey getModulus

Introduction

In this page you can find the example usage for java.security.interfaces RSAPublicKey getModulus.

Prototype

public BigInteger getModulus();

Source Link

Document

Returns the modulus.

Usage

From source file:org.excalibur.core.util.SecurityUtils2.java

public static String getFingerPrint(RSAPublicKey key) throws IOException {
    try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
        write(key.getPublicExponent().toByteArray(), buf);
        write(key.getModulus().toByteArray(), buf);

        return getFingerPrint(buf.toByteArray());
    }/*w w  w . ja v a 2  s  .c o m*/
}

From source file:org.xdi.oxauth.cert.fingerprint.FingerprintHelper.java

private static byte[] getDerEncoding(RSAPublicKey key) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    DataOutputStream dataOutput = new DataOutputStream(buffer);
    writeDataWithLength("ssh-rsa".getBytes(), dataOutput);
    writeDataWithLength(key.getPublicExponent().toByteArray(), dataOutput);
    writeDataWithLength(key.getModulus().toByteArray(), dataOutput);

    return buffer.toByteArray();
}

From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java

/**
 * Returns the digest value of a given public key.
 * //from   ww w. j ava  2 s. c om
 * 
 * <p>In Version H003? of the EBICS protocol the ES of the financial:
 * 
 * <p>The SHA-256 hash values of the financial institution's public keys for X002 and E002 are
 * composed by concatenating the exponent with a blank character and the modulus in hexadecimal
 * representation (using lower case letters) without leading zero (as to the hexadecimal
 * representation). The resulting string has to be converted into a byte array based on US ASCII
 * code.
 * 
 * @param publicKey the public key
 * @return the digest value
 * @throws EbicsException
 */
public static byte[] getKeyDigest(RSAPublicKey publicKey) throws AxelorException {
    String modulus;
    String exponent;
    String hash;
    byte[] digest;

    exponent = Hex.encodeHexString(publicKey.getPublicExponent().toByteArray());
    modulus = Hex.encodeHexString(removeFirstByte(publicKey.getModulus().toByteArray()));
    hash = exponent + " " + modulus;

    if (hash.charAt(0) == '0') {
        hash = hash.substring(1);
    }

    try {
        digest = MessageDigest.getInstance("SHA-256", "BC").digest(hash.getBytes("US-ASCII"));
    } catch (GeneralSecurityException e) {
        throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR);
    } catch (UnsupportedEncodingException e) {
        throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR);
    }

    return new String(Hex.encodeHex(digest, false)).getBytes();
}

From source file:org.kopi.ebics.certificate.KeyUtil.java

/**
 * Returns the digest value of a given public key.
 * /*from  w w  w.jav a  2  s .  co  m*/
 * 
 * <p>In Version H003? of the EBICS protocol the ES of the financial:
 * 
 * <p>The SHA-256 hash values of the financial institution's public keys for X002 and E002 are
 * composed by concatenating the exponent with a blank character and the modulus in hexadecimal
 * representation (using lower case letters) without leading zero (as to the hexadecimal
 * representation). The resulting string has to be converted into a byte array based on US ASCII
 * code.
 * 
 * @param publicKey the public key
 * @return the digest value
 * @throws EbicsException
 */
public static byte[] getKeyDigest(RSAPublicKey publicKey) throws EbicsException {
    String modulus;
    String exponent;
    String hash;
    byte[] digest;

    exponent = Hex.encodeHexString(publicKey.getPublicExponent().toByteArray());
    modulus = Hex.encodeHexString(removeFirstByte(publicKey.getModulus().toByteArray()));
    hash = exponent + " " + modulus;

    if (hash.charAt(0) == '0') {
        hash = hash.substring(1);
    }

    try {
        digest = MessageDigest.getInstance("SHA-256", "BC").digest(hash.getBytes("US-ASCII"));
    } catch (GeneralSecurityException e) {
        throw new EbicsException(e.getMessage());
    } catch (UnsupportedEncodingException e) {
        throw new EbicsException(e.getMessage());
    }

    return new String(Hex.encodeHex(digest, false)).getBytes();
}

From source file:org.excalibur.core.util.SecurityUtils2.java

public static byte[] encode(RSAPublicKey key) throws IOException {
    try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
        write(PUBLIC_KEY_SSH_RSA_NAME, buf);
        write(key.getPublicExponent().toByteArray(), buf);
        write(key.getModulus().toByteArray(), buf);

        return buf.toByteArray();
    }/*  w ww  .j  a v a2 s .  c om*/
}

From source file:VerifyDescriptors.java

private static String determineKeyHash(String key) throws Exception {
    PEMReader pemReader = new PEMReader(new StringReader(key));
    RSAPublicKey dirIdentityKey = (RSAPublicKey) pemReader.readObject();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new ASN1OutputStream(baos)
            .writeObject(new org.bouncycastle.asn1.pkcs.RSAPublicKey(dirIdentityKey.getModulus(),
                    dirIdentityKey.getPublicExponent()).toASN1Primitive());
    byte[] pkcs = baos.toByteArray();
    byte[] dirIdentityKeyHashBytes = new byte[20];
    SHA1Digest sha1 = new SHA1Digest();
    sha1.update(pkcs, 0, pkcs.length);/* w w  w  . j  ava 2  s.  c o m*/
    sha1.doFinal(dirIdentityKeyHashBytes, 0);
    String keyHash = Hex.encodeHexString(dirIdentityKeyHashBytes);
    return keyHash;
}

From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java

/**
 * Validates RSA public and private key.
 *
 * @param keyPair the keypair//ww w .  ja v a 2 s  . co  m
 * @return true if keys matches
 */
public static boolean validateKeyPair(KeyPair keyPair) {
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    if (publicKey.getModulus().bitLength() != privateKey.getModulus().bitLength()) {
        LOG.error("Keypair length matching error");
        return false;
    }

    byte[] rawPayload = new byte[64];
    new Random().nextBytes(rawPayload);

    MessageEncoderDecoder encDec = new MessageEncoderDecoder(privateKey, publicKey);
    byte[] encodedPayload;
    byte[] decodedPayload;
    try {
        encodedPayload = encDec.encodeData(rawPayload);
        decodedPayload = encDec.decodeData(encodedPayload);
    } catch (GeneralSecurityException ex) {
        LOG.error("Validation keypair error ", ex);
        return false;
    }
    return Arrays.equals(rawPayload, decodedPayload);
}

From source file:de.pawlidi.openaletheia.generator.KeyGenerator.java

public static boolean flushPublicKeySpec(final String directory, RSAPublicKey publicKey) {
    if (StringUtils.isBlank(directory) || !new File(directory).isDirectory() || publicKey == null) {
        return false;
    }/*  w  ww  .jav  a 2 s. c  om*/
    return writeKeySpec(new File(directory, PUBLIC_KEYSPEC_FILE), publicKey.getModulus(),
            publicKey.getPublicExponent());

}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * RSA???/*from   w  ww  .j a  v  a  2  s.  co m*/
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGO_KEY);
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???/*  w  ww.j  ava2s  .  c o  m*/
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}