Search in sources :

Example 1 with AsymmetricBlockCipher

use of org.bouncycastle.crypto.AsymmetricBlockCipher 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 AsymmetricBlockCipher

use of org.bouncycastle.crypto.AsymmetricBlockCipher in project xipki by xipki.

the class SignerUtil method createPSSRSASigner.

// CHECKSTYLE:SKIP
public static PSSSigner createPSSRSASigner(AlgorithmIdentifier sigAlgId, AsymmetricBlockCipher cipher) throws XiSecurityException {
    ParamUtil.requireNonNull("sigAlgId", sigAlgId);
    if (!PKCSObjectIdentifiers.id_RSASSA_PSS.equals(sigAlgId.getAlgorithm())) {
        throw new XiSecurityException("signature algorithm " + sigAlgId.getAlgorithm() + " is not allowed");
    }
    AlgorithmIdentifier digAlgId;
    try {
        digAlgId = AlgorithmUtil.extractDigesetAlgFromSigAlg(sigAlgId);
    } catch (NoSuchAlgorithmException ex) {
        throw new XiSecurityException(ex.getMessage(), ex);
    }
    RSASSAPSSparams param = RSASSAPSSparams.getInstance(sigAlgId.getParameters());
    AlgorithmIdentifier mfgDigAlgId = AlgorithmIdentifier.getInstance(param.getMaskGenAlgorithm().getParameters());
    Digest dig = getDigest(digAlgId);
    Digest mfgDig = getDigest(mfgDigAlgId);
    int saltSize = param.getSaltLength().intValue();
    int trailerField = param.getTrailerField().intValue();
    AsymmetricBlockCipher tmpCipher = (cipher == null) ? new RSABlindedEngine() : cipher;
    return new PSSSigner(tmpCipher, dig, mfgDig, saltSize, getTrailer(trailerField));
}
Also used : XiSecurityException(org.xipki.security.exception.XiSecurityException) Digest(org.bouncycastle.crypto.Digest) RSABlindedEngine(org.bouncycastle.crypto.engines.RSABlindedEngine) RSASSAPSSparams(org.bouncycastle.asn1.pkcs.RSASSAPSSparams) PSSSigner(org.bouncycastle.crypto.signers.PSSSigner) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier) AsymmetricBlockCipher(org.bouncycastle.crypto.AsymmetricBlockCipher)

Example 3 with AsymmetricBlockCipher

use of org.bouncycastle.crypto.AsymmetricBlockCipher 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

AsymmetricBlockCipher (org.bouncycastle.crypto.AsymmetricBlockCipher)3 PKCS1Encoding (org.bouncycastle.crypto.encodings.PKCS1Encoding)2 RSAEngine (org.bouncycastle.crypto.engines.RSAEngine)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 RSASSAPSSparams (org.bouncycastle.asn1.pkcs.RSASSAPSSparams)1 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)1 Digest (org.bouncycastle.crypto.Digest)1 RSABlindedEngine (org.bouncycastle.crypto.engines.RSABlindedEngine)1 PSSSigner (org.bouncycastle.crypto.signers.PSSSigner)1 XiSecurityException (org.xipki.security.exception.XiSecurityException)1