Search in sources :

Example 61 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project Wurst-MC-1.12 by Wurst-Imperium.

the class Encryption method loadRsaKeys.

private KeyPair loadRsaKeys(Path publicFile, Path privateFile) throws GeneralSecurityException, ReflectiveOperationException, IOException {
    KeyFactory factory = KeyFactory.getInstance("RSA");
    // load public key
    PublicKey publicKey;
    try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(publicFile))) {
        publicKey = factory.generatePublic(new RSAPublicKeySpec((BigInteger) in.readObject(), (BigInteger) in.readObject()));
    }
    // load private key
    PrivateKey privateKey;
    try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(privateFile))) {
        privateKey = factory.generatePrivate(new RSAPrivateKeySpec((BigInteger) in.readObject(), (BigInteger) in.readObject()));
    }
    return new KeyPair(publicKey, privateKey);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) ObjectInputStream(java.io.ObjectInputStream)

Example 62 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project remusic by aa112901.

the class RSAUtils method getPrivateKey.

/**
 * 使用模和指数生成RSA私钥
 * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
 * /None/NoPadding】
 *
 * @param modulus  模
 * @param exponent 指数
 * @return
 */
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
    try {
        BigInteger b1 = new BigInteger(modulus);
        BigInteger b2 = new BigInteger(exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
        return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 63 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.

the class CertificateGenerator method generateSelfSigned.

/**
 * Generates a self-signed certificate. These are used to identify servers.
 *
 * @return A matched pair of public and private certificates.
 */
public CertificatePair generateSelfSigned() {
    keyPairGenerator.initialize(KEY_SIZE);
    KeyPair kp = keyPairGenerator.genKeyPair();
    try {
        RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
        RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
        String uuid = UUID.randomUUID().toString();
        signer.initSign(kp.getPrivate(), new SecureRandom());
        signer.update(uuid.getBytes(Charsets.UTF_8));
        signer.update(pub.getModulus().toByteArray());
        signer.update(pub.getPublicExponent().toByteArray());
        byte[] rawSig = signer.sign();
        BigInteger signature = new BigInteger(rawSig);
        PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
        PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
        return new CertificatePair(publicCert, privateCert);
    } catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) {
        throw new RuntimeException("Unexpected exception generating certificate", e);
    }
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 64 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.

the class CertificateGenerator method generate.

/**
 * Generates a certificate signed by the given signer - a server will typically generate client identity certificates
 * signed by its certificate.
 *
 * @param signingCertificate
 * @return A matched pair of public and private certificates.
 */
public CertificatePair generate(PrivateIdentityCertificate signingCertificate) {
    keyPairGenerator.initialize(KEY_SIZE);
    KeyPair kp = keyPairGenerator.genKeyPair();
    RSAPrivateKeySpec signingRSAKey = new RSAPrivateKeySpec(signingCertificate.getModulus(), signingCertificate.getExponent());
    try {
        PrivateKey signingKey = keyFactory.generatePrivate(signingRSAKey);
        RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
        RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
        String uuid = UUID.randomUUID().toString();
        signer.initSign(signingKey, new SecureRandom());
        signer.update(uuid.getBytes(Charsets.UTF_8));
        signer.update(pub.getModulus().toByteArray());
        signer.update(pub.getPublicExponent().toByteArray());
        byte[] rawSig = signer.sign();
        BigInteger signature = new BigInteger(rawSig);
        PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature);
        PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent());
        return new CertificatePair(publicCert, privateCert);
    } catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) {
        throw new RuntimeException("Unexpected exception generating certificate", e);
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) SecureRandom(java.security.SecureRandom) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 65 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project Terasology by MovingBlocks.

the class PrivateIdentityCertificate method decrypt.

/**
 * Decrypts data encrypted by the paired public certificate
 *
 * @param data
 * @return The decrypted data
 * @throws BadEncryptedDataException If the data could not be decrypted due to an error with the data.
 */
public byte[] decrypt(byte[] data) throws BadEncryptedDataException {
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
        PrivateKey key = keyFactory.generatePrivate(keySpec);
        Cipher cipher = Cipher.getInstance(IdentityConstants.CERTIFICATE_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Insufficient support for '" + IdentityConstants.CERTIFICATE_ALGORITHM + "', required for identity management", e);
    } catch (InvalidKeySpecException | InvalidKeyException e) {
        throw new RuntimeException("Unexpected error during encryption", e);
    } catch (BadPaddingException | IllegalBlockSizeException e) {
        throw new BadEncryptedDataException("Invalid encrypted data", e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)78 KeyFactory (java.security.KeyFactory)49 PrivateKey (java.security.PrivateKey)41 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)30 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)27 BigInteger (java.math.BigInteger)25 PublicKey (java.security.PublicKey)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 InvalidKeyException (java.security.InvalidKeyException)14 Cipher (javax.crypto.Cipher)14 RSAPublicKey (java.security.interfaces.RSAPublicKey)13 Signature (java.security.Signature)12 KeyPair (java.security.KeyPair)10 KeySpec (java.security.spec.KeySpec)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)9 IOException (java.io.IOException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 SignatureException (java.security.SignatureException)8 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 SecureRandom (java.security.SecureRandom)6