List of usage examples for org.bouncycastle.crypto.modes SICBlockCipher SICBlockCipher
public SICBlockCipher(BlockCipher c)
From source file:cologne.eck.peafactory.crypto.CipherStuff.java
License:Open Source License
/** * Encrypt/decrypt an array of bytes. This function is only used to * encrypt the session key//from w w w . j a v a 2 s .co m * * @param forEncryption - true for encryption, false for decryption * @param input - plain text or cipher text * @param key - the cryptographic key for the cipher * @param zeroize - fills the key with 0 for true (when the key is no longer used) * @return - plain text or cipher text */ public final static byte[] processCTR(boolean forEncryption, byte[] input, byte[] key, boolean zeroize) { //Help.printBytes("key", input); KeyParameter kp = new KeyParameter(key); if (zeroize == true) { Zeroizer.zero(key); } byte[] iv = null; if (forEncryption == false) { // input is ciphertext, IV is stored on top of ciphertext // get IV: iv = new byte[CipherStuff.getCipherAlgo().getBlockSize()]; System.arraycopy(input, 0, iv, 0, iv.length); // truncate ciphertext: byte[] tmp = new byte[input.length - iv.length]; System.arraycopy(input, iv.length, tmp, 0, tmp.length); input = tmp; } else { // input is plaintext iv = new RandomStuff().createRandomBytes(CipherStuff.getCipherAlgo().getBlockSize()); } ParametersWithIV ivp = new ParametersWithIV(kp, iv); BufferedBlockCipher b = new BufferedBlockCipher(new SICBlockCipher(cipherAlgo)); b.init(true, ivp); byte[] out = new byte[input.length]; int len = b.processBytes(input, 0, input.length, out, 0); try { len += b.doFinal(out, len); } catch (DataLengthException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidCipherTextException e) { // TODO Auto-generated catch block e.printStackTrace(); } Zeroizer.zero(kp.getKey()); if (forEncryption == false) {// decryption: output plaintext return out; } else { // encryption: output (IV || ciphertext) Zeroizer.zero(input); // store IV on top of ciphertext byte[] tmp = new byte[out.length + iv.length]; System.arraycopy(iv, 0, tmp, 0, iv.length); System.arraycopy(out, 0, tmp, iv.length, out.length); return tmp; } }
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.//from w w w . j a v a 2 s. c om * @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:net.java.otr4j.crypto.OtrCryptoEngine.java
License:LGPL
/** * Decrypt AES-encrypted payload./*from w w w .j a va2 s . c o m*/ * * @param key the decryption key * @param ctr the counter value used in encryption * @param b the ciphertext * @return Returns the decrypted content. * @throws OtrCryptoException In case of illegal ciphertext. */ @Nonnull public static byte[] aesDecrypt(final byte[] key, final byte[] ctr, final byte[] b) throws OtrCryptoException { requireLengthExactly(CTR_LENGTH_BYTES, ctr); assert !allZeroBytes( key) : "Expected non-zero bytes for key. This may indicate that a critical bug is present, or it may be a false warning."; assert !allZeroBytes( b) : "Expected non-zero bytes for b. This may indicate that a critical bug is present, or it may be a false warning."; final AESEngine aesDec = new AESEngine(); final SICBlockCipher sicAesDec = new SICBlockCipher(aesDec); final BufferedBlockCipher bufSicAesDec = new BufferedBlockCipher(sicAesDec); bufSicAesDec.init(false, new ParametersWithIV(new KeyParameter(key), ctr)); final byte[] aesOutLwDec = new byte[b.length]; final int done = bufSicAesDec.processBytes(b, 0, b.length, aesOutLwDec, 0); try { bufSicAesDec.doFinal(aesOutLwDec, done); } catch (final InvalidCipherTextException ex) { throw new OtrCryptoException("Encrypted message contents is bad.", ex); } return aesOutLwDec; }
From source file:net.java.otr4j.crypto.OtrCryptoEngine.java
License:LGPL
/** * Encrypt payload using AES./*w ww .ja va 2s.com*/ * * @param key the encryption key * @param ctr the initial counter value to use * @param b the plaintext content in bytes * @return Returns the encrypted content. */ @Nonnull public static byte[] aesEncrypt(final byte[] key, final byte[] ctr, final byte[] b) { requireLengthExactly(CTR_LENGTH_BYTES, ctr); assert !allZeroBytes( key) : "Expected non-zero bytes for key. This may indicate that a critical bug is present, or it may be a false warning."; assert !allZeroBytes( b) : "Expected non-zero bytes for b. This may indicate that a critical bug is present, or it may be a false warning."; final AESEngine aesEnc = new AESEngine(); final SICBlockCipher sicAesEnc = new SICBlockCipher(aesEnc); final BufferedBlockCipher bufSicAesEnc = new BufferedBlockCipher(sicAesEnc); bufSicAesEnc.init(true, new ParametersWithIV(new KeyParameter(key), ctr)); final byte[] aesOutLwEnc = new byte[b.length]; final int done = bufSicAesEnc.processBytes(b, 0, b.length, aesOutLwEnc, 0); try { bufSicAesEnc.doFinal(aesOutLwEnc, done); } catch (final InvalidCipherTextException ex) { throw new IllegalStateException("Failed to encrypt content.", ex); } return aesOutLwEnc; }
From source file:net.java.otr4j.crypto.OtrCryptoEngineImpl.java
License:Apache License
@Override public byte[] aesDecrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException { AESFastEngine aesDec = new AESFastEngine(); SICBlockCipher sicAesDec = new SICBlockCipher(aesDec); BufferedBlockCipher bufSicAesDec = new BufferedBlockCipher(sicAesDec); // Create initial counter value 0. if (ctr == null) ctr = ZERO_CTR;/* ww w .ja v a 2 s . com*/ bufSicAesDec.init(false, new ParametersWithIV(new KeyParameter(key), ctr)); byte[] aesOutLwDec = new byte[b.length]; int done = bufSicAesDec.processBytes(b, 0, b.length, aesOutLwDec, 0); try { bufSicAesDec.doFinal(aesOutLwDec, done); } catch (Exception e) { throw new OtrCryptoException(e); } return aesOutLwDec; }
From source file:net.java.otr4j.crypto.OtrCryptoEngineImpl.java
License:Apache License
@Override public byte[] aesEncrypt(byte[] key, byte[] ctr, byte[] b) throws OtrCryptoException { AESFastEngine aesEnc = new AESFastEngine(); SICBlockCipher sicAesEnc = new SICBlockCipher(aesEnc); BufferedBlockCipher bufSicAesEnc = new BufferedBlockCipher(sicAesEnc); // Create initial counter value 0. if (ctr == null) ctr = ZERO_CTR;/*from w w w . java 2s . c o m*/ bufSicAesEnc.init(true, new ParametersWithIV(new KeyParameter(key), ctr)); byte[] aesOutLwEnc = new byte[b.length]; int done = bufSicAesEnc.processBytes(b, 0, b.length, aesOutLwEnc, 0); try { bufSicAesEnc.doFinal(aesOutLwEnc, done); } catch (Exception e) { throw new OtrCryptoException(e); } return aesOutLwEnc; }
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 w w . ja v a 2 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 . j a v a2 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);//from ww w.j a v a 2 s .c om 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); 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);//from w w w . ja v a2s . c o m byte[] in = payload; byte[] out = new byte[in.length]; int i = 0; 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)); }