当前位置: 首页>>代码示例>>Java>>正文


Java AEADBlockCipher.init方法代码示例

本文整理汇总了Java中org.bouncycastle.crypto.modes.AEADBlockCipher.init方法的典型用法代码示例。如果您正苦于以下问题:Java AEADBlockCipher.init方法的具体用法?Java AEADBlockCipher.init怎么用?Java AEADBlockCipher.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.bouncycastle.crypto.modes.AEADBlockCipher的用法示例。


在下文中一共展示了AEADBlockCipher.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: runTestCase

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private void runTestCase(String testName, String[] testVector, int macLengthBits, byte[] K)
    throws InvalidCipherTextException
{
    int pos = 0;
    byte[] N = Hex.decode(testVector[pos++]);
    byte[] A = Hex.decode(testVector[pos++]);
    byte[] P = Hex.decode(testVector[pos++]);
    byte[] C = Hex.decode(testVector[pos++]);

    int macLengthBytes = macLengthBits / 8;

    KeyParameter keyParameter = new KeyParameter(K);
    AEADParameters parameters = new AEADParameters(keyParameter, macLengthBits, N, A);

    AEADBlockCipher encCipher = initOCBCipher(true, parameters);
    AEADBlockCipher decCipher = initOCBCipher(false, parameters);

    checkTestCase(encCipher, decCipher, testName, macLengthBytes, P, C);
    checkTestCase(encCipher, decCipher, testName + " (reused)", macLengthBytes, P, C);

    // Key reuse
    AEADParameters keyReuseParams = AEADTestUtil.reuseKey(parameters);
    encCipher.init(true, keyReuseParams);
    decCipher.init(false, keyReuseParams);
    checkTestCase(encCipher, decCipher, testName + " (key reuse)", macLengthBytes, P, C);
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:27,代码来源:OCBTest.java

示例2: testReset

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
public static void testReset(Test test, AEADBlockCipher cipher1, AEADBlockCipher cipher2, CipherParameters params)
    throws InvalidCipherTextException
{
    cipher1.init(true, params);

    byte[] plaintext = new byte[1000];
    byte[] ciphertext = new byte[cipher1.getOutputSize(plaintext.length)];

    // Establish baseline answer
    crypt(cipher1, plaintext, ciphertext);

    // Test encryption resets
    checkReset(test, cipher1, params, true, plaintext, ciphertext);

    // Test decryption resets with fresh instance
    cipher2.init(false, params);
    checkReset(test, cipher2, params, false, ciphertext, plaintext);
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:19,代码来源:AEADTestUtil.java

示例3: transform

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
@Override
public void transform(InputStream in, OutputStream out) {
    try {
        AEADBlockCipher aeadBlockCipher = createAEADBlockCipher();

        AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key.getKey()),
                Constants.GCM_MAC_SIZE, nonce, nonSecretPayload);

        aeadBlockCipher.init(true, aeadParameters);

        // Write nonce as first 4 bytes
        out.write(this.nonce);
        try (CipherInputStream cipherInputStream = new CipherInputStream(in, aeadBlockCipher)) {
            IOUtils.copy(cipherInputStream, out);
        }
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
        throw new EncryptorException(ex.getMessage(), ex);
    }
}
 
开发者ID:dzhemriza,项目名称:toffi,代码行数:21,代码来源:BaseBlockCipherEncoder.java

示例4: transform

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
@Override
public void transform(InputStream in, OutputStream out) {
    try {
        AEADBlockCipher aeadBlockCipher = createAEADBlockCipher();

        // Read the first 4 bytes as they contains nonce
        byte[] nonce = new byte[4];
        int bytesRead = in.read(nonce);
        if (bytesRead < 4) {
            throw new RuntimeException("Failed to read nonce! Bytes read=" + bytesRead);
        }

        AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key.getKey()),
                Constants.GCM_MAC_SIZE, nonce, nonSecretPayload);

        aeadBlockCipher.init(false, aeadParameters);

        try (CipherOutputStream cipherOutputStream = new CipherOutputStream(out, aeadBlockCipher)) {
            IOUtils.copy(in, cipherOutputStream);
        }
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new EncryptorException(ex.getMessage(), ex);
    }
}
 
开发者ID:dzhemriza,项目名称:toffi,代码行数:26,代码来源:BaseBlockCipherDecoder.java

示例5: outputSizeTests

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private void outputSizeTests()
{
    byte[] K = new byte[16];
    byte[] A = null;
    byte[] IV = new byte[15];

    AEADParameters parameters = new AEADParameters(new KeyParameter(K), 16 * 8, IV, A);
    AEADBlockCipher cipher = initOCBCipher(true, parameters);

    if (cipher.getUpdateOutputSize(0) != 0)
    {
        fail("incorrect getUpdateOutputSize for initial 0 bytes encryption");
    }

    if (cipher.getOutputSize(0) != 16)
    {
        fail("incorrect getOutputSize for initial 0 bytes encryption");
    }

    cipher.init(false, parameters);

    if (cipher.getUpdateOutputSize(0) != 0)
    {
        fail("incorrect getUpdateOutputSize for initial 0 bytes decryption");
    }

    // NOTE: 0 bytes would be truncated data, but we want it to fail in the doFinal, not here
    if (cipher.getOutputSize(0) != 0)
    {
        fail("fragile getOutputSize for initial 0 bytes decryption");
    }

    if (cipher.getOutputSize(16) != 0)
    {
        fail("incorrect getOutputSize for initial MAC-size bytes decryption");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:38,代码来源:OCBTest.java

示例6: testTamperedWrite

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
/**
 * Test tampering of ciphertext followed by write to decrypting CipherOutputStream
 */
private void testTamperedWrite(AEADBlockCipher cipher, CipherParameters params)
    throws Exception
{
    cipher.init(true, params);

    byte[] ciphertext = new byte[cipher.getOutputSize(streamSize)];
    cipher.doFinal(ciphertext, cipher.processBytes(new byte[streamSize], 0, streamSize, ciphertext, 0));

    // Tamper
    ciphertext[0] += 1;

    cipher.init(false, params);
    ByteArrayOutputStream plaintext = new ByteArrayOutputStream();
    OutputStream output = createCipherOutputStream(plaintext, cipher);

    for (int i = 0; i < ciphertext.length; i++)
    {
        output.write(ciphertext[i]);
    }
    try
    {
        output.close();
        fail("Expected invalid ciphertext after tamper and write : " + cipher.getAlgorithmName());
    }
    catch (InvalidCipherTextIOException e)
    {
        // Expected
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:33,代码来源:CipherStreamTest.java

示例7: setupInputStream

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
public static InputStream setupInputStream(InputStream is, byte[] keyAndIv) {
    if (keyAndIv != null && keyAndIv.length == 48) {
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(keyAndIv, 0, iv, 0, 16);
        System.arraycopy(keyAndIv, 16, key, 0, 32);
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(true, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherInputStream(is, cipher);
    } else {
        return is;
    }
}
 
开发者ID:zom,项目名称:Zom-Android,代码行数:14,代码来源:Downloader.java

示例8: setupOutputStream

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
public static OutputStream setupOutputStream(OutputStream os, String reference) {
    if (reference != null && reference.length() == 96) {
        byte[] keyAndIv = hexToBytes(reference);
        byte[] key = new byte[32];
        byte[] iv = new byte[16];
        System.arraycopy(keyAndIv, 0, iv, 0, 16);
        System.arraycopy(keyAndIv, 16, key, 0, 32);
        AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
        cipher.init(false, new AEADParameters(new KeyParameter(key), 128, iv));
        return new CipherOutputStream(os, cipher);
    } else {
        return os;
    }
}
 
开发者ID:zom,项目名称:Zom-Android,代码行数:15,代码来源:Downloader.java

示例9: encrypt

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(randomKeyBytes), 128, randomIvBytes));
    return cipherData(cipher, data);
}
 
开发者ID:Archistar,项目名称:archistar-smc,代码行数:9,代码来源:AESGCMEncryptor.java

示例10: decrypt

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
@Override
public byte[] decrypt(byte[] data, byte[] randomKey)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException,
        IllegalStateException, InvalidCipherTextException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(randomKey), 128, randomIvBytes));
    return cipherData(cipher, data);
}
 
开发者ID:Archistar,项目名称:archistar-smc,代码行数:10,代码来源:AESGCMEncryptor.java

示例11: initOCBCipher

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private AEADBlockCipher initOCBCipher(boolean forEncryption, AEADParameters parameters)
{
    AEADBlockCipher c = createOCBCipher();
    c.init(forEncryption, parameters);
    return c;
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:7,代码来源:OCBTest.java

示例12: runLongerTestCase

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private void runLongerTestCase(int keyLen, int tagLen, String expectedOutputHex)
    throws InvalidCipherTextException
{
    byte[] expectedOutput = Hex.decode(expectedOutputHex);
    byte[] keyBytes = new byte[keyLen / 8];
    keyBytes[keyBytes.length - 1] = (byte)tagLen;
    KeyParameter key = new KeyParameter(keyBytes);

    AEADBlockCipher c1 = initOCBCipher(true, new AEADParameters(key, tagLen, createNonce(385)));
    AEADBlockCipher c2 = createOCBCipher();

    long total = 0;

    byte[] S = new byte[128];

    int n = 0;
    for (int i = 0; i < 128; ++i)
    {
        c2.init(true, new AEADParameters(key, tagLen, createNonce(++n)));
        total += updateCiphers(c1, c2, S, i, true, true);
        c2.init(true, new AEADParameters(key, tagLen, createNonce(++n)));
        total += updateCiphers(c1, c2, S, i, false, true);
        c2.init(true, new AEADParameters(key, tagLen, createNonce(++n)));
        total += updateCiphers(c1, c2, S, i, true, false);
    }

    long expectedTotal = 16256 + (48 * tagLen);

    if (total != expectedTotal)
    {
        fail("test generated the wrong amount of input: " + total);
    }

    byte[] output = new byte[c1.getOutputSize(0)];
    c1.doFinal(output, 0);

    if (!areEqual(expectedOutput, output))
    {
        fail("incorrect encrypt in long-form test");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:42,代码来源:OCBTest.java

示例13: testEncryptionWithBCLowLevelAPI

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
@Test
	public void testEncryptionWithBCLowLevelAPI() throws Exception
	{
		final byte[] plain = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
		final byte[] key = new byte[] {
				 1,  2,  3,  4,  5,  6,  7,  8,
				 9, 10, 11, 12, 13, 14, 15, 16,
				17, 18, 19, 20, 21, 22, 23, 24,
				25, 26, 27, 28, 29, 30, 31, 32
		};
		final byte[] iv = new byte[] {
				 1,  2,  3,  4,  5,  6,  7,  8,
				 9, 10, 11, 12, 13, 14, 15, 16
		};

//		{ // first try CFB - works fine, currently (2011-09-23).
//			CipherParameters parameters = new ParametersWithIV(new KeyParameter(key), iv);
//			byte[] firstCiphertext = null;
//			BufferedBlockCipher cipher = new BufferedBlockCipher(new CFBBlockCipher(new TwofishEngine(), 128));
//
//			cipher.init(true, parameters);
//
//			for (int i = 0; i < 10000; ++i) {
//				System.out.println("*** cfb " + i + " ***");
//
//				// Whether we re-initialise with or without key does not matter.
//				cipher.init(true, parameters);
//
//				byte[] ciphertext = new byte[cipher.getOutputSize(plain.length)];
//				int encLength = cipher.processBytes(plain, 0, plain.length, ciphertext, 0);
//				cipher.doFinal(ciphertext, encLength);
//
//				if (firstCiphertext == null)
//					firstCiphertext = ciphertext;
//				else
//					Assert.assertArrayEquals(firstCiphertext, ciphertext);
//			}
//		}


		{ // now try GCM - fails on 'fhernhache', currently (2011-09-23).
			byte[] firstCiphertext = null;
//			AEADParameters parameters = new AEADParameters(new KeyParameter(key), 128, iv, plain);
			final AEADBlockCipher cipher = new GCMBlockCipher(new TwofishEngine());
			final AEADBlockCipher invCipher = new GCMBlockCipher(new TwofishEngine());

			cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
			invCipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));

			for (int i = 0; i < 10000; ++i) {
				System.out.println("*** gcm " + i + " ***");
				random.nextBytes(iv);

				// Whether we re-initialise with or without key does not matter.
				cipher.init(true, new ParametersWithIV(null, iv));
				invCipher.init(false, new ParametersWithIV(null, iv));

				final byte[] ciphertext = new byte[cipher.getOutputSize(plain.length)];
				final int encLength = cipher.processBytes(plain, 0, plain.length, ciphertext, 0);
				cipher.doFinal(ciphertext, encLength);

				if (firstCiphertext == null)
					firstCiphertext = ciphertext;
				else
					assertThat(ciphertext).isNotEqualTo(firstCiphertext);

				final byte[] decrypted = new byte[cipher.getOutputSize(ciphertext.length)];
				int decLength = invCipher.processBytes(ciphertext, 0, ciphertext.length, decrypted, 0);
				decLength += invCipher.doFinal(decrypted, decLength);
				final byte[] decryptedTruncated = new byte[decLength];
				System.arraycopy(decrypted, 0, decryptedTruncated, 0, decLength);
				assertThat(decryptedTruncated).isEqualTo(plain);
			}
		}
	}
 
开发者ID:subshare,项目名称:subshare,代码行数:76,代码来源:GCMTest.java

示例14: createOCBCipher

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private static AEADBlockCipher createOCBCipher(boolean forEncryption, AEADParameters parameters) {
    AEADBlockCipher c = new OCBBlockCipher(new AESEngine(), new AESEngine());
    c.init(forEncryption, parameters);

    return c;
}
 
开发者ID:nelenkov,项目名称:keystore-decryptor,代码行数:7,代码来源:Keymaster1Blob.java

示例15: runLongerTestCase

import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private void runLongerTestCase(int aesKeySize, int tagLen, byte[] expectedOutput)
    throws InvalidCipherTextException
{
    KeyParameter key = new KeyParameter(new byte[aesKeySize / 8]);
    byte[] N = new byte[12];

    AEADBlockCipher c1 = new OCBBlockCipher(new AESFastEngine(), new AESFastEngine());
    c1.init(true, new AEADParameters(key, tagLen, N));

    AEADBlockCipher c2 = new OCBBlockCipher(new AESFastEngine(), new AESFastEngine());

    long total = 0;

    byte[] S = new byte[128];

    for (int i = 0; i < 128; ++i)
    {
        N[11] = (byte)i;

        c2.init(true, new AEADParameters(key, tagLen, N));

        total += updateCiphers(c1, c2, S, i, true, true);
        total += updateCiphers(c1, c2, S, i, false, true);
        total += updateCiphers(c1, c2, S, i, true, false);
    }

    long expectedTotal = 16256 + (48 * tagLen);

    if (total != expectedTotal)
    {
        fail("test generated the wrong amount of input: " + total);
    }

    byte[] output = new byte[c1.getOutputSize(0)];
    c1.doFinal(output, 0);

    if (!areEqual(expectedOutput, output))
    {
        fail("incorrect encrypt in long-form test");
    }
}
 
开发者ID:credentials,项目名称:irma_future_id,代码行数:42,代码来源:OCBTest.java


注:本文中的org.bouncycastle.crypto.modes.AEADBlockCipher.init方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。