List of usage examples for org.bouncycastle.crypto.engines AESEngine AESEngine
public AESEngine()
From source file:org.ethereum.crypto.ECKey.java
License:Open Source License
/** * Decrypt cipher by AES in SIC(also know as CTR) mode * * @param cipher -proper cipher/*from ww w. java2s . co m*/ * @return decrypted cipher, equal length to the cipher. */ public byte[] decryptAES(byte[] cipher) { if (priv == null) { throw new MissingPrivateKeyException(); } AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(priv)); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); ctrEngine.init(false, params); int i = 0; byte[] out = new byte[cipher.length]; while (i < cipher.length) { ctrEngine.processBlock(cipher, i, out, i); i += engine.getBlockSize(); if (cipher.length - i < engine.getBlockSize()) { break; } } // process left bytes if (cipher.length - i > 0) { byte[] tmpBlock = new byte[16]; System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, cipher.length - i); } return out; }
From source file:org.ethereum.net.rlpx.FrameCodec.java
License:Open Source License
public FrameCodec(EncryptionHandshake.Secrets secrets) { this.mac = secrets.mac; AESEngine encCipher = new AESEngine(); enc = new SICBlockCipher(encCipher); enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[encCipher.getBlockSize()])); AESEngine decCipher = new AESEngine(); dec = new SICBlockCipher(decCipher); dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[decCipher.getBlockSize()])); egressMac = secrets.egressMac;//from w ww.jav a2 s. c o m ingressMac = secrets.ingressMac; }
From source file:org.ethereum.net.rlpx.FrameCodec.java
License:Open Source License
private AESEngine makeMacCipher() { // Stateless AES encryption AESEngine macc = new AESEngine(); macc.init(true, new KeyParameter(mac)); return macc;/*from w ww .j av a 2 s .c o m*/ }
From source file:org.fnppl.opensdx.security.SymmetricKey.java
License:Open Source License
public SymmetricKey(byte[] key_bytes, byte[] iv) { this.keyBytes = key_bytes; this.initVector = iv; CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine()); KeyParameter kp = new KeyParameter(keyBytes); ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector); aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding()); aesCipher.init(true, aesCBCParams);/*from ww w . j a v a 2s . com*/ blockSize = aesCipher.getBlockSize(); }
From source file:org.fnppl.opensdx.security.SymmetricKey.java
License:Open Source License
public void decrypt(InputStream in, OutputStream out) throws Exception { // if(key.length!=initvector.length || key.length!=keybits/8) { // throw new Exception("invalid params"); // }// ww w.j ava 2 s. c om CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine()); KeyParameter kp = new KeyParameter(keyBytes); ParametersWithIV aesCBCParams = new ParametersWithIV(kp, initVector); PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC, new PKCS7Padding()); aesCipher.init(false, aesCBCParams); // aesCipher.init(true, aesCBCParams); //TODO pad block corrupted error when false. WHY?? int read = -1; byte[] buff = new byte[128 / 8];//blocksize while ((read = in.read(buff)) != -1) { byte[] ou = new byte[buff.length]; // System.err.println("read: "+read); int rg = aesCipher.processBytes(buff, 0, read, ou, 0); out.write(ou, 0, rg); // System.err.println("rg: "+rg); } buff = new byte[2 * 128 / 8];//blocksize read = aesCipher.doFinal(buff, 0); out.write(buff, 0, read); }
From source file:org.freenetproject.freemail.RTSFetcher.java
License:Open Source License
private byte[] decrypt_rts(File rtsmessage) throws IOException, InvalidCipherTextException { // initialise our ciphers RSAKeyParameters ourprivkey = AccountManager.getPrivateKey(account.getProps()); AsymmetricBlockCipher deccipher = new RSAEngine(); deccipher.init(false, ourprivkey);//from w w w . ja va 2 s . c o m PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); // first n bytes will be an encrypted RSA block containting the // AES IV and Key. Read that. byte[] encrypted_params = new byte[deccipher.getInputBlockSize()]; int read = 0; FileInputStream fis = new FileInputStream(rtsmessage); try { while (read < encrypted_params.length) { read += fis.read(encrypted_params, read, encrypted_params.length - read); if (read < 0) break; } if (read < 0) { fis.close(); throw new InvalidCipherTextException("RTS Message too short"); } byte[] aes_iv_and_key = deccipher.processBlock(encrypted_params, 0, encrypted_params.length); KeyParameter kp = new KeyParameter(aes_iv_and_key, aescipher.getBlockSize(), aes_iv_and_key.length - aescipher.getBlockSize()); ParametersWithIV kpiv = new ParametersWithIV(kp, aes_iv_and_key, 0, aescipher.getBlockSize()); try { aescipher.init(false, kpiv); } catch (IllegalArgumentException iae) { fis.close(); throw new InvalidCipherTextException(iae.getMessage()); } byte[] plaintext = new byte[aescipher.getOutputSize((int) rtsmessage.length() - read)]; int ptbytes = 0; while (read < rtsmessage.length()) { byte[] buf = new byte[(int) rtsmessage.length() - read]; int thisread = fis.read(buf, 0, (int) rtsmessage.length() - read); ptbytes += aescipher.processBytes(buf, 0, thisread, plaintext, ptbytes); read += thisread; } try { aescipher.doFinal(plaintext, ptbytes); } catch (DataLengthException dle) { throw new InvalidCipherTextException(dle.getMessage()); } return plaintext; } finally { fis.close(); } }
From source file:org.jboss.aerogear.crypto.BlockCipher.java
License:Apache License
/** * Retrieve a new instance of the block mode provided * @param blockMode block mode name/*from w ww .j a v a 2 s .c o m*/ * @return instance to the block mode */ public static AEADBlockCipher getNewCipher(Mode blockMode) { AESEngine aesEngine = new AESEngine(); switch (blockMode) { case GCM: return new GCMBlockCipher(aesEngine); default: throw new RuntimeException("Block cipher not found"); } }
From source file:org.jpedal.io.DecryptionFactory.java
License:Open Source License
/** * decode AES ecnoded data with IV parameters * @param password/* w w w .j av a2s . c o m*/ * @param encKey * @param encData a data gained from deducting IV bytes in beginning (encData = data - ivBytes) * @param ivData * @return * @throws Exception */ private static byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding); cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.jpedal.io.security.BouncyCastleDecryption.java
License:Open Source License
@Override public byte[] decodeAES(final byte[] encKey, final byte[] encData, final byte[] ivData) throws Exception { final KeyParameter keyParam = new KeyParameter(encKey); final CipherParameters params = new ParametersWithIV(keyParam, ivData); // setup AES cipher in CBC mode with PKCS7 padding final BlockCipherPadding padding = new PKCS7Padding(); final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);//from w ww. ja va 2s. c om cipher.reset(); cipher.init(false, params); // create a temporary buffer to decode into (it'll include padding) final byte[] buf = new byte[cipher.getOutputSize(encData.length)]; int len = cipher.processBytes(encData, 0, encData.length, buf, 0); len += cipher.doFinal(buf, len); // remove padding final byte[] out = new byte[len]; System.arraycopy(buf, 0, out, 0, len); // return string representation of decoded bytes return out; }
From source file:org.opcfoundation.ua.transport.security.BcCryptoProvider.java
License:Open Source License
@Override public int decryptSymm(SecurityToken token, byte[] dataToDecrypt, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ServiceResultException { BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(token.getRemoteEncryptingKey()), token.getRemoteInitializationVector())); int decryptedBytes = cipher.processBytes(dataToDecrypt, inputOffset, inputLength, output, outputOffset); try {/*from w ww. ja v a 2s. c o m*/ decryptedBytes += cipher.doFinal(output, outputOffset + decryptedBytes); return decryptedBytes; } catch (DataLengthException e) { logger.error("Input data is not an even number of encryption blocks."); throw new ServiceResultException(StatusCodes.Bad_InternalError, "Error in symmetric decrypt: Input data is not an even number of encryption blocks."); } catch (CryptoException e) { throw new ServiceResultException(StatusCodes.Bad_InternalError, e); } }