private byte[] doOperation(byte[] input, boolean isEncrypt) { AsymmetricBlockCipher cipher = new OAEPEncoding(new RSAEngine(), new SHA256Digest(), new SHA1Digest(), null); RSAKeyParameters key = isEncrypt ? publicKey : privateKey; cipher.init(isEncrypt, key); try { return cipher.processBlock(input, 0, input.length); } catch (InvalidCipherTextException e) { throw new EncryptionException("Encryption fails", e); } }
private void initFromSpec( OAEPParameterSpec pSpec) throws NoSuchPaddingException { MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm()); if (digest == null) { throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm()); } cipher = new OAEPEncoding(new RSABlindedEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue()); paramSpec = pSpec; }
private void initFromSpec( OAEPParameterSpec pSpec) throws NoSuchPaddingException { MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm()); if (digest == null) { throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm()); } cipher = new BufferedAsymmetricBlockCipher(new OAEPEncoding(new ElGamalEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue())); paramSpec = pSpec; }
private void encDec( String label, RSAKeyParameters pubParameters, RSAKeyParameters privParameters, byte[] seed, byte[] input, byte[] output) throws InvalidCipherTextException { AsymmetricBlockCipher cipher = new OAEPEncoding(new RSAEngine()); cipher.init(true, new ParametersWithRandom(pubParameters, new VecRand(seed))); byte[] out; out = cipher.processBlock(input, 0, input.length); for (int i = 0; i != output.length; i++) { if (out[i] != output[i]) { fail(label + " failed encryption"); } } cipher.init(false, privParameters); out = cipher.processBlock(output, 0, output.length); for (int i = 0; i != input.length; i++) { if (out[i] != input[i]) { fail(label + " failed decoding"); } } }
private void initFromSpec(OAEPParameterSpec pSpec) throws NoSuchPaddingException, NoSuchFieldException, IllegalAccessException { MGF1ParameterSpec mgfParams = (MGF1ParameterSpec)pSpec.getMGFParameters(); Digest digest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm()); if (digest == null) { throw new NoSuchPaddingException("no match on OAEP constructor for digest algorithm: "+ mgfParams.getDigestAlgorithm()); } cipher(new OAEPEncoding(new NativeRSAEngine(), digest, ((PSource.PSpecified)pSpec.getPSource()).getValue())); set( pSpec, "paramSpec" ); }
public BcRsaCipher(byte[] publicKey, byte[] privateKey) { super(publicKey); try { PKS8RsaPrivateKey pks8RsaPrivateKey = new PKS8RsaPrivateKey(privateKey); AsymmetricKeyParameter keyParameter = new RSAKeyParameters(true, pks8RsaPrivateKey.getModulus(), pks8RsaPrivateKey.getExponent()); cipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); cipher.init(false, new ParametersWithRandom(keyParameter, RuntimeRandomProvider.INSTANCE)); } catch (Exception e) { e.printStackTrace(); } }
public BcRsaEncryptCipher(byte[] publicKey) { try { X509RsaPublicKey key = new X509RsaPublicKey(publicKey); RSAKeyParameters param = new RSAKeyParameters(false, key.getModulus(), key.getExponent()); cipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); cipher.init(true, new ParametersWithRandom(param, RuntimeRandomProvider.INSTANCE)); } catch (Exception e) { e.printStackTrace(); } }
/** * encrypt data with asymmetric key. create asymmetricla encrypted data:<br> * <ul> * <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()); } }
/** * decrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul> * <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()); } }
public OAEPPadding() { super(new OAEPEncoding(new RSABlindedEngine())); }
/** * decrypt data with asymmetric key. create asymmetrically encrypted data:<br> * <ul> * <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()); } }