private AESEngine makeMacCipher() { // Stateless AES encryption AESEngine macc = new AESEngine(); macc.init(true, new KeyParameter(mac)); return macc; }
public FrameCodec(EncryptionHandshake.Secrets secrets) { this.mac = secrets.mac; BlockCipher cipher; enc = new SICBlockCipher(cipher = new AESEngine()); enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); dec = new SICBlockCipher(cipher = new AESEngine()); dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); egressMac = secrets.egressMac; ingressMac = secrets.ingressMac; }
SICBlockCipher ctrEngine = new SICBlockCipher(engine); KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD())); ParametersWithIV params = new ParametersWithIV(key, new byte[16]);
cipher.init(true, new ParametersWithIV(new KeyParameter(K1), IV)); cipher.init(true, new KeyParameter(K1)); K2a = K2; mac.init(new KeyParameter(K2a)); mac.update(IV, 0, IV.length); mac.update(C, 0, C.length);
cipher.init(false, new ParametersWithIV(new KeyParameter(K1), IV)); cipher.init(false, new KeyParameter(K1)); K2a = K2; mac.init(new KeyParameter(K2a)); mac.update(IV, 0, IV.length); mac.update(in_enc, inOff + V.length, inLen - V.length - T2.length);
@Test // basic encryption/decryption public void test11() throws Throwable { byte[] keyBytes = sha3("...".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); ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
@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; 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)); }
static void process(byte[] key, byte[] nonce, byte[] buf, int off, int len) { ChaChaEngine e = new ChaChaEngine(20); e.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); e.processBytes(buf, off, len, buf, off); } }
public void setKey(byte[] key, int keyOff, int keyLen) throws IOException { KeyParameter cipherKey = new KeyParameter(key, keyOff, keyLen); cipher.init(isEncrypting, new ParametersWithIV(cipherKey, ZEROES, 0, 12)); }
private void E(byte[] key, byte[] s, int sOff, byte[] in, int inOff) { cipher.init(true, new KeyParameter(key)); cipher.processBlock(in, inOff, s, sOff); }
public static byte[] decrypt(byte[] dataToDecrypt, String password, String charset) throws CryptoException,UnsupportedEncodingException { byte[] defaultiv = new byte[16]; EncryptedData data = new EncryptedData(defaultiv, dataToDecrypt); return decrypt(data, new KeyParameter(Sha256Hash.hash(password.getBytes(charset)))); }
public static byte[] decrypt(byte[] dataToDecrypt, String password) throws CryptoException{ byte[] defaultiv = new byte[16]; EncryptedData data = new EncryptedData(defaultiv, dataToDecrypt); return decrypt(data, new KeyParameter(Sha256Hash.hash(password.getBytes()))); }
@Override public void initialize(Direction direction, SymmetricKey key) { this.key = key.getKey(); if (initialized()) { CipherParameters params = new KeyParameter(this.key); aesEngine.init(direction == Direction.ENCRYPT, params); } }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
/** Encripta un bloque usando AES. * @param key Clave AES. * @param z Bloque a crifrar. * @return Bloque cifrado. */ public static byte[] encryptBlock(final byte[] key, final byte[] z) { final byte[] s = new byte[BLOCK_SIZE]; final KeyParameter encKey = new KeyParameter(key); final BlockCipher cipher = new AESEngine(); cipher.init(true, encKey); cipher.processBlock(z, 0, s, 0); return s; }
static HMac createHmacSha512Digest(byte[] key) { SHA512Digest digest = new SHA512Digest(); HMac hMac = new HMac(digest); hMac.init(new KeyParameter(key)); return hMac; }
public static BufferedBlockCipher createSymmetricKeyWrapper(boolean forEncryption, BlockCipher engine, byte[] key, byte[] iv) { BufferedBlockCipher c = new BufferedBlockCipher(new CFBBlockCipher(engine, engine.getBlockSize() * 8)); c.init(forEncryption, new ParametersWithIV(new KeyParameter(key), iv)); return c; }
/** Creates a new instance of AESCipher */ public AESCipherCBCnoPad(boolean forEncryption, byte[] key) { BlockCipher aes = new AESFastEngine(); cbc = new CBCBlockCipher(aes); KeyParameter kp = new KeyParameter(key); cbc.init(forEncryption, kp); }
CMSOutputEncryptor(ASN1ObjectIdentifier encryptionOID, int keySize, SecureRandom random) throws CMSException { if (random == null) { random = new SecureRandom(); } CipherKeyGenerator keyGen = helper.createKeyGenerator(encryptionOID, random); encKey = new KeyParameter(keyGen.generateKey()); algorithmIdentifier = helper.generateAlgorithmIdentifier(encryptionOID, encKey, random); cipher = helper.createContentCipher(true, encKey, algorithmIdentifier); }
@Override public byte[] doAesCmac(final byte[] data, final byte[] key) { final BlockCipher cipher = new AESEngine(); final org.spongycastle.crypto.Mac mac = new CMac(cipher, 64); final KeyParameter keyP = new KeyParameter(key); mac.init(keyP); mac.update(data, 0, data.length); final byte[] out = new byte[8]; mac.doFinal(out, 0); return out; }