本文整理汇总了Java中org.bouncycastle.crypto.modes.AEADBlockCipher.getOutputSize方法的典型用法代码示例。如果您正苦于以下问题:Java AEADBlockCipher.getOutputSize方法的具体用法?Java AEADBlockCipher.getOutputSize怎么用?Java AEADBlockCipher.getOutputSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.modes.AEADBlockCipher
的用法示例。
在下文中一共展示了AEADBlockCipher.getOutputSize方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: updateCiphers
import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private int updateCiphers(AEADBlockCipher c1, AEADBlockCipher c2, byte[] S, int i,
boolean includeAAD, boolean includePlaintext)
throws InvalidCipherTextException
{
int inputLen = includePlaintext ? i : 0;
int outputLen = c2.getOutputSize(inputLen);
byte[] output = new byte[outputLen];
int len = 0;
if (includeAAD)
{
c2.processAADBytes(S, 0, i);
}
if (includePlaintext)
{
len += c2.processBytes(S, 0, i, output, len);
}
len += c2.doFinal(output, len);
c1.processAADBytes(output, 0, len);
return len;
}
示例3: 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");
}
}
示例4: 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
}
}
示例5: CipherInputStream
import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
/**
* Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.
*/
public CipherInputStream(InputStream is, AEADBlockCipher cipher) {
super(is);
this.aeadBlockCipher = cipher;
int outSize = cipher.getOutputSize(INPUT_BUF_SIZE);
buf = new byte[(outSize > INPUT_BUF_SIZE) ? outSize : INPUT_BUF_SIZE];
inBuf = new byte[INPUT_BUF_SIZE];
}
示例6: CipherInputStream
import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
/**
* Constructs a CipherInputStream from an InputStream and an AEADBlockCipher.
*/
public CipherInputStream(InputStream is, AEADBlockCipher cipher)
{
super(is);
this.aeadBlockCipher = cipher;
buf = new byte[cipher.getOutputSize(INPUT_BUF_SIZE)];
inBuf = new byte[INPUT_BUF_SIZE];
}
示例7: updateCiphers
import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private int updateCiphers(AEADBlockCipher c1, AEADBlockCipher c2, byte[] S, int i,
boolean includeAAD, boolean includePlaintext)
throws InvalidCipherTextException
{
int inputLen = includePlaintext ? i : 0;
int outputLen = c2.getOutputSize(inputLen);
byte[] output = new byte[outputLen];
int len = 0;
if (includeAAD)
{
c2.processAADBytes(S, 0, i);
}
if (includePlaintext)
{
len += c2.processBytes(S, 0, i, output, len);
}
len += c2.doFinal(output, len);
c1.processAADBytes(output, 0, len);
return len;
}
示例8: cipherData
import org.bouncycastle.crypto.modes.AEADBlockCipher; //导入方法依赖的package包/类
private static byte[] cipherData(AEADBlockCipher cipher, byte[] data) throws IllegalStateException, InvalidCipherTextException {
int minSize = cipher.getOutputSize(data.length);
byte[] outBuf = new byte[minSize];
int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
int length2 = cipher.doFinal(outBuf, length1);
int actualLength = length1 + length2;
if (actualLength == minSize) {
return outBuf;
} else {
return Arrays.copyOf(outBuf, actualLength);
}
}
示例9: 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");
}
}
示例10: 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);
}
}
}
示例11: 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");
}
}