Example usage for org.bouncycastle.crypto.encodings OAEPEncoding OAEPEncoding

List of usage examples for org.bouncycastle.crypto.encodings OAEPEncoding OAEPEncoding

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.encodings OAEPEncoding OAEPEncoding.

Prototype

public OAEPEncoding(AsymmetricBlockCipher cipher, Digest hash) 

Source Link

Usage

From source file:co.lqnt.lockbox.Cipher.java

License:Open Source License

/**
 * Construct a new bi-directional cipher.
 *//*  www .j a v  a2 s. co m*/
public Cipher() {
    CodecInterface base64UriCodec = new Base64UriCodec();
    AsymmetricBlockCipher rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    BufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()),
            new PKCS7Padding());
    Digest sha1Digest = new SHA1Digest();
    SecureRandom random = new SecureRandom();

    this.encryptionCipher = new EncryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest, random);
    this.decryptionCipher = new DecryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest);
}

From source file:co.lqnt.lockbox.DecryptionCipher.java

License:Open Source License

/**
 * Construct a new decryption cipher./*from   w w  w  .  ja  v  a 2s. c o m*/
 */
public DecryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:co.lqnt.lockbox.EncryptionCipher.java

License:Open Source License

/**
 * Construct a new encryption cipher.//  w  ww .j  av  a 2 s .co m
 */
public EncryptionCipher() {
    this.base64UriCodec = new Base64UriCodec();
    this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
    this.sha1Digest = new SHA1Digest();
    this.random = new SecureRandom();
    this.asciiCharset = Charset.forName("US-ASCII");
}

From source file:dorkbox.util.crypto.RsaTest.java

License:Apache License

@SuppressWarnings("deprecation")
@Test//from   ww w  .  ja  va 2s .com
public void Rsa() {
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

    AsymmetricCipherKeyPair key = CryptoRSA.generateKeyPair(new SecureRandom(entropySeed.getBytes()), 1024);

    RSAKeyParameters public1 = (RSAKeyParameters) key.getPublic();
    RSAPrivateCrtKeyParameters private1 = (RSAPrivateCrtKeyParameters) key.getPrivate();

    RSAEngine engine = new RSAEngine();
    SHA1Digest digest = new SHA1Digest();
    OAEPEncoding rsaEngine = new OAEPEncoding(engine, digest);

    // test encrypt/decrypt
    byte[] encryptRSA = CryptoRSA.encrypt(rsaEngine, public1, bytes, logger);
    byte[] decryptRSA = CryptoRSA.decrypt(rsaEngine, private1, encryptRSA, logger);

    if (Arrays.equals(bytes, encryptRSA)) {
        fail("bytes should not be equal");
    }

    if (!Arrays.equals(bytes, decryptRSA)) {
        fail("bytes not equal");
    }

    // test signing/verification
    PSSSigner signer = new PSSSigner(engine, digest, digest.getDigestSize());

    byte[] signatureRSA = CryptoRSA.sign(signer, private1, bytes, logger);
    boolean verify = CryptoRSA.verify(signer, public1, signatureRSA, bytes);

    if (!verify) {
        fail("failed signature verification");
    }
}

From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java

License:Open Source License

private AsymmetricBlockCipher getAsymmetricCipher(boolean forEncryption, SecurityAlgorithm algorithm,
        CipherParameters params) throws ServiceResultException {
    AsymmetricBlockCipher cipher = null;
    if (algorithm.equals(SecurityAlgorithm.Rsa15)) {
        cipher = new PKCS1Encoding(new RSAEngine());
    } else if (algorithm.equals(SecurityAlgorithm.RsaOaep)) {
        cipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest());
    } else {//from   ww w. ja  v a 2  s .c om
        throw new ServiceResultException(StatusCodes.Bad_SecurityPolicyRejected,
                "Unsupported asymmetric encryption algorithm: " + algorithm);
    }
    cipher.init(forEncryption, params);
    return cipher;
}