List of usage examples for org.bouncycastle.crypto.encodings OAEPEncoding getInputBlockSize
public int getInputBlockSize()
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>//from w ww. j a va 2 s. c om * <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 ww w . jav a2 s . com*/ * <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: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/*from www. j a va2s. 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 w w w .j a v a 2 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>/* www. jav a 2 s . c om*/ * <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>/*from ww w .j av a 2s. c om*/ * <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:TorJava.Common.Encryption.java
License:Open Source License
/** * encrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul>/* w w w . j av a 2 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[] asym_decrypt(RSAKeyParameters 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 encrypted_bytes = 0; // init OAEP OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(false, priv); // apply RSA+OAEP encrypted_bytes = oaep.getInputBlockSize(); byte[] oaep_input = new byte[encrypted_bytes]; System.arraycopy(data, 0, oaep_input, 0, encrypted_bytes); byte[] part1 = oaep.decodeBlock(oaep_input, 0, encrypted_bytes); // extract symmetric key byte[] symmetric_key = new byte[16]; System.arraycopy(part1, 0, symmetric_key, 0, 16); // init AES AESCounterMode aes = new AESCounterMode(true, symmetric_key); // apply AES byte[] aes_input = new byte[data.length - encrypted_bytes]; System.arraycopy(data, encrypted_bytes, aes_input, 0, aes_input.length); byte part2[] = aes.processStream(aes_input); // 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) { Logger.logCell(Logger.ERROR, "CommonEncryption.asym_decrypt(): can't decrypt cipher text:" + e.getMessage()); throw new TorException("CommonEncryption.asym_decrypt(): InvalidCipherTextException:" + e.getMessage()); } }
From source file:TorJava.Node.java
License:Open Source License
/** * encrypt data with asymmetric key. create asymmetricla encrypted data:<br> * <ul>//from ww w. j av a 2 s .c o m * <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 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 */ byte[] asym_encrypt(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 encrypted_bytes = 0; // init OAEP OAEPEncoding oaep = new OAEPEncoding(new RSAEngine()); oaep.init(true, new RSAKeyParameters(false, server.onionKey.getModulus(), server.onionKey.getPublicExponent())); // apply RSA+OAEP encrypted_bytes = oaep.getInputBlockSize(); byte[] oaep_input = new byte[encrypted_bytes]; System.arraycopy(data, 0, oaep_input, 0, encrypted_bytes); byte[] part1 = oaep.encodeBlock(oaep_input, 0, encrypted_bytes); // init AES AESCounterMode aes = new AESCounterMode(true, symmetric_key_for_create); // apply AES byte[] aes_input = new byte[data.length - encrypted_bytes]; System.arraycopy(data, encrypted_bytes, aes_input, 0, aes_input.length); byte part2[] = aes.processStream(aes_input); // 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) { Logger.logCell(Logger.ERROR, "Node.asym_encrypt(): can't encrypt cipher text:" + e.getMessage()); throw new TorException("InvalidCipherTextException:" + e.getMessage()); } }