List of usage examples for org.bouncycastle.crypto.encodings OAEPEncoding OAEPEncoding
public OAEPEncoding(AsymmetricBlockCipher cipher)
From source file:cf.monteux.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * decrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul>// ww w . j av a2 s .co m * <li>OAEP padding [42 bytes] (RSA-encrypted)</li> * <li>Symmetric key [16 bytes]</li> * <li>First part of data [70 bytes]</li> * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)</li> * </ul> * encrypt and store in result * * @param priv key to use for decryption * @param data to be decrypted, needs currently to be at least 70 bytes long * @return raw data */ public static byte[] asymDecrypt(final RSAPrivateKey priv, final byte[] data) throws TorException { if (data == null) { throw new NullPointerException("can't encrypt NULL data"); } if (data.length < 70) { throw new TorException("input array too short"); } try { int encryptedBytes = 0; // init OAEP final OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent())); // apply RSA+OAEP encryptedBytes = oaep.getInputBlockSize(); final byte[] oaepInput = new byte[encryptedBytes]; System.arraycopy(data, 0, oaepInput, 0, encryptedBytes); final byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes); // extract symmetric key final byte[] symmetricKey = new byte[16]; System.arraycopy(part1, 0, symmetricKey, 0, 16); // init AES final AESCounterMode aes = new AESCounterMode(symmetricKey); // apply AES final byte[] aesInput = new byte[data.length - encryptedBytes]; System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length); final byte[] part2 = aes.processStream(aesInput); // replace unencrypted data final byte[] result = new byte[part1.length - 16 + part2.length]; System.arraycopy(part1, 16, result, 0, part1.length - 16); System.arraycopy(part2, 0, result, part1.length - 16, part2.length); return result; } catch (final InvalidCipherTextException e) { logger.error("Encryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage()); throw new TorException("Encryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage()); } }
From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * decrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul>/*from www. ja v a 2s . c o m*/ * <li>OAEP padding [42 bytes] (RSA-encrypted)</li> * <li>Symmetric key [16 bytes]</li> * <li>First part of data [70 bytes]</li> * <li>Second part of data [x-70 bytes] (Symmetrically encrypted)</li> * </ul> * encrypt and store in result * * @param priv key to use for decryption * @param data to be decrypted, needs currently to be at least 70 bytes long * @return raw data */ public static byte[] asymDecrypt(final RSAPrivateKey priv, final byte[] data) throws TorException { if (data == null) { throw new NullPointerException("can't encrypt NULL data"); } if (data.length < 70) { throw new TorException("input array too short"); } try { int encryptedBytes = 0; // init OAEP final OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent())); // apply RSA+OAEP encryptedBytes = oaep.getInputBlockSize(); final byte[] oaepInput = new byte[encryptedBytes]; System.arraycopy(data, 0, oaepInput, 0, encryptedBytes); final byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes); // extract symmetric key final byte[] symmetricKey = new byte[16]; System.arraycopy(part1, 0, symmetricKey, 0, 16); // init AES final AESCounterMode aes = new AESCounterMode(symmetricKey); // apply AES final byte[] aesInput = new byte[data.length - encryptedBytes]; System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length); final byte[] part2 = aes.processStream(aesInput); // replace unencrypted data final byte[] result = new byte[part1.length - 16 + part2.length]; System.arraycopy(part1, 16, result, 0, part1.length - 16); System.arraycopy(part2, 0, result, part1.length - 16, part2.length); return result; } catch (final InvalidCipherTextException e) { LOG.error("Encryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage()); throw new TorException("Encryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage()); } }
From source file:edu.biu.scapi.midLayer.asymmetricCrypto.encryption.BcRSAOaep.java
License:Open Source License
/** * Constructor that lets the user choose the source of randomness. * @param random source of randomness.//from w w w .j a v a 2s. co m */ public BcRSAOaep(SecureRandom random) { this.random = random; //Creates the OAEP encoding with RSABlindedEngine of BC. this.bcBlockCipher = new OAEPEncoding(new RSABlindedEngine()); }
From source file:org.fnppl.opensdx.security.PrivateKey.java
License:Open Source License
public byte[] decrypt(byte[] data) throws Exception { RSABlindedEngine rsae = new RSABlindedEngine(); OAEPEncoding oaep = new OAEPEncoding(rsae); oaep.init(false, //fr encrypt: true // bp priv);//from w w w . j a v a 2 s. c om if (data.length > rsae.getInputBlockSize()) { throw new RuntimeException("PrivateKey.encrypt::data.length(" + data.length + ") too long - max is: " + rsae.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.fnppl.opensdx.security.PrivateKey.java
License:Open Source License
public byte[] encrypt(byte[] data) throws Exception { RSABlindedEngine rsae = new RSABlindedEngine(); // RSABlindingEngine rsae = new RSABlindingEngine(); // /* w w w .java 2s .c o m*/ // RSABlindingParameters bp = new RSABlindingParameters( // priv, // PublicKey.generateBlindingFactor(pubkey) // ); OAEPEncoding oaep = new OAEPEncoding(rsae); oaep.init(true, //fr encrypt: true // bp priv); if (data.length > rsae.getInputBlockSize()) { throw new RuntimeException("PrivateKey.encrypt::data.length(" + data.length + ") too long - max is: " + rsae.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.fnppl.opensdx.security.PublicKey.java
License:Open Source License
public byte[] encrypt(byte[] data) throws Exception { // RSABlindingEngine rsae = new RSABlindingEngine(); RSABlindedEngine rsab = new RSABlindedEngine(); // RSABlindingParameters bp = new RSABlindingParameters( // pub, // generateBlindingFactor(pub) // ); // OAEPEncoding oaep = new OAEPEncoding(rsae); OAEPEncoding oaep = new OAEPEncoding(rsab); oaep.init(true, //fr encrypt: true pub/*w w w . j av a 2s. c o m*/ // bp ); if (data.length > rsab.getInputBlockSize()) { throw new RuntimeException("PublicKey.encrypt::data.length(" + data.length + ") too long - max is: " + oaep.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.fnppl.opensdx.security.PublicKey.java
License:Open Source License
public byte[] decrypt(byte[] data) throws Exception { // RSABlindingEngine rsae = new RSABlindingEngine(); RSABlindedEngine rsae = new RSABlindedEngine(); // RSABlindingParameters bp = new RSABlindingParameters( // pub, // generateBlindingFactor(pub) // ); OAEPEncoding oaep = new OAEPEncoding(rsae); oaep.init(false, //fr encrypt: true pub/*from www . jav a2 s.c o m*/ // bp ); if (data.length > rsae.getInputBlockSize()) { throw new RuntimeException("PublicKey.decrypt::data.length(" + data.length + ") too long - max is: " + oaep.getInputBlockSize()); } return oaep.processBlock(data, 0, data.length); }
From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * encrypt data with asymmetric key. create asymmetricla encrypted data:<br> * <ul>/*w w w . j a v a2s. com*/ * <li>OAEP padding [42 bytes] (RSA-encrypted) * <li>Symmetric key [16 bytes] FIXME: we assume that we ALWAYS need this * <li>First part of data [70 bytes] * <li>Second part of data [x-70 bytes] (Symmetrically encrypted) * <ul> * encrypt and store in result * * @param pub * @param symmetricKey AES key * @param data * to be encrypted, needs currently to be at least 70 bytes long * @return the first half of the key exchange, ready to be send to the other * partner */ public static byte[] asymEncrypt(RSAPublicKey pub, byte[] symmetricKey, byte[] data) throws TorException { if (data == null) { throw new NullPointerException("can't encrypt NULL data"); } if (data.length < 70) { throw new TorException("input array too short"); } try { int encryptedBytes = 0; // initialize OAEP OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(true, new RSAKeyParameters(false, pub.getModulus(), pub.getPublicExponent())); // apply RSA+OAEP encryptedBytes = oaep.getInputBlockSize(); byte[] oaepInput = new byte[encryptedBytes]; System.arraycopy(data, 0, oaepInput, 0, encryptedBytes); byte[] part1 = oaep.encodeBlock(oaepInput, 0, encryptedBytes); // initialize AES AESCounterMode aes = new AESCounterMode(true, symmetricKey); // apply AES byte[] aesInput = new byte[data.length - encryptedBytes]; System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length); byte part2[] = aes.processStream(aesInput); // replace unencrypted data byte[] result = new byte[part1.length + part2.length]; System.arraycopy(part1, 0, result, 0, part1.length); System.arraycopy(part2, 0, result, part1.length, part2.length); return result; } catch (InvalidCipherTextException e) { log.severe("Node.asymEncrypt(): can't encrypt cipher text:" + e.getMessage()); throw new TorException("InvalidCipherTextException:" + e.getMessage()); } }
From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * decrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul>/* w w w .java2 s .co m*/ * <li>OAEP padding [42 bytes] (RSA-encrypted) * <li>Symmetric key [16 bytes] * <li>First part of data [70 bytes] * <li>Second part of data [x-70 bytes] (Symmetrically encrypted) * <ul> * encrypt and store in result * * @param priv * key to use for decryption * @param data * to be decrypted, needs currently to be at least 70 bytes long * @return raw data */ public static byte[] asymDecrypt(RSAPrivateKey priv, byte[] data) throws TorException { if (data == null) { throw new NullPointerException("can't encrypt NULL data"); } if (data.length < 70) { throw new TorException("input array too short"); } try { int encryptedBytes = 0; // init OAEP OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(false, new RSAKeyParameters(true, priv.getModulus(), priv.getPrivateExponent())); // apply RSA+OAEP encryptedBytes = oaep.getInputBlockSize(); byte[] oaepInput = new byte[encryptedBytes]; System.arraycopy(data, 0, oaepInput, 0, encryptedBytes); byte[] part1 = oaep.decodeBlock(oaepInput, 0, encryptedBytes); // extract symmetric key byte[] symmetricKey = new byte[16]; System.arraycopy(part1, 0, symmetricKey, 0, 16); // init AES AESCounterMode aes = new AESCounterMode(true, symmetricKey); // apply AES byte[] aesInput = new byte[data.length - encryptedBytes]; System.arraycopy(data, encryptedBytes, aesInput, 0, aesInput.length); byte part2[] = aes.processStream(aesInput); // replace unencrypted data byte[] result = new byte[part1.length - 16 + part2.length]; System.arraycopy(part1, 16, result, 0, part1.length - 16); System.arraycopy(part2, 0, result, part1.length - 16, part2.length); return result; } catch (InvalidCipherTextException e) { log.severe("CommonEncryption.asymDecrypt(): can't decrypt cipher text:" + e.getMessage()); throw new TorException("CommonEncryption.asymDecrypt(): InvalidCipherTextException:" + e.getMessage()); } }
From source file:org.xwiki.crypto.cipher.internal.asymmetric.factory.AbstractBcOAEPEncodedCipherFactory.java
License:Open Source License
@Override protected AsymmetricBlockCipher getCipherInstance(boolean forEncryption, AsymmetricCipherParameters parameters) { return new OAEPEncoding(getEngineInstance()); }