List of usage examples for org.bouncycastle.crypto.modes SICBlockCipher init
public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException
From source file:freenet.support.io.FileUtil.java
License:GNU General Public License
/** Write hard to identify random data to the OutputStream. Does not drain the global secure * random number generator, and is significantly faster than it. * @param os The stream to write to./*www . jav a2s. com*/ * @param length The number of bytes to write. * @throws IOException If unable to write to the stream. */ public static void fill(OutputStream os, long length) throws IOException { long remaining = length; byte[] buffer = new byte[BUFFER_SIZE]; int read = 0; while ((remaining == -1) || (remaining > 0)) { synchronized (FileUtil.class) { if (cis == null || cisCounter > Long.MAX_VALUE / 2) { // Reset it well before the birthday paradox (note this is actually counting bytes). byte[] key = new byte[16]; byte[] iv = new byte[16]; SecureRandom rng = NodeStarter.getGlobalSecureRandom(); rng.nextBytes(key); rng.nextBytes(iv); AESFastEngine e = new AESFastEngine(); SICBlockCipher ctr = new SICBlockCipher(e); ctr.init(true, new ParametersWithIV(new KeyParameter(key), iv)); cis = new CipherInputStream(zis, new BufferedBlockCipher(ctr)); cisCounter = 0; } read = cis.read(buffer, 0, ((remaining > BUFFER_SIZE) || (remaining == -1)) ? BUFFER_SIZE : (int) remaining); cisCounter += read; } if (read == -1) { if (length == -1) { return; } throw new EOFException("stream reached eof"); } os.write(buffer, 0, read); if (remaining > 0) remaining -= read; } }
From source file:org.bunkr.core.crypto.CipherBuilder.java
License:Open Source License
/** * Build a block cipher for encrypting or decrypting the target file. * * Intelligently create the correct cipher and initialize it correctly from the encryptionData present on the * target file. If the goal is encryption, the encryption data for the file is reinitialized from random. * * @param file the target FileInventoryItem * @param encrypting boolean indicating encryption (true) or decryption (false) * @return a BlockCipher//from w ww .j a v a2 s.c o m */ public static BlockCipher buildCipherForFile(FileInventoryItem file, boolean encrypting) { Encryption alg = file.getEncryptionAlgorithm(); if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) { if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) { SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine()); byte[] edata = file.getEncryptionData(); if (encrypting) { edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()]; RandomMaker.fill(edata); file.setEncryptionData(edata); } byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength); byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength, alg.keyByteLength + fileCipher.getBlockSize()); fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv)); return fileCipher; } } else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) { if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) { SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine()); byte[] edata = file.getEncryptionData(); if (encrypting) { edata = new byte[alg.keyByteLength + fileCipher.getBlockSize()]; RandomMaker.fill(edata); file.setEncryptionData(edata); } byte[] ekey = Arrays.copyOfRange(edata, 0, alg.keyByteLength); byte[] eiv = Arrays.copyOfRange(edata, alg.keyByteLength, alg.keyByteLength + fileCipher.getBlockSize()); fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(ekey), eiv)); return fileCipher; } } throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg)); }
From source file:org.bunkr.core.crypto.CipherBuilder.java
License:Open Source License
/** * Simple version of buildCipherForFile, this time without the encryption data manipulation or file object. * * @param alg the encryption algorithm being used * @param key encryption key data bytes/*from w w w . ja va 2 s . co m*/ * @param iv initialization vector data bytes * @return a BlockCipher */ public static BlockCipher buildCipher(Encryption alg, boolean encrypting, byte[] key, byte[] iv) { if (key.length != alg.keyByteLength) throw new IllegalArgumentException(String.format("Supplied key length %s != required key length %s", key.length, alg.keyByteLength)); if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.AES)) { SICBlockCipher fileCipher = new SICBlockCipher(new AESEngine()); if (iv.length != fileCipher.getBlockSize()) throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s", iv.length, fileCipher.getBlockSize())); if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) { fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv)); return fileCipher; } } else if (alg.c.equals(Algorithms.SYMMETRIC_CIPHER.TWOFISH)) { SICBlockCipher fileCipher = new SICBlockCipher(new TwofishEngine()); if (iv.length != fileCipher.getBlockSize()) throw new IllegalArgumentException(String.format("Supplied iv length %s != required iv length %s", iv.length, fileCipher.getBlockSize())); if (alg.m.equals(Algorithms.SYMMETRIC_MODE.CTR)) { fileCipher.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv)); return fileCipher; } } throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", alg)); }
From source file:org.ethereum.crypto.CryptoTest.java
License:Open Source License
@Test // basic encryption/decryption public void test11() throws Throwable { byte[] keyBytes = HashUtil.keccak256("...".getBytes()); log.info("key: {}", Hex.toHexString(keyBytes)); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("22400891000000000000000000000000"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); ctrEngine.init(true, params); byte[] cipher = new byte[16]; ctrEngine.processBlock(payload, 0, cipher, 0); log.info("cipher: {}", Hex.toHexString(cipher)); byte[] output = new byte[cipher.length]; ctrEngine.init(false, params);/* ww w .j ava 2 s . c o m*/ ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
From source file:org.ethereum.crypto.CryptoTest.java
License:Open Source License
@Test // big packet encryption public void test12() throws Throwable { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode( "0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode( "f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params); byte[] in = payload; byte[] out = new byte[in.length]; int i = 0;//from w ww . j a va2s . c o m while (i < in.length) { ctrEngine.processBlock(in, i, out, i); i += engine.getBlockSize(); if (in.length - i < engine.getBlockSize()) break; } // process left bytes if (in.length - i > 0) { byte[] tmpBlock = new byte[16]; System.arraycopy(in, i, tmpBlock, 0, in.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, in.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }
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 w ww . ja va2 s .com*/ * @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; }