本文整理汇总了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);
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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");
}
}
示例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
}
}
示例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;
}
}
示例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;
}
}
示例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);
}
示例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);
}
示例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;
}
示例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");
}
}
示例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);
}
}
}
示例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;
}
示例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");
}
}