Search in sources :

Example 1 with RSAEngine

use of org.bouncycastle.crypto.engines.RSAEngine in project faf-java-server by FAForever.

the class UniqueIdServiceTest method rsaEncrypt.

private static byte[] rsaEncrypt(byte[] encryptedData, CipherParameters privateKey) throws IOException, InvalidCipherTextException {
    AsymmetricBlockCipher engine = new PKCS1Encoding(new RSAEngine());
    engine.init(true, privateKey);
    return engine.processBlock(encryptedData, 0, encryptedData.length);
}
Also used : PKCS1Encoding(org.bouncycastle.crypto.encodings.PKCS1Encoding) RSAEngine(org.bouncycastle.crypto.engines.RSAEngine) AsymmetricBlockCipher(org.bouncycastle.crypto.AsymmetricBlockCipher)

Example 2 with RSAEngine

use of org.bouncycastle.crypto.engines.RSAEngine in project cloudbreak by hortonworks.

the class PkiUtil method generateSignature.

public static String generateSignature(String privateKeyPem, byte[] data) {
    RSAKeyParameters rsaKeyParameters = CACHE.get(privateKeyPem);
    if (rsaKeyParameters == null) {
        try (PEMParser pEMParser = new PEMParser(new StringReader(clarifyPemKey(privateKeyPem)))) {
            PEMKeyPair pemKeyPair = (PEMKeyPair) pEMParser.readObject();
            KeyFactory factory = KeyFactory.getInstance("RSA");
            KeySpec publicKeySpec = new X509EncodedKeySpec(pemKeyPair.getPublicKeyInfo().getEncoded());
            PublicKey publicKey = factory.generatePublic(publicKeySpec);
            KeySpec privateKeySpec = new PKCS8EncodedKeySpec(pemKeyPair.getPrivateKeyInfo().getEncoded());
            PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
            KeyPair kp = new KeyPair(publicKey, privateKey);
            RSAPrivateKeySpec privKeySpec = factory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
            rsaKeyParameters = new RSAKeyParameters(true, privKeySpec.getModulus(), privKeySpec.getPrivateExponent());
            CACHE.put(privateKeyPem, rsaKeyParameters);
        } catch (NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
            throw new SecurityException(e);
        }
    }
    Signer signer = new PSSSigner(new RSAEngine(), new SHA256Digest(), SALT_LENGTH);
    signer.init(true, rsaKeyParameters);
    signer.update(data, 0, data.length);
    try {
        byte[] signature = signer.generateSignature();
        return BaseEncoding.base64().encode(signature);
    } catch (CryptoException e) {
        throw new SecurityException(e);
    }
}
Also used : KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) PrivateKey(java.security.PrivateKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) Signer(org.bouncycastle.crypto.Signer) ContentSigner(org.bouncycastle.operator.ContentSigner) PSSSigner(org.bouncycastle.crypto.signers.PSSSigner) PEMParser(org.bouncycastle.openssl.PEMParser) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PSSSigner(org.bouncycastle.crypto.signers.PSSSigner) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CryptoException(org.bouncycastle.crypto.CryptoException) RSAEngine(org.bouncycastle.crypto.engines.RSAEngine) PrivateKeyFactory(org.bouncycastle.crypto.util.PrivateKeyFactory) KeyFactory(java.security.KeyFactory)

Example 3 with RSAEngine

use of org.bouncycastle.crypto.engines.RSAEngine in project 360-Engine-for-Android by 360.

the class RSAEncryptionUtils method rsa.

/**
 * Encrypts or Decrypts bytes with the given RSA Public or Private Key.
 *
 * @param encrypt true for encrypt, false for decrypt.
 * @param key the RSA Public or Private Key.
 * @param data the data to encrypt or decrypt.
 * @return the encrypted or decrypted data.
 */
private static byte[] rsa(final boolean encrypt, final RSAKeyParameters key, final byte[] data) throws InvalidCipherTextException {
    final byte[] dataAligned = new byte[roundUp(data.length, ROUND_UP_VALUE)];
    System.arraycopy(data, 0, dataAligned, 0, data.length);
    final RSAEngine rsa = new RSAEngine();
    final AsymmetricBlockCipher pkcs1 = new PKCS1Encoding(rsa);
    pkcs1.init(encrypt, key);
    if (encrypt)
        return pkcs1.processBlock(dataAligned, 0, dataAligned.length);
    return trimZeros(pkcs1.processBlock(dataAligned, 0, dataAligned.length));
}
Also used : PKCS1Encoding(org.bouncycastle.crypto.encodings.PKCS1Encoding) RSAEngine(org.bouncycastle.crypto.engines.RSAEngine) AsymmetricBlockCipher(org.bouncycastle.crypto.AsymmetricBlockCipher)

Aggregations

RSAEngine (org.bouncycastle.crypto.engines.RSAEngine)3 AsymmetricBlockCipher (org.bouncycastle.crypto.AsymmetricBlockCipher)2 PKCS1Encoding (org.bouncycastle.crypto.encodings.PKCS1Encoding)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 KeyFactory (java.security.KeyFactory)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 KeySpec (java.security.spec.KeySpec)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 CryptoException (org.bouncycastle.crypto.CryptoException)1 Signer (org.bouncycastle.crypto.Signer)1 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)1 RSAKeyParameters (org.bouncycastle.crypto.params.RSAKeyParameters)1